Detect when it is connected / disconnected to / from Android phone

Pebble watches have an intention that globally changes when Pebble is connected / disconnected. This allows phone apps to know if the watch is connected or not. I searched, but I am not able to find information about a similar feature for Android Wear. How to find out if a mobile phone is connected to the phone? Is it possible to get an event like with Pebble? thanks

+5
source share
3 answers

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> 

For more details about my solution in this Stack Overflow Question

+3
source

Have you tried NodeApi , you can use getConnectedNodes

Gets a list of nodes to which this device is currently connected.

or addListener(GoogleApiClient client, NodeApi.NodeListener listener) , which

Registers a listener to receive all node events.

Then you can use callbacks from NodeApi.NodeListener

 onPeerConnected(Node peer) and onPeerDisconnected(Node peer) 
+5
source

As akmal said, you can get devices that are connected to the phone, but know if the wearable can currently connect to your Pocket PC

 List<Node> connectedNodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await().getNodes(); 

The list contains the following data:

 Node{Moto 360 6BBF, id=3b6d19bf, hops=1, isNearby=true} 
+4
source

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


All Articles