How to detect if android device is paired with android wear watch

前端 未结 5 865
忘了有多久
忘了有多久 2020-12-01 08:24

I am creating an android wear app that extends push notifications. My app downloads approximately 10 images from a server when a push notification comes in and displays the

相关标签:
5条回答
  • 2020-12-01 08:48

    You need to use the NodeApi, in particular NodeApi.getConnectedNodes().

    For example (from a background thread -- otherwise use setResultCallback() and not await()):

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

    If the list of nodes returned contains at least one element, then there is a connected Android Wear device, otherwise there isn't.


    Update: With the release of Google Play Services 7.3, support for multiple Android Wear devices connected simultaneously has been added. You can use the CapabilityApi to request nodes with specific capabilities.

    0 讨论(0)
  • 2020-12-01 08:48

    Above all answers are correct if handheld device is connected only Android Watch. What if handheld device is connected to more than one device (multiwearables) like Android Watch, HealthBand, Beacon etc then above answers may not work.

    The following sections show you how to advertise device nodes that can process activity requests, discover the nodes capable of fulfilling a requested need, and send messages to those nodes,

    Advertise capabilities

    To launch an activity on a handheld device from a wearable device, use the MessageApi class to send the request. Since multiple wearables can be connected to the handheld device, the wearable app needs to determine that a connected node is capable of launching the activity. In your handheld app, advertise that the node it runs on provides specific capabilities.

    To advertise the capabilities of your handheld app:

    1. Create an XML configuration file in the res/values/ directory of your project and name it wear.xml.
    2. Add a resource named android_wear_capabilities to wear.xml.
    3. Define capabilities that the device provides.

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

    Retrieve the nodes with the required capabilities

    Initially, you can detect the capable nodes by calling the CapabilityApi.getCapability() method. The following example shows how to manually retrieve the results of reachable nodes with the android_wear capability. Use following code in Wear module:

    public abstract class BaseActivity extends Activity implements MessageApi.MessageListener, NodeApi.NodeListener,
        DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    
    private static final String
            SMART_WEAR_CAPABILITY_NAME = "android_wear";
    
    protected GoogleApiClient mGoogleApiClient;
    protected ArrayList<String> results;
    private String TAG = "BaseActivity::Wear";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    
    private Collection<String> getNodes() {
        results = new ArrayList<>();
        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
    
        for (Node node : nodes.getNodes()) {
            Log.d(TAG, node.getId());
            results.add(node.getId());
        }
    
        return results;
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        Wearable.MessageApi.removeListener(mGoogleApiClient, this);
        Wearable.NodeApi.removeListener(mGoogleApiClient, this);
        Wearable.DataApi.removeListener(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
    
    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected(): Successfully connected to Google API client");
        Wearable.MessageApi.addListener(mGoogleApiClient, this);
        Wearable.DataApi.addListener(mGoogleApiClient, this);
        Wearable.NodeApi.addListener(mGoogleApiClient, this);
        results = new ArrayList<>();
    
        getNodeIdOfHandheldDevice();
    }
    
    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "onConnectionSuspended(): Connection to Google API client was suspended");
    }
    
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e(TAG, "onConnectionFailed(): Failed to connect, with result: " + connectionResult);
    }
    
    @Override
    public void onPeerConnected(Node node) {
        Log.e(TAG, "onPeerConnected():");
    }
    
    @Override
    public void onPeerDisconnected(Node node) {
        Log.e(TAG, "onPeerDisconnected():");
    }
    
    private void getNodeIdOfHandheldDevice() {
        Wearable.CapabilityApi.getCapability(
                mGoogleApiClient, SMART_WEAR_CAPABILITY_NAME,
                CapabilityApi.FILTER_REACHABLE).setResultCallback(
                new ResultCallback<CapabilityApi.GetCapabilityResult>() {
                    @Override
                    public void onResult(CapabilityApi.GetCapabilityResult result) {
                        if (result.getStatus().isSuccess()) {
                            updateFindMeCapability(result.getCapability());
                        } else {
                            Log.e(TAG,
                                    "setOrUpdateNotification() Failed to get capabilities, "
                                            + "status: "
                                            + result.getStatus().getStatusMessage());
                        }
                    }
                });
    }
    
    private void updateFindMeCapability(CapabilityInfo capabilityInfo) {
        Set<Node> connectedNodes = capabilityInfo.getNodes();
        if (connectedNodes.isEmpty()) {
            results.clear();
        } else {
            for (Node node : connectedNodes) {
                // we are only considering those nodes that are directly connected
                if (node.isNearby()) {
                    results.add(node.getId());
                }
            }
        }
    }
    
    }
    

    For more detail please check Android Dev

    0 讨论(0)
  • 2020-12-01 08:50

    Check for paired devices

    You can check that the user has a watch paired with his device while it doesn't has to be connected at the time. This is better as this way the watch does not have to be connected at the time to be detected.

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if(device.getDeviceClass()==BluetoothClass.Device.WEARABLE_WRIST_WATCH){
                Log.d("Found", "Paired wearable: "+ device.getName() + ", with address: " + device.getAddress());
            {
        {
    } else {
        Toast.makeText(this, "No paired wearables found", Toast.LENGTH_SHORT).show();
    }
    

    Note that this code requires bluetooth permissions in your manifest.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    
    0 讨论(0)
  • 2020-12-01 08:54

    There were already 2 options listed here. They are both valid depending on your use case. I's like to add a 3rd option however incomplete.

    Option 1 : find connected nodes using NodeApi

    The NodeApi class has a method for retrieving connected nodes. With this you're sure that the user didn't just had a watch in the past or tested one sometime. He really has a watch nearby and connected.

    Drawback of this approach is that you will get no results if bluetooth is not enabled or the watch is not connected at the moment.

    The method is:

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

    A more complete code example looks like this:

    private GoogleApiClient client;
    private static final long CONNECTION_TIME_OUT_MS = 1000;
    
    public void checkIfWearableConnected() {
    
        retrieveDeviceNode(new Callback() {
            @Override
            public void success(String nodeId) {
                Toast.makeText(this, "There was at least one wearable found", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void failed(String message) {
                Toast.makeText(this, "There are no wearables found", Toast.LENGTH_SHORT).show();
            }
        });
    
    }
    
    private GoogleApiClient getGoogleApiClient(Context context) {
            if (client == null)
                client = new GoogleApiClient.Builder(context)
                        .addApi(Wearable.API)
                        .build();
            return client;
        }
    
    private interface Callback {
            public void success(final String nodeId);
            public void failed(final String message);
        }
    
    private void retrieveDeviceNode(final Callback callback) {
            final GoogleApiClient client = getGoogleApiClient(this);
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
                    NodeApi.GetConnectedNodesResult result =
                            Wearable.NodeApi.getConnectedNodes(client).await();
                    List<Node> nodes = result.getNodes();
                    if (nodes.size() > 0) {
                        String nodeId = nodes.get(0).getId();
                        callback.success(nodeId);
                    } else {
                        callback.failed("no wearables found");
                    }
                    client.disconnect();
                }
            }).start();
        }
    

    Option 2 : check for the Android Wear App

    And that is the advantage of the second option. If you get a watch the first thing you to do to get it connected is to install the Android Wear app on your handheld. So you can use the PackageManager to verify if this Android Wear app is installed.

    Drawback here is that you could get the wear app installed without having a watch.

    Code example:

    try {
        getPackageManager().getPackageInfo("com.google.android.wearable.app", PackageManager.GET_META_DATA);
    
        Toast.makeText(this, "The Android Wear App is installed", Toast.LENGTH_SHORT).show();
    } catch (PackageManager.NameNotFoundException e) {
        //android wear app is not installed
        Toast.makeText(this, "The Android Wear App is NOT installed", Toast.LENGTH_SHORT).show();
    }
    

    Option 3 : Check for paired devices

    Not sure if this is possible but in some cases this would be the perfect solution since you can check that the user has a watch paired with his device while it doesn't has to be connected at the time.

    Below is a code example, however this doesn't include checking if the paired devices are wearables or not. This is only a starting point.

    Code example from: getbondeddevices() not returning paired bluetooth devices

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
         Toast.makeText(this, "At least one paired bluetooth device found", Toast.LENGTH_SHORT).show();
        // TODO at this point you'd have to iterate these devices and check if any of them is a wearable (HOW?)
        for (BluetoothDevice device : pairedDevices) {
            Log.d("YOUR_TAG", "Paired device: "+ device.getName() + ", with address: " + device.getAddress());
        }
    } else {
        Toast.makeText(this, "No paired bluetooth devices found", Toast.LENGTH_SHORT).show();
    }
    

    Note that this code requires bluetooth permissions in your manifest.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    
    0 讨论(0)
  • 2020-12-01 08:55

    I'm using a much simpler method that is suitable for my use case, check if the android wear app is installed:

        try {
            getPackageManager().getPackageInfo("com.google.android.wearable.app", PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            //android wear app is not installed
        }
    
    0 讨论(0)
提交回复
热议问题