如何检测 android 磨损设备何时断开连接?

How to detect when android wear device gets disconnected?

我有一个应用程序可以通过使用 WearableListenerServiceonPeerConnected/onPeerDisconnected.

来检测 android 磨损设备何时断开连接

似乎这些已被弃用,所以我现在尝试使用 onCapabilityChanged,但我无法调用此函数。我在我的服务清单中使用它。有关这些功能的文档不是很好。

<intent-filter>
            <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
</intent-filter>

所以我终于让它工作了。它综合了一些需要设置的东西,但我会把它们全部列出来。

  1. Gradle。您需要确保移动版本和可穿戴版本具有相同的应用程序 ID、相同的版本代码、相同的版本名称,并且可能还有相同的播放服务版本。如果您使用项目 gradle 文件来保存这些值并让每个模块引用这些值,这将更容易处理。

在根 build.gradle 文件中有:

ext {
    TARGET_SDK_VERSION = 25
    VERSION_CODE = 7
    VERSION_NAME = '2.0'

    COMPILE_SDK_VERSION = 25
    BUILD_TOOLS_VERSION = '25.0.2'

    APPLICATION_ID = "com.example.projectname"

    PLAY_SERVICES_WEARABLE =  'com.google.android.gms:play-services-wearable:9.4.0'
}

在每个模块 build.gradle 文件中,可以参考如下所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.COMPILE_SDK_VERSION
    buildToolsVersion rootProject.ext.BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId rootProject.ext.APPLICATION_ID
        minSdkVersion 20
        targetSdkVersion rootProject.ext.TARGET_SDK_VERSION
        versionCode rootProject.ext.VERSION_CODE
        versionName rootProject.ext.VERSION_NAME
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    provided 'com.google.android.wearable:wearable:2.0.1'
    compile 'com.google.android.support:wearable:2.0.1'
    compile rootProject.ext.PLAY_SERVICES_WEARABLE
}
  1. 清单。随着播放服务的新更新,WearableListenerService 现在必须为每个要被 android 系统调用的覆盖函数定义一个 intent-filter。在 onCapabilityChanged 函数的情况下,intent 过滤器应定义为:
    <service
        android:name=".MyWearableListenerService"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
            <data android:scheme="wear" android:host="*"/>
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:scheme="wear" android:host="*" android:pathPrefix="/PREF"/>
            <data android:scheme="wear" android:host="*" android:pathPrefix="/start"/>
        </intent-filter>
    </service>

onCapabilityChangedintent-filtercom.google.android.gms.wearable.CAPABILITY_CHANGED。除此之外,还需要告知 intent-filter 数据方案和主机。这可以简单地是 data android:scheme="wear" android:host="*"。这个 intent-filter 可以省略 pathPrefix。请注意,com.google.android.gms.wearable.DATA_CHANGEDcom.google.android.gms.wearable.MESSAGE_RECEIVED 的 intent-filter 需要定义 pathPrefix 才能在服务中调用它们各自的函数。

  1. 能力文件。为了启动 onCapabilityChanged 功能,系统需要检测具有连接能力的设备。为此,我们必须在每个模块的 xml 文件中定义功能。

为此,在每个模块中,在 res/values 目录中保存一个名为 wear.xml 的文件。该文件必须有一个名为 android_wear_capabilities 的字符串数组,其中的项目描述了您希望模块向另一台设备宣传的功能。下面是可穿戴模块中包含的 wear.xml 文件的示例。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="android_wear_capabilities">
        <item>verify_remote_wear_app</item>
    </string-array>
</resources>

首先,重要的是要注意文件必须被命名为wear.xml并且必须被放置在值中目录。其次,字符串数组 必须 命名为 android_wear_capabilities。还要确保每个模块中的每个功能都有一个唯一的名称。

如果以上任何一条都不正确,那么 onCapabilityChanged 函数将永远不会被调用,你会很沮丧。

现在,要真正判断设备是否已断开连接,请使用 onCapabilityChanged 函数:

public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
        super.onCapabilityChanged(capabilityInfo);
        if(capabilityInfo.getNodes().size() > 0){
            Log.d(TAG, "Device Connected");
        }else{
            Log.d(TAG, "No Devices");
        }
    }

假设一次只连接 1 台设备,此功能会告诉您设备何时连接或断开连接。