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

3

7

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?

Trencherman answered 22/5, 2018 at 19:42 Comment(1)
1. Could you please post the implementation of getting the peers, etc as it will help others help you; and 2. when asking for permissions, are you confirming that the permission(s) are given?Sidewinder
W
6

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

        }
    });
Whity answered 5/11, 2018 at 6:29 Comment(3)
On my Nokia 6.1 Android 9 (API Level 28) it is essential to enable Location service in the settings, checking permission alone does not help. On my Huawei P10 Lite Android 7.0 (API Level 24) checking permission alone is OK, no need to enable Location service in the settings. Either there was a change from Android 7.0 to 8.0 or it is manufacturer dependent. Note: Handling of dangerous permissions like location changed earlier, in Android 6.0.Sessoms
@priyanka singhal please do both stepsWhity
It is throwing error Method threw 'com.google.android.gms.common.api.ResolvableApiException' exception.Voltammeter
P
1

I fixed it by adding runtime permission...

Manifest.permission.ACCESS_COARSE_LOCATION

allowed and everything okay

Pyramid answered 30/10, 2018 at 0:43 Comment(1)
where in your project did you add this?Ananna
S
0

Just turn on location, its work for me

Superstratum answered 1/8, 2021 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.