以下代码示例返回错误代码:0,这是android中内部错误的错误代码。是否有任何解决方法可以在 Android 10 设备中发现对等点?
wifip2pmanager.discoverPeers(wifip2pmanagerChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
status.setText("Peer Discovery Started");
}
@Override
public void onFailure(int reason) {
status.setText("Error code:" + reason);
}
});
除了列表中的声明外,还需要动态申请该权限。
我今天在使用android 33时遇到了这个问题,通过添加
ACCESS_FINE_LOCATION
和NEARBY_WIFI_DEVICES
解决了它。请求应用程序权限:当 Activity 处于 onCreate() 时
@Override
protected void onCreate(Bundle savedInstanceState) {
...
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.NEARBY_WIFI_DEVICES,
}, 0);
...
}
检查授予/拒绝的权限:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
for(int i = 0; i < permissions.length; i++){
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
Log.d("MainActivity", "Permission " + permissions[i] + " granted");
}else{
Log.d("MainActivity", "Permission " + permissions[i] + " denied");
}
}
}
}