James Chamberlain
James Chamberlain

Reputation: 21

Nearby Connections api not working on Android Pie

I have setup a test app for the Nearby Connections API from Google. It works perfectly on a Nexus 7 (2012) running Android 4.4.4 and a Sony Xperia XZ running Android 8.

However, when I run the app on my Pixel 3A running Android 9 I get the following error message when I try to start advertising in the app:

"com.google.android.gms.common.api.ApiException: 17: API: Nearby.CONNECTIONS_API is not available on this device."

There is also this error message which happens before the app tries to start advertising

"failed to connect to socket 'localabstract:com.jameschamberlain.nearbyconnectionstest': Connection refused"

Any help would be appreciated

public void advertise(View v) {
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                showLocationExplanation(this, LOCATION_PERMISSION_REQUEST_ADVERTISE);
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_ADVERTISE);
            }
        } else {
            // Permission has already been granted
            startAdvertising();
        }
    }


    /**
     * Begin the actual advertising using the Nearby Connections API
     */
    private void startAdvertising() {
        AdvertisingOptions advertisingOptions = new AdvertisingOptions.Builder().setStrategy(Strategy.P2P_STAR).build();
        Nearby.getConnectionsClient(getApplicationContext()).startAdvertising(
                "James",
                SERVICE_ID,
                connectionLifecycleCallback,
                advertisingOptions)
                .addOnSuccessListener(
                        new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void unusedResult) {
                                // We're advertising!
                                loggingTextView.append("\nAdvertising");
                            }
                        })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                // We were unable to start advertising.
                                loggingTextView.append("\nNot advertising");
                                loggingTextView.append("\n" + e.toString());
                                Log.e("NearbyError", e.toString());
                            }
                        });
    }

Upvotes: 2

Views: 406

Answers (1)

Willey Hute
Willey Hute

Reputation: 1078

May be your pixel 3A emulator device have not created with google APIs . When you create a new device in AVD try to create image with google APIs.

Upvotes: 0

Related Questions