private void checkIfPhoneHasApp() { Log.d(TAG, "checkIfPhoneHasApp()"); PendingResult<CapabilityApi.GetCapabilityResult> pendingResult = Wearable.CapabilityApi.getCapability( mGoogleApiClient, CAPABILITY_PHONE_APP, CapabilityApi.FILTER_ALL); pendingResult.setResultCallback(new ResultCallback<CapabilityApi.GetCapabilityResult>() { @Override public void onResult(@NonNull CapabilityApi.GetCapabilityResult getCapabilityResult) { Log.d(TAG, "onResult(): " + getCapabilityResult); if (getCapabilityResult.getStatus().isSuccess()) { CapabilityInfo capabilityInfo = getCapabilityResult.getCapability(); mAndroidPhoneNodeWithApp = pickBestNodeId(capabilityInfo.getNodes()); verifyNodeAndUpdateUI(); } else { Log.d(TAG, "Failed CapabilityApi: " + getCapabilityResult.getStatus()); } } }); }
@SuppressLint("LongLogTag") @Override public void onConnected(@Nullable Bundle bundle) { Log.d(TAG, "onConnected()"); // Set up listeners for capability changes (install/uninstall of remote app). Wearable.CapabilityApi.addCapabilityListener( mGoogleApiClient, this, CAPABILITY_WEAR_APP); // Initial request for devices with our capability, aka, our Wear app installed. findWearDevicesWithApp(); // Initial request for all Wear devices connected (with or without our capability). // Additional Note: Because there isn't a listener for ALL Nodes added/removed from network // that isn't deprecated, we simply update the full list when the Google API Client is // connected and when capability changes come through in the onCapabilityChanged() method. findAllWearDevices(); }
private void setupF35Wearable() { CapabilityApi.GetCapabilityResult result = Wearable.CapabilityApi.getCapability( mGoogleApiClient, F35_WEARABLE_CAPABILITY_NAME, CapabilityApi.FILTER_REACHABLE).await(); updateTranscriptionCapability(result.getCapability()); //setupComplete(); we can fire messages CapabilityApi.CapabilityListener capabilityListener = new CapabilityApi.CapabilityListener() { @Override public void onCapabilityChanged(CapabilityInfo capabilityInfo) { updateTranscriptionCapability(capabilityInfo); } }; Wearable.CapabilityApi.addCapabilityListener( mGoogleApiClient, capabilityListener, F35_WEARABLE_CAPABILITY_NAME); }
@Override public void onConnected(@Nullable Bundle bundle) { super.onConnected(bundle); // Set up listeners for capability changes (install/uninstall of remote app). Wearable.CapabilityApi.addCapabilityListener( mGoogleApiClient, this, CAPABILITY_PHONE_APP); checkIfPhoneHasApp(); final MainApplication app = this.mApp; if (app != null) { app.onConnected(); } }
private void retrieveDeviceNode() { final CapabilityApi.GetCapabilityResult result = Wearable.CapabilityApi.getCapability(mGoogleApiClient, WearMessages.CAPABILITY_HOMBOT_HOST, Wearable.CapabilityApi.FILTER_REACHABLE).await(); // NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); Set<Node> nodes = result.getCapability().getNodes(); if (nodes.size() > 0) { for (Node node : nodes) { if (node.isNearby()) { mNodeId = node.getId(); } } } }
private void setupReachableReceiverActionTrigger() { CapabilityApi.GetCapabilityResult result = Wearable.CapabilityApi.getCapability( googleApiClient, context .getResources() .getString(eu.power_switch.shared.R.string.RECEIVER_ACTION_TRIGGER_CAPABILITY_NAME), CapabilityApi.FILTER_REACHABLE) .await(); updateReceiverActionTriggerCapability(result.getCapability()); CapabilityApi.CapabilityListener capabilityListener = new CapabilityApi.CapabilityListener() { @Override public void onCapabilityChanged(CapabilityInfo capabilityInfo) { updateReceiverActionTriggerCapability(capabilityInfo); } }; Wearable.CapabilityApi.addCapabilityListener(googleApiClient, capabilityListener, context.getResources() .getString(eu.power_switch.shared.R.string.RECEIVER_ACTION_TRIGGER_CAPABILITY_NAME)); }
private void getBestNode(long timeOut){ final String nodeCompatibilty = getString(com.crossbow.wear.R.string.crossbow_compatibility); Wearable.CapabilityApi.getCapability(googleApiClient, nodeCompatibilty, CapabilityApi.FILTER_REACHABLE).setResultCallback( new ResultCallback<CapabilityApi.GetCapabilityResult>() { @Override public void onResult(CapabilityApi.GetCapabilityResult nodes) { String nodeID = null; Set<Node> nodeList = nodes.getCapability().getNodes(); //get the nearest node for(Node node : nodeList) { if(node.isNearby()) { nodeID = node.getId(); break; } nodeID = node.getId(); } sendMessage(nodeID); } } ); }
private String getBestNode(long timeOut) throws VolleyError { final String nodeCompatibilty = context.getString(R.string.crossbow_compatibility); CapabilityApi.GetCapabilityResult nodes = Wearable.CapabilityApi.getCapability(googleApiClient, nodeCompatibilty, CapabilityApi.FILTER_REACHABLE).await(timeOut, TimeUnit.MILLISECONDS); String nodeID = null; Set<Node> nodeList = nodes.getCapability().getNodes(); if(nodeList.isEmpty()) { throw new NoConnectionError(new VolleyError("No nodes found to handle the request")); } //get the nearest node for(Node node : nodeList) { if(node.isNearby()) { return node.getId(); } nodeID = node.getId(); } return nodeID; }
@SuppressLint("LongLogTag") @Override public void onPause() { Log.d(TAG, "onPause()"); super.onPause(); if ((mGoogleApiClient != null) && mGoogleApiClient.isConnected()) { Wearable.CapabilityApi.removeCapabilityListener( mGoogleApiClient, this, CAPABILITY_PHONE_APP); mGoogleApiClient.disconnect(); } }
@Override public void onConnected(@Nullable Bundle bundle) { Log.d(TAG, "onConnected()"); // Set up listeners for capability changes (install/uninstall of remote app). Wearable.CapabilityApi.addCapabilityListener( mGoogleApiClient, this, CAPABILITY_PHONE_APP); checkIfPhoneHasApp(); }
@SuppressLint("LongLogTag") @Override public void onPause() { Log.d(TAG, "onPause()"); super.onPause(); if ((mGoogleApiClient != null) && mGoogleApiClient.isConnected()) { Wearable.CapabilityApi.removeCapabilityListener( mGoogleApiClient, this, CAPABILITY_WEAR_APP); mGoogleApiClient.disconnect(); } }
@SuppressLint("LongLogTag") private void findWearDevicesWithApp() { Log.d(TAG, "findWearDevicesWithApp()"); // You can filter this by FILTER_REACHABLE if you only want to open Nodes (Wear Devices) // directly connect to your phone. PendingResult<CapabilityApi.GetCapabilityResult> pendingResult = Wearable.CapabilityApi.getCapability( mGoogleApiClient, CAPABILITY_WEAR_APP, CapabilityApi.FILTER_ALL); pendingResult.setResultCallback(new ResultCallback<CapabilityApi.GetCapabilityResult>() { @Override public void onResult(@NonNull CapabilityApi.GetCapabilityResult getCapabilityResult) { Log.d(TAG, "findWearDevicesWithApp onResult(): " + getCapabilityResult); if (getCapabilityResult.getStatus().isSuccess()) { CapabilityInfo capabilityInfo = getCapabilityResult.getCapability(); mWearNodesWithApp = capabilityInfo.getNodes(); verifyNodeAndUpdateUI(); } else { Log.d(TAG, "Failed CapabilityApi: " + getCapabilityResult.getStatus()); } } }); }
@Override protected void destroy() { if ((mGoogleApiClient != null) && mGoogleApiClient.isConnected()) { Wearable.CapabilityApi.removeCapabilityListener( mGoogleApiClient, this, CAPABILITY_PHONE_APP); } mApp = null; super.destroy(); }
private void checkIfPhoneHasApp() { PendingResult<CapabilityApi.GetCapabilityResult> pendingResult = Wearable.CapabilityApi.getCapability( mGoogleApiClient, CAPABILITY_PHONE_APP, CapabilityApi.FILTER_ALL); pendingResult.setResultCallback((getCapabilityResult) -> { if (getCapabilityResult.getStatus().isSuccess()) { onCapabilityChanged(getCapabilityResult.getCapability()); } }); }
/** * Adds one or more capabilities to the client at runtime. Make sure you balance this with a * similar call to {@link #removeCapabilities(String...)} * * @see #removeCapabilities(String...) */ public void addCapabilities(String... capabilities) { if (capabilities == null || capabilities.length == 0) { return; } assertApiConnectivity(); for (final String capability : capabilities) { Wearable.CapabilityApi.addLocalCapability(mGoogleApiClient, capability) .setResultCallback( new ResultCallback<CapabilityApi.AddLocalCapabilityResult>() { @Override public void onResult( @NonNull CapabilityApi.AddLocalCapabilityResult addLocalCapabilityResult) { if (!addLocalCapabilityResult.getStatus().isSuccess()) { Log.e(TAG, "Failed to add the capability " + capability); } else { mWatchedCapabilities.add(capability); } for (DataConsumer consumer : mDataConsumers) { consumer.onAddCapabilityResult( addLocalCapabilityResult.getStatus() .getStatusCode()); } } }); } }
/** * Removes one or more capabilities from the client at runtime. * * @see #addCapabilities(String...) */ public void removeCapabilities(String... capabilities) { if (capabilities == null || capabilities.length == 0) { return; } assertApiConnectivity(); for (final String capability : capabilities) { Wearable.CapabilityApi.removeLocalCapability(mGoogleApiClient, capability) .setResultCallback( new ResultCallback<CapabilityApi.RemoveLocalCapabilityResult>() { @Override public void onResult( CapabilityApi.RemoveLocalCapabilityResult removeLocalCapabilityResult) { if (!removeLocalCapabilityResult.getStatus().isSuccess()) { Log.e(TAG, "Failed to remove the capability " + capability); } else { mWatchedCapabilities.remove(capability); } for (DataConsumer consumer : mDataConsumers) { consumer.onRemoveCapabilityResult( removeLocalCapabilityResult.getStatus() .getStatusCode()); } } }); } }
public void onGoogleApiClientConnected(@NonNull final Context context, @NonNull final GoogleApiClient googleApiClient) { mGoogleApiClient = googleApiClient; final String capability = context.getString(R.string.wear_capability_playback_control); Wearable.CapabilityApi.getCapability(googleApiClient, capability, CapabilityApi.FILTER_REACHABLE).setResultCallback( result -> updateRemoteControlCapability(result.getCapability())); Wearable.CapabilityApi.addCapabilityListener( googleApiClient, mCapabilityListener, capability); }
public void onGoogleApiClientDisconnected() { if (mGoogleApiClient != null) { Wearable.CapabilityApi.removeListener(mGoogleApiClient, mCapabilityListener); mGoogleApiClient = null; } if (mPlaybackControlNodeId != null && mPlaybackNodeListener != null) { mPlaybackNodeListener.onNodeConnectionStateChanged(false); } synchronized (mCapabilityLock) { mPlaybackControlNodeId = null; } }
private void findCapableNode(final String capability) { Log.d(TAG,"Start looking for a capable node"); new Thread(new Runnable() { @Override public void run() { CapabilityApi.GetCapabilityResult result = Wearable.CapabilityApi.getCapability(googleApiClient,capability,CapabilityApi.FILTER_REACHABLE).await(); CapabilityInfo capabilityInfo = result.getCapability(); Set<Node> nodes = capabilityInfo.getNodes(); String nodeID = findBestNodeId(nodes); Log.d(TAG,"Node found: "+nodeID); if (nodeID==null) { //This might be caused by there not being a watch paired to the device //Run it on the UI thread ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { errorListener.onError(new Throwable("Error, cannot find a connected device")); } }); return; } WearSocket.this.nodeID = nodeID; nodeFound.release(); } }).start(); }
/** * Sends the actual message to ask other devices that are capable of showing "details" to start * the appropriate activity * * @param path the path to pass to the wearable message API * @param extraInfo extra info that varies based on the path being sent */ private void startDeviceActivityInternal(String path, String extraInfo) { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); ConnectionResult connectionResult = googleApiClient.blockingConnect( Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS); if (connectionResult.isSuccess() && googleApiClient.isConnected()) { CapabilityApi.GetCapabilityResult result = Wearable.CapabilityApi.getCapability( googleApiClient, getApplicationContext().getString(R.string.show_detail_capability_name), CapabilityApi.FILTER_REACHABLE) .await(GET_CAPABILITY_TIMEOUT_S, TimeUnit.SECONDS); if (result.getStatus().isSuccess()) { Set<Node> nodes = result.getCapability().getNodes(); for (Node node : nodes) { Wearable.MessageApi.sendMessage( googleApiClient, node.getId(), path, extraInfo.getBytes()); } } else { Log.e(TAG, "startDeviceActivityInternal() Failed to get capabilities, status: " + result.getStatus().getStatusMessage()); } googleApiClient.disconnect(); } }
public <T> T sendMessageAndReceive(Object msg, int operationTimeout, Class<T> clazz) throws IOException { makeSureIsConnected(); CapabilityInfo capabilityInfo = Wearable.CapabilityApi.getCapability( googleClient, context.getString(R.string.jj_request_proxy), CapabilityApi.FILTER_REACHABLE).await().getCapability(); if (capabilityInfo == null) { throw new ConnectionException("CapabilityApi unavailable"); } Set<Node> nodes = capabilityInfo.getNodes(); String id = generateUniqId(); Object waitObject = new Object(); waitObjects.put(id, waitObject); sendMessage(id, pickBestNodeId(nodes), msg); try { synchronized (waitObject) { waitObject.wait(readTimeout + operationTimeout); } } catch (InterruptedException e) { throw new ConnectionException("GoogleApiClient timeout"); } finally { waitObjects.remove(id); } byte[] message = resultObjects.remove(id); if (message != null) { return readObject(message, clazz); } else { throw new ConnectionException("GoogleApiClient timeout"); } }
@Override protected Void doInBackground(Void... voids) { if (googleApiClient.isConnected()) { if (System.currentTimeMillis() - lastRequest > 20 * 1000) { // enforce 20-second debounce period lastRequest = System.currentTimeMillis(); //NodeApi.GetConnectedNodesResult nodes = // Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); if (localnode == null || (localnode != null && localnode.isEmpty())) setLocalNodeName(); CapabilityApi.GetCapabilityResult capabilityResult = Wearable.CapabilityApi.getCapability( googleApiClient, CAPABILITY_WEAR_APP, CapabilityApi.FILTER_REACHABLE).await(GET_CAPABILITIES_TIMEOUT_MS, TimeUnit.MILLISECONDS); CapabilityInfo nodes; if (!capabilityResult.getStatus().isSuccess()) { Log.e(TAG, "doInBackground Failed to get capabilities, status: " + capabilityResult.getStatus().getStatusMessage()); nodes = null; } else { nodes = capabilityResult.getCapability(); } SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit(); boolean enable_wearG5 = sharedPrefs.getBoolean("enable_wearG5", false); boolean force_wearG5 = sharedPrefs.getBoolean("force_wearG5", false); String node_wearG5 = mPrefs.getString("node_wearG5", ""); if (nodes != null && nodes.getNodes().size() > 0) { updateWearSyncBgsCapability(nodes); int count = nodes.getNodes().size(); Log.d(TAG, "doInBackground connected. CapabilityApi.GetCapabilityResult mWearNodeID=" + (mWearNodeId != null ? mWearNodeId : "") + " count=" + count);//KS boolean isConnectedToWearable = false; for (Node peer : nodes.getNodes()) { //onPeerConnected String wearNode = peer.getDisplayName() + "|" + peer.getId(); Log.d(TAG, "CheckWearableConnected onPeerConnected peer name & ID: " + wearNode); if (wearNode.equals(node_wearG5)) { isConnectedToWearable = true; sendPrefSettings(); break; } else if (node_wearG5.equals("")) { isConnectedToWearable = true; prefs.putString("node_wearG5", wearNode); prefs.commit(); break; } } sendPrefSettings(); initWearData(); if (enable_wearG5) { //Only stop service if Phone will rely on Wear Collection Service if (force_wearG5 && isConnectedToWearable) { Log.d(TAG, "CheckWearableConnected onPeerConnected force_wearG5=true Phone stopBtService and continue to use Wear BT Collector"); stopBtService(); } else { Log.d(TAG, "CheckWearableConnected onPeerConnected force_wearG5=false Phone startBtService"); startBtService(); } } } else { //onPeerDisconnected Log.d(TAG, "CheckWearableConnected onPeerDisconnected"); if (sharedPrefs.getBoolean("wear_sync", false)) { Log.d(TAG, "CheckWearableConnected onPeerDisconnected wear_sync=true Phone startBtService"); startBtService(); } } } else { Log.d(TAG, "Debounce limit hit - not sending"); } } else { Log.d(TAG, "Not connected for sending"); googleApiClient.connect(); } return null; }
/** * Clients can register to {@link DataConsumer#onGmsApiConnected()}. */ private void onConnected(Bundle bundle) { WearUtil.logD(TAG, "Google Api Connected"); for (DataConsumer consumer : mDataConsumers) { consumer.onGmsApiConnected(); } addCapabilities(mCapabilitiesToBeAdded); Wearable.CapabilityApi.getAllCapabilities(mGoogleApiClient, CapabilityApi.FILTER_REACHABLE).setResultCallback( new ResultCallback<CapabilityApi.GetAllCapabilitiesResult>() { @Override public void onResult( CapabilityApi.GetAllCapabilitiesResult getAllCapabilitiesResult) { if (getAllCapabilitiesResult.getStatus().isSuccess()) { Map<String, CapabilityInfo> capabilities = getAllCapabilitiesResult .getAllCapabilities(); if (capabilities != null) { for (String capability : capabilities.keySet()) { CapabilityInfo info = capabilities.get(capability); mCapabilityToNodesMapping.put(capability, info.getNodes()); } } onConnectedInitialCapabilitiesReceived(); } else { Log.e(TAG, "getAllCapabilities(): Failed to get all the capabilities"); } } }); Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback( new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) { if (getConnectedNodesResult.getStatus().isSuccess()) { mConnectedNodes.clear(); mConnectedNodes.addAll(getConnectedNodesResult.getNodes()); onConnectedInitialNodesReceived(); } } }); }
private void onActionSearch(@NonNull final Intent intent) { final String query = intent.getStringExtra(EXTRA_QUERY); if (query == null) { throw new IllegalArgumentException("EXTRA_QUERY is null"); } final WearSearchData.Results results = new WearSearchData.Results(); results.albums = queryAlbums(query); results.artists = queryArtists(query); results.tracks = queryTracks(query); final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API).build(); final ConnectionResult connectionResult = googleApiClient.blockingConnect(); if (!connectionResult.isSuccess()) { Log.w(TAG, "GoogleApiClient not connected: " + GoogleApiAvailability.getInstance() .getErrorString(connectionResult.getErrorCode())); return; } try { final String capability = getString(R.string.wear_capability_search_receiver); final CapabilityApi.GetCapabilityResult capabilityResult = Wearable.CapabilityApi .getCapability(googleApiClient, capability, CapabilityApi.FILTER_REACHABLE) .await(); final CapabilityInfo capabilityInfo = capabilityResult.getCapability(); if (capabilityInfo == null) { Log.w(TAG, "No search receiver devices connected"); return; } final String searchReceiverNodeId = pickBestNodeId(capabilityInfo.getNodes()); if (TextUtils.isEmpty(searchReceiverNodeId)) { Log.w(TAG, "No search receiver nodes found"); return; } final byte[] resultsArray; try { resultsArray = ProtoUtils.toByteArray(results); } catch (IOException e) { Log.w(TAG, e); return; } Wearable.MessageApi.sendMessage(googleApiClient, searchReceiverNodeId, DataPaths.Messages.SEARCH_RESULT, resultsArray).await(); } finally { googleApiClient.disconnect(); } }