Java 类com.google.android.gms.wearable.CapabilityApi 实例源码

项目:Excuser    文件:MainWearActivity.java   
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());
            }
        }
    });
}
项目:Excuser    文件:DeviceWearConnectionFragment.java   
@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();
}
项目:adrenaline_watch_face    文件:SonicBoomFace.java   
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);
}
项目:locus-addon-wearables    文件:WearCommService.java   
@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();
    }
}
项目:hombot-control    文件:WearRequestEngine.java   
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();
                }
            }
        }
    }
项目:PowerSwitch_Android    文件:MessageApiHandler.java   
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));
}
项目:airhero_watch_face    文件:F35Face.java   
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);
}
项目:CrossBow    文件:ActivityMain.java   
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);
                }
            }
    );

}
项目:CrossBow    文件:PlayNetwork.java   
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;
}
项目:Excuser    文件:MainWearActivity.java   
@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();
    }
}
项目:Excuser    文件:MainWearActivity.java   
@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();
}
项目:Excuser    文件:DeviceWearConnectionFragment.java   
@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();
    }
}
项目:Excuser    文件:DeviceWearConnectionFragment.java   
@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());
            }
        }
    });
}
项目:locus-addon-wearables    文件:WearCommService.java   
@Override
protected void destroy() {
    if ((mGoogleApiClient != null) && mGoogleApiClient.isConnected()) {
        Wearable.CapabilityApi.removeCapabilityListener(
                mGoogleApiClient,
                this,
                CAPABILITY_PHONE_APP);
    }
    mApp = null;
    super.destroy();
}
项目:locus-addon-wearables    文件:WearCommService.java   
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());
        }
    });
}
项目:GmsWear    文件:GmsWear.java   
/**
 * 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());
                                }
                            }
                        });
    }
}
项目:GmsWear    文件:GmsWear.java   
/**
 * 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());
                                }
                            }
                        });
    }
}
项目:PainlessMusicPlayer    文件:RemoteControl.java   
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);
}
项目:PainlessMusicPlayer    文件:RemoteControl.java   
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;
    }
}
项目:WearSocket    文件:WearSocket.java   
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();
}
项目:io2015-codelabs    文件:UtilityService.java   
/**
 * 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();
    }
}
项目:io2015-codelabs    文件:UtilityService.java   
/**
 * 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();
    }
}
项目:JudoNetworking    文件:MessageUtils.java   
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");
    }
}
项目:xDrip    文件:WatchUpdaterService.java   
@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;
}
项目:xDrip-plus    文件:WatchUpdaterService.java   
@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;
}
项目:GmsWear    文件:GmsWear.java   
/**
 * 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();
                    }
                }
            });
}
项目:PainlessMusicPlayer    文件:WearableSearchProviderService.java   
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();
    }

}