我开始创建一个Android应用程序wifi无线Wifi直接功能。 我做同行发现,我喜欢在列表视图中看到同行。
我现在有一个应用程序试图发现对等体并显示列表中的对等体。
但我面临的问题是,这在Android 5设备上运行良好。在这里,我可以看到我的Android 8设备,但不是相反。我在Android 8设备上看不到我的Android 5设备。
我按照这里的步骤操作: https://developer.android.com/training/connect-devices-wirelessly/nsd
并且还发现了这篇帖子:Android O issues with WiFi Peer Discovery,其中说我需要在继续之前询问权限。
我的清单:
<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"/>
可能是什么问题?如果需要解决问题,我可以提供更多信息吗?
答案 0 :(得分:2)
要解决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;
}
}
}
});
答案 1 :(得分:0)
我通过添加运行时权限对其进行了修复...
Manifest.permission.ACCESS_COARSE_LOCATION
允许,一切正常
答案 2 :(得分:-1)
只需打开定位,它对我有用