Android - Wifi Direct / p2p not able to find peers on Android 8

跟風遠走 提交于 2020-05-13 06:23:06

问题


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?


回答1:


To solve above problem with Android 8(Oreo), There are two steps to do:
1) Grant location permission.

    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) Enable Location service/ Turn on GPS of device.
Below code open dialog for enable location.

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;
                }
            }

        }
    });



回答2:


I fixed it by adding runtime permission...

Manifest.permission.ACCESS_COARSE_LOCATION

allowed and everything okay



来源:https://stackoverflow.com/questions/50475371/android-wifi-direct-p2p-not-able-to-find-peers-on-android-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!