Detect when wearable connected/disconnected to/from Android phone

前端 未结 3 1484
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 21:57

The Pebble watch has a Intent that is globally sended when the Pebble is connected/disconnected. This allow the phone apps to know if the watch is connected or not. I have s

相关标签:
3条回答
  • 2021-01-18 22:04

    You can easily do this by extends the WearableListenerService and override 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 a xml file in values/ directory named wear.xml

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

    For more, checkout the my solution in this Stack Overflow Question

    0 讨论(0)
  • 2021-01-18 22:12

    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)
    
    0 讨论(0)
  • 2021-01-18 22:25

    As akmal said you can get devices which are connected to the phone, But to know whether the wearable currently connected with handheld

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

    The list contains data like this:

    Node{Moto 360 6BBF, id=3b6d19bf, hops=1, isNearby=true}
    
    0 讨论(0)
提交回复
热议问题