Henrik
Henrik

Reputation: 31

Using Google Message API to receive data in services

I send data, bytes, from a wear watch. I want to receive the data in a background service on my phone but onMesageReceived is never called. Any idea of what I'm doing wrong? My first attempt was with an app on the phone which worked but isn't practical.

Service on mobile:

    public class LampControlService extends Service implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {
        private static final String TAG = "test";
        private static final String WEAR_MESSAGE_PATH = "/message";
        private GoogleApiClient mGoogleApiClient;
        private ArrayAdapter<String> mAdapter;
        @Override
        public void onCreate(){
            HandlerThread thread = new HandlerThread("test");
            thread.start();

            //---Build a new Google API client--
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .build();

            if (mGoogleApiClient != null && !(mGoogleApiClient.isConnected() ||
                    mGoogleApiClient.isConnecting()))
                    mGoogleApiClient.connect();    
        }

        public int onStartCommand(Intent intent, int flags, int startId){
            return START_STICKY;
        }

        @Override
        public void onDestroy() {
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onConnected(Bundle bundle) {
            Wearable.MessageApi.addListener( mGoogleApiClient, this );
            Log.d(TAG, "onConnected()");
        }

        @Override
        public void onMessageReceived( final MessageEvent messageEvent ) {
            Log.d(TAG, "onMessageReceived()");
            if (messageEvent.getPath().equalsIgnoreCase(WEAR_MESSAGE_PATH)) {
                String lamp = new String(messageEvent.getData());
                sendLampCommand(lamp);
            }
            else {
    //            super.onMessageReceived(messageEvent);
            }
        }
        // @Override
        public void onConnectionSuspended(int i) {
        }

        private static void sendLampCommand(String lamp){
          // Send lamp command to web server
        }
    }

Lines added in AndroidManifest:

<service
    android:name=".LampControlService">
    <service android:name=".DataLayerListenerService">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>
</service>

Upvotes: 0

Views: 91

Answers (2)

Sterling
Sterling

Reputation: 6635

I'd recommend that you extend WearableListenerService instead of trying to do everything yourself. It's ready-made for a use case like yours, and will ensure that all the basics are covered without you needing to worry about the details.

Full instructions are here: https://developer.android.com/training/wearables/data-layer/events.html#Listen (in the With a WearableListenerService section)

Upvotes: 1

Vyacheslav
Vyacheslav

Reputation: 27221

I faced the same issue. First of all, check Google Services Library. Use the most old which supports your device.

And/or check whether your device sleeps. Some devices behave incorrectly. I mean, open this directory:

.\Sdk\extras\google\m2repository\com\google\android\gms\play-services-wearable\

and check your version. Do not use the last version. Use 8.4.0\ or similar.

Upvotes: 1

Related Questions