public static void initMessageBroadcaster(final Context context) { Wearable.getCapabilityClient(context).addListener(new CapabilityClient.OnCapabilityChangedListener() { @Override public void onCapabilityChanged(@NonNull CapabilityInfo capabilityInfo) { updateBestNode(context); } }, "apde_receive_logs"); // Can't do this on the main thread (new Thread(new Runnable() { @Override public void run() { updateBestNode(context); } })).start(); }
@Override public void onResume() { super.onResume(); mDataItemGeneratorFuture = mGeneratorExecutor.scheduleWithFixedDelay( new DataItemGenerator(), 1, 5, TimeUnit.SECONDS); mStartActivityBtn.setEnabled(true); mSendPhotoBtn.setEnabled(mCameraSupported); // Instantiates clients without member variables, as clients are inexpensive to create and // won't lose their listeners. (They are cached and shared between GoogleApi instances.) Wearable.getDataClient(this).addListener(this); Wearable.getMessageClient(this).addListener(this); Wearable.getCapabilityClient(this) .addListener(this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE); }
@Override public void onPause() { super.onPause(); mDataItemGeneratorFuture.cancel(true /* mayInterruptIfRunning */); Wearable.getDataClient(this).removeListener(this); Wearable.getMessageClient(this).removeListener(this); Wearable.getCapabilityClient(this).removeListener(this); }
private static void updateBestNode(Context context) { bestNodeId = null; try { CapabilityInfo info = Tasks.await(Wearable.getCapabilityClient(context).getCapability("apde_receive_logs", CapabilityClient.FILTER_REACHABLE)); for (Node node : info.getNodes()) { if (node.isNearby()) { bestNodeId = node.getId(); } } } catch (Exception e) { // Don't call printStackTrace() because that would make an infinite loop Log.e("apde", e.toString()); } }
@Override public void run() { boolean successful = false; try { CapabilityInfo info = Tasks.await(Wearable.getCapabilityClient(context).getCapability("apde_run_watch_sketches", CapabilityClient.FILTER_REACHABLE)); for (Node node : info.getNodes()) { if (node.isNearby()) { successful = true; break; } } } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } if (successful) { callback.success(); } else { callback.failure(); } } })).start();