6

I started creating an Android app wifi Wifi direct functionality. I do a peer discover and I like to see the peers in a listview.

I now have a app which tries to discover peers and shows the peers in a list.

But I'm facing the problem that this works fine on my Android 5 device. Here I can see my Android 8 device, but not the other way around. I cannot see my Android 5 device on my Android 8 device.

I followed steps from here: https://developer.android.com/training/connect-devices-wirelessly/nsd

And also discovered this post: Android O issues with WiFi Peer Discovery which says that I need to ask for permissions before continuing.

My manifest:

<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"/>

What could be the problem? Maybe I can provide more information if needed to solve the problem?

4

3 回答 3

5

要解决 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;
                }
            }

        }
    });
于 2018-11-05T06:29:14.690 回答
1

我通过添加运行时权限来修复它......

Manifest.permission.ACCESS_COARSE_LOCATION

允许,一切正常

于 2018-10-30T00:43:06.620 回答
-1

只需打开位置,它对我有用

于 2021-08-01T09:17:20.293 回答