Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 these additional images on the watch. These images are specific to the android wear app and are not shown on the handheld device.

How do I tell if the handheld device is paired with an android wear device so that I can determine if it is necessary to download the additional images required for the wear app?

Thanks!

like image 775
TWilly Avatar asked Jul 22 '14 00:07

TWilly


People also ask

Why won't My Wear OS watch connect to my phone?

If you're having issues connecting your watch and phone, please begin by making sure that your phone's OS version is compatible (Android 6.0+ and iOS 10.0+) and that the Wear OS by Google app is up-to-date. Then, check if you have activated Bluetooth on your phone, disable, and re-enable it.


2 Answers

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"/> 
like image 139
hcpl Avatar answered Nov 27 '22 16:11

hcpl


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.

like image 21
matiash Avatar answered Nov 27 '22 14:11

matiash