我开始创建一个 Android 应用 wifi Wifi 直连功能。 我做了一个同行发现,我喜欢在 ListView 中看到同行。
我现在有一个应用程序,它试图发现对等点并在列表中显示对等点。
但我面临的问题是这在我的 Android 5 设备上运行良好。在这里我可以看到我的 Android 8 设备,但反之则看不到。我在 Android 8 设备上看不到我的 Android 5 设备。
我按照这里的步骤操作: https://developer.android.com/training/connect-devices-wirelessly/nsd
并且还发现了这个帖子:Android O issues with WiFi Peer Discovery这表示我需要在继续之前请求许可。
我的 list :
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
可能是什么问题?如果需要解决问题,也许我可以提供更多信息?
最佳答案
要解决 Android 8(Oreo) 的上述问题,需要执行两个步骤:
1)授予位置权限。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
}else{
mManager.requestPeers(mChannel, mainActivity.peerListListener);
//do something, permission was previously granted; or legacy device
}
2) 启用位置服务/打开设备的 GPS。
下面的代码打开对话框以启用位置。
public static void displayLocationSettingsRequest(final Context context, final int REQUEST_CHECK_SETTINGS) {
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(context).checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
LocationSettingsResponse response = null;
try {
response = task.getResult(ApiException.class);
} catch (ApiException exception) {
exception.printStackTrace();
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult((MainActivity)context, REQUEST_CHECK_SETTINGS);
break;
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
});
关于Android - Wifi Direct/p2p 无法在 Android 8 上找到对等点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50475371/