如何解决在某些设备上发现 p2p 连接的对等点失败,在某些设备上它如何失败?
这是我的代码,它在设备 realme c12 上运行良好并发现可用设备,但在 realme3i 上失败。还建议我在服务器端和客户端连接,如何通过套接字编程连接。
public class discoverActivity extends AppCompatActivity {
LottieAnimationView discoverani;
Handler discoveranimae = new Handler();
wifip2pManager manager;
wifip2pManager.Channel channel;
broadcastReceiver receiver;
CustomAdapterFordiscoveredDevice customAdapterFordiscoveredDevice;
public static ArrayList<wifip2pDevice> peerslist = new ArrayList<wifip2pDevice>();
public static String[] devisename;
wifip2pDevice[] deviceslist;
RecyclerView recyclerView;
IntentFilter intentFilter;
CustomAdapterFordiscoveredDevice.RecycleDeviceonclick listner;
TextView deviceStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discover);
recyclerView = findViewById(R.id.devicesdiscovered);
deviceStatus = findViewById(R.id.deviceStatus);
deviceStatus.setText("discovery Started");
discoverani = findViewById(R.id.discoveranimae);
Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
discoveranimae.post(new Runnable() {
@Override
public void run() {
discoverani.playAnimation();
}
});
}
};
thread1.start();
manager = (wifip2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this,getMainLooper(),null);
receiver = new WifiDirectbroadcastReceiver(manager,channel,this);
intentFilter = new IntentFilter();
intentFilter.addAction(wifip2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(wifip2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(wifip2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(wifip2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
registerReceiver(receiver,intentFilter);
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Todo: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions,and then overriding
// public void onRequestPermissionsResult(int requestCode,String[] permissions,// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
manager.discoverPeers(channel,new wifip2pManager.ActionListener() {
@SuppressLint("MissingPermission")
@Override
public void onSuccess() {
Log.d("discovery","Started");
// manager.requestPeers(channel,peerListListener);
}
@Override
public void onFailure(int reason) {
Log.d("discovery","Failed");
}
});
}
wifip2pManager.PeerListListener peerListListener = new wifip2pManager.PeerListListener() {
@Override
public void onPeersAvailable(wifip2pDeviceList peers) {
if (!peers.getDeviceList().equals(peerslist)) {
peerslist.clear();
peerslist.addAll(peers.getDeviceList());
devisename = new String[peers.getDeviceList().size()];
deviceslist = new wifip2pDevice[peers.getDeviceList().size()];
int index = 0;
for (wifip2pDevice device : peers.getDeviceList()) {
devisename[index] = device.deviceName;
deviceslist[index] = device;
index++;
}
setonClicklistner();
customAdapterFordiscoveredDevice = new CustomAdapterFordiscoveredDevice(discoverActivity.this,listner);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(customAdapterFordiscoveredDevice);
Log.d("number of device found",String.valueOf(devisename.length));
}
if (peerslist.size() == 0) {
Toast.makeText(getApplicationContext(),"No Device Found",Toast.LENGTH_LONG).show();
return;
}
}
};
private void setonClicklistner() {
listner = new CustomAdapterFordiscoveredDevice.RecycleDeviceonclick() {
@Override
public void Onclick(View itemView,final int adapterPosition) {
Toast.makeText(getApplicationContext(),devisename[adapterPosition],Toast.LENGTH_SHORT).show();
wifip2pDevice device = deviceslist[adapterPosition];
wifip2pConfig config = new wifip2pConfig();
config.deviceAddress = device.deviceAddress;
if (ActivityCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Todo: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions,and then overriding
// public void onRequestPermissionsResult(int requestCode,// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
manager.connect(channel,config,new wifip2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(getApplicationContext(),"connected to"+devisename[adapterPosition],Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reason) {
Toast.makeText(getApplicationContext(),"Failed to connected to"+devisename[adapterPosition],Toast.LENGTH_SHORT).show();
}
});
}
};
}
wifip2pManager.ConnectionInfoListener connectionInfoListener=new wifip2pManager.ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(wifip2pInfo info) {
final InetAddress inetAddress=info.groupOwnerAddress;
if(info.groupFormed && info.isGroupOwner){
deviceStatus.setText("Host");
}
else if(info.groupFormed){
deviceStatus.setText("Client");
}
}
};
@Override
protected void onResume() {
super.onResume();
registerReceiver(receiver,intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
}
我希望我提供了足够的细节,请提出建议。为什么在某些特定设备上它会失败,我试图从 logcat 了解,但他们没有任何详细信息
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。