如何检测android Wear设备何时断开连接? [英] How to detect when android wear device gets disconnected?
问题描述
我有一个应用程序可以通过使用 WearableListenerService
和 onPeerConnected
/onPeerDisconnected
来检测 android 穿戴设备何时断开连接.
I had an app that could detect when an android wear device disconnected by using a WearableListenerService
and onPeerConnected
/onPeerDisconnected
.
似乎这些已被弃用,所以我现在尝试使用 onCapabilityChanged
,但我无法调用此函数.我在我的服务清单中使用它.关于这些功能的文档不是很好.
It seems these have been deprecated, so I am now trying to use onCapabilityChanged
, but I cannot get this function called. I use this in my manifest for my service. Documentation on these features are not very good.
<intent-filter>
<action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
</intent-filter>
推荐答案
所以我终于让它工作了.需要设置一些东西,但我会一一列出.
So I finally got it to work. It took a combination of things that needed to be set up, but I'll list them all.
- 摇篮.您需要确保移动版本和可穿戴版本具有相同的应用程序 ID、相同的版本代码、相同的版本名称,并且可能具有相同的播放服务版本.如果您使用项目 gradle 文件来保存这些值并让每个模块引用这些值,这将更容易处理.
在根 build.gradle 文件中有:
In the Root build.gradle file have:
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 文件中,可以参考如下所示:
In each of the module build.gradle files, these can be referenced as shown below:
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
}
- 清单.随着对播放服务的新更新,
WearableListenerService
现在必须为每个被 Android 系统调用的覆盖函数定义一个intent-filter
.对于onCapabilityChanged
函数,意图过滤器应定义为:
- The Manifest. With the new updates to the play services, a
WearableListenerService
must now have anintent-filter
defined for each overrided function to be called by the android system. In the case of theonCapabilityChanged
function, the intent filter should be defined as:
<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>
onCapabilityChanged
的 intent-filter
是 com.google.android.gms.wearable.CAPABILITY_CHANGED
.除此之外,意图过滤器还需要被告知数据方案和主机.这可以简单地是 data android:scheme="wear" android:host="*"
.此意图过滤器可以省略 pathPrefix
.请注意,com.google.android.gms.wearable.DATA_CHANGED
和 com.google.android.gms.wearable.MESSAGE_RECEIVED
的意图过滤器需要 pathPrefix
定义为能够在服务中调用各自的函数.
The intent-filter
for onCapabilityChanged
is com.google.android.gms.wearable.CAPABILITY_CHANGED
. Along with that, the intent-filter also needs to be told the data scheme and host. This can simply be data android:scheme="wear" android:host="*"
. The pathPrefix
can be omitted for this intent-filter. Notice that the intent-filter for the com.google.android.gms.wearable.DATA_CHANGED
and com.google.android.gms.wearable.MESSAGE_RECEIVED
needs the pathPrefix
defined to be able to have their respective functions called in the service.
- 能力文件.为了启动
onCapabilityChanged
功能,系统需要检测具有连接能力的设备.为此,我们必须在每个模块的 xml 文件中定义功能.
- The capability file. In order for the
onCapabilityChanged
function to launch, the system needs to detect a device with a capability being connected. To do this, we must have the capability defined in an xml file in each module.
为此,在每个模块中,在 res/values 目录中保存一个名为 wear.xml
的文件.该文件必须有一个名为 android_wear_capabilities
的字符串数组,其中包含描述您希望您的模块向其他设备宣传的功能的项目.以下是可穿戴模块中包含的 wear.xml
文件示例.
To do this, in each module, save a file named wear.xml
in the res/values directory. The file must have a string array named android_wear_capabilities
with items that describe the capabilities you wish your module to advertise to another device. Below is an example of a wear.xml
file included in a wearable module.
<?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
并且必须放在values目录中.其次,字符串数组必须命名为android_wear_capabilities
.还要确保每个模块中的每个功能都有唯一的名称.
First, It is important to note that the file must be named wear.xml
and must be placed in the values directory. Secondly, the string-array must be named android_wear_capabilities
. Also make sure that every capability in each module has a unique name.
如果以上任何一个不正确,那么 onCapabilityChanged
函数将永远不会被调用,你会沮丧地拔毛.
If any of the above is not correct, then the onCapabilityChanged
function will never be called, and you will be pulling your hair out in frustration.
现在,要实际判断设备是否已断开连接,请使用 onCapabilityChanged
函数:
Now, to actually tell if a device was disconnected, use the onCapabilityChanged
function:
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 个设备.
This function will tell you when a device has connected or disconnected, assuming only 1 device is connected at a time.
这篇关于如何检测android Wear设备何时断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!