Detect when wearable connected/disconnected to/from Android phone

前端 未结 3 1488
没有蜡笔的小新
没有蜡笔的小新 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 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() {
                    @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 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

    
        
            is_connection_lost
        
    
    

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

提交回复
热议问题