Android Wear Disconnect Notification

I have an Android Wear app that transfers accelerometer values ​​to a Handheld app.

I want to be notified in the Handheld app when the connection to the Android app has just been lost (for example, the Android Wear app is closed).

How do I implement this?

thanks for the help

+2
source share
2 answers

Devices connected to the network due to wear are called nodes. You can use the Node API to determine when devices enter or exit the network (for example, when a wear device is disconnected from the phone).

https://developers.google.com/android/reference/com/google/android/gms/wearable/NodeApi.NodeListener

You can also use the Node API to get a list of all connected devices (for example, all wearing devices connected to the phone) at any given time.

https://developers.google.com/android/reference/com/google/android/gms/wearable/NodeApi

+1
source

You can easily do this by extending the WearableListenerService method and overriding the onConnectedNodes () method.

Wearable side

public class DisconnectListenerService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks { /* the capability that the phone app would provide */ private static final String CONNECTION_STATUS_CAPABILITY_NAME = "is_connection_lost"; private GoogleApiClient mGoogleApiClient; @Override public void onCreate() { super.onCreate(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .build(); } @Override public void onConnectedNodes(List<Node> connectedNodes) { if (mGoogleApiClient.isConnected()) { updateStatus(); } else if (!mGoogleApiClient.isConnecting()) { mGoogleApiClient.connect(); } } private void updateStatus() { Wearable.CapabilityApi.getCapability( mGoogleApiClient, CONNECTION_STATUS_CAPABILITY_NAME, CapabilityApi.FILTER_REACHABLE).setResultCallback( new ResultCallback<CapabilityApi.GetCapabilityResult>() { @Override public void onResult(CapabilityApi.GetCapabilityResult result) { if (result.getStatus().isSuccess()) { updateConnectionCapability(result.getCapability()); } else { Log.e(TAG, "Failed to get capabilities, " + "status: " + result.getStatus().getStatusMessage()); } } }); } private void updateConnectionCapability(CapabilityInfo capabilityInfo) { Set<Node> connectedNodes = capabilityInfo.getNodes(); if (connectedNodes.isEmpty()) { // The connection is lost ! } else { for (Node node : connectedNodes) { if (node.isNearby()) { // The connection is OK ! } } } } @Override public void onConnected(Bundle bundle) { updateStatus(); } @Override public void onConnectionSuspended(int cause) { } @Override public void onDestroy() { if (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting()) { mGoogleApiClient.disconnect(); } super.onDestroy(); } } 

Phone side

create an xml file in the / values ​​directory named wear.xml

 <resources> <string-array name="android_wear_capabilities"> <item>is_connection_lost</item> </string-array> </resources> 
0
source

Source: https://habr.com/ru/post/1201619/


All Articles