Search code examples
androidgoogle-apiraspberry-pi3android-thingsgoogle-nearby

Cannot discover Raspberry pi using Nearby api


I am working on a project where I have to use Google's Nearby API to communicate between Raspberry pi3 (working on Android things) and android mobile. I started advertising service on Raspberry pi3 and discovery service on mobile successfully. But I can't discover Raspberry pi3 on my mobile. Here is my code-

On Raspberry pi3:

private void startAdvertizing() {

    logging("advertizing initiated");



    Nearby.Connections.startAdvertising(mCLient,"Tinku",getString(R.string.service_id),mConnectionLifecycleCallback
                                        ,new AdvertisingOptions(Strategy.P2P_CLUSTER)).setResultCallback(new ResultCallback<Connections.StartAdvertisingResult>() {
        @Override
        public void onResult(@NonNull Connections.StartAdvertisingResult startAdvertisingResult) {
            if (startAdvertisingResult.getStatus().isSuccess()){
                logging("avertizind success");
            } else {
                logging(startAdvertisingResult.getStatus().getStatusMessage());
            }
        }
    });

}

On mobile side:

 private void startDicovery() {
    Log.d(TAG,"Initializing discovery");
    tv.setText("Initializing discovery");

    EndpointDiscoveryCallback mEndPointDiscoveryCallback = new EndpointDiscoveryCallback() {
        @Override
        public void onEndpointFound(String s, DiscoveredEndpointInfo discoveredEndpointInfo) {
            Log.d(TAG,"Endpoint found");
            tv.setText("Endpoint found");
        }

        @Override
        public void onEndpointLost(String s) {
            Log.d(TAG,"Endpoint Lost, "+s);
            tv.setText("Endpoint Lost, "+s);
        }
    };


    Nearby.Connections.startDiscovery(mCLient,getString(R.string.service_id),mEndPointDiscoveryCallback,new DiscoveryOptions(Strategy.P2P_CLUSTER))
                        .setResultCallback(new ResultCallback<Status>() {
                            @Override
                            public void onResult(@NonNull Status status) {
                                if (status.isSuccess()){
                                    logging("Discovert success");
                                    isDiscovery = true;
                                } else {
                                    logging("Discovery failed "+ status.getStatusMessage());
                                    isDiscovery = false;
                                }
                            }
                        });

}

I don't know what I am missing. Any help would be appreciated.


Solution

  • Hmm, I've seen this working before. There's a couple things you can try.

    1) Check the logs. See Is it possible to monitor discovering process when using Google Nearby Connections 2.0?

    2) Try it on two Android phones just to make sure the code works. The snippets look good, but you never know.

    3) Send me the model & Android OS build version of your phone and your Android Things device.