J.Maduka
J.Maduka

Reputation: 93

WifiP2pManager.discoverPeers fails in android 10

following code sample returns Error code: 0, which is the error code for internal error in android. Is there any workaround which can enable discovering peers in android 10 devices?

                wifip2pmanager.discoverPeers(wifip2pmanagerChannel, new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    status.setText("Peer Discovery Started");
                }

                @Override
                public void onFailure(int reason) {

                    status.setText("Error code:" + reason);
                }
            });

Upvotes: 5

Views: 2984

Answers (3)

DawnOfJune
DawnOfJune

Reputation: 1

I faced the issue today when using android 33, solved it by adding ACCESS_FINE_LOCATION and NEARBY_WIFI_DEVICES. To ask for app permissions: when the activity is onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.NEARBY_WIFI_DEVICES,
    }, 0);
    ...
}

To check for granted/denied permissions:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 0) {
        for(int i = 0; i < permissions.length; i++){
            if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
                Log.d("MainActivity", "Permission " + permissions[i] + " granted");
            }else{
                Log.d("MainActivity", "Permission " + permissions[i] + " denied");
            }
        }
    }

}

Upvotes: 0

luke8800gts
luke8800gts

Reputation: 428

Exactly the same happened to me...

ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION are not enough. The user has to explcitly activate the location services! (in my case turning on location solved the problem...)

This means: Either you activate location in your settings manually or you make a usability friendly request to the user to activate location services (looks similar to permission request window; see google maps)

See this question for example code of the latter. Hope this helps!

Edit: If you search for an anwser that not envolves any Google libs, see the anwser to this question.

Upvotes: 9

Ambrose
Ambrose

Reputation: 21

In addition to the statement in the list, you also need to dynamically apply for this permission.

Upvotes: 2

Related Questions