检测何时可穿戴连接/断开与Android手机的连接 [英] Detect when wearable connected/disconnected to/from Android phone

查看:136
本文介绍了检测何时可穿戴连接/断开与Android手机的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pebble手表有一个Intent,当Pebble连接/断开连接时,全局被发送。这允许手机应用程序知道手表是否连接。
我搜索过,但我无法找到有关Android Wear类似功能的信息。
如何知道穿戴者是否连接到手机?有可能收到像卵石一样的事件吗?
感谢

解决方案

您可以通过扩展WearableListenerService并覆盖onConnectedNodes()方法来轻松实现。



可穿戴端

  public class DisconnectListenerService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks {

/ *手机应用程序提供的功能* /
private static final String CONNECTION_STATUS_CAPABILITY_NAME =is_connection_lost;

私人GoogleApiClient mGoogleApiClient;

@Override
public void onCreate(){
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.build();


@Override
public void onConnectedNodes(List&Node> connectedNodes){
if(mGoogleApiClient.isConnected()){
updateStatus() ;
} else if(!mGoogleApiClient.isConnecting()){
mGoogleApiClient.connect();
}
}

private void updateStatus(){
Wearable.CapabilityApi.getCapability(
mGoogleApiClient,CONNECTION_STATUS_CAPABILITY_NAME,
CapabilityApi.FILTER_REACHABLE) .setResultCallback(
new ResultCallback&CapabilityApi.GetCapabilityResult>(){
@Override
public void onResult(CapabilityApi.GetCapabilityResult result){
if(result.getStatus()。isSuccess ()){
updateConnectionCapability(result.getCapability());
} else {
Log.e(TAG,无法获取功能,+status:+ result.getStatus ().getStatusMessage());
}
}
});
}

private void updateConnectionCapability(CapabilityInfo capabilityInfo){
Set< Node> connectedNodes = capabilityInfo.getNodes();
if(connectedNodes.isEmpty()){
//连接丢失!
} else {
for(Node node:connectedNodes){
if(node.isNearby()){
//连接正常!
}
}
}
}

@Override
public void onConnected(Bundle bundle){
updateStatus();
}

@Override
public void onConnectionSuspended(int cause){

}

@Override
public void onDestroy(){
if(mGoogleApiClient.isConnected()|| mGoogleApiClient.isConnecting()){
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
}

电话端 p>

在名为wear.xml的值/目录中创建一个xml文件

  ;资源> 
< string-array name =android_wear_capabilities>
< item> is_connection_lost< / item>
< / string-array>
< / resources>

有关更多信息,请在此堆栈溢出问题


The Pebble watch has a Intent that is globally sended when the Pebble is connected/disconnected. This allow the phone apps to know if the watch is connected or not. I have searched but I'm not capable to locate info about a similar feature for Android Wear. How know if a wearable is connected to the phone? Is possible to receive a event like with Pebble? Thanks

解决方案

You can easily do this by extends the WearableListenerService and override onConnectedNodes() method.

Wearable Side

public class DisconnectListenerService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks {

/* the capability that the phone app would provide */
private static final String CONNECTION_STATUS_CAPABILITY_NAME = "is_connection_lost";

private GoogleApiClient mGoogleApiClient;

@Override
public void onCreate() {
    super.onCreate();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .build();
}

@Override
public void onConnectedNodes(List<Node> connectedNodes) {
    if (mGoogleApiClient.isConnected()) {
        updateStatus();
    } else if (!mGoogleApiClient.isConnecting()) {
        mGoogleApiClient.connect();
    }
}

private void updateStatus() {
    Wearable.CapabilityApi.getCapability(
            mGoogleApiClient, CONNECTION_STATUS_CAPABILITY_NAME,
            CapabilityApi.FILTER_REACHABLE).setResultCallback(
            new ResultCallback<CapabilityApi.GetCapabilityResult>() {
                @Override
                public void onResult(CapabilityApi.GetCapabilityResult result) {
                    if (result.getStatus().isSuccess()) {
                        updateConnectionCapability(result.getCapability());
                    } else {
                        Log.e(TAG, "Failed to get capabilities, " + "status: " + result.getStatus().getStatusMessage());
                    }
                }
            });
}

private void updateConnectionCapability(CapabilityInfo capabilityInfo) {
    Set<Node> connectedNodes = capabilityInfo.getNodes();
    if (connectedNodes.isEmpty()) {
        // The connection is lost !
    } else {
        for (Node node : connectedNodes) {
            if (node.isNearby()) {
                // The connection is OK !
            }
        }
    }
}

@Override
public void onConnected(Bundle bundle) {
    updateStatus();
}

@Override
public void onConnectionSuspended(int cause) {

}

@Override
public void onDestroy() {
    if (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting()) {
        mGoogleApiClient.disconnect();
    }
    super.onDestroy();
    }
}

Phone Side

create a xml file in values/ directory named wear.xml

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

For more, checkout the my solution in this Stack Overflow Question

这篇关于检测何时可穿戴连接/断开与Android手机的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆