![](/img/trans.png)
[英]How to detect whenever my android device gets connected/disconnected from paired bluetooth device
[英]How to detect when android wear device gets disconnected?
我有一个应用程序,该应用程序可以通过使用WearableListenerService
和onPeerConnected
/ onPeerDisconnected
来检测android穿戴设备何时断开连接。
似乎这些功能已被弃用,因此我现在尝试使用onCapabilityChanged
,但无法调用此函数。 我在清单中使用它来提供服务。 这些功能的文档不是很好。
<intent-filter>
<action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
</intent-filter>
所以我终于让它工作了。 它需要组合设置一些内容,但我将列出所有内容。
在Root 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
}
WearableListenerService
现在必须为要被android系统调用的每个覆盖函数定义一个intent-filter
。 对于onCapabilityChanged
函数,意图过滤器应定义为: <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
的Intent过滤器需要定义pathPrefix
才能在服务中调用它们各自的功能。
onCapabilityChanged
函数,系统需要检测已连接功能的设备。 为此,我们必须具有在每个模块的xml文件中定义的功能。 为此,在每个模块中,将一个名为wear.xml
的文件保存在res / values目录中。 该文件必须具有一个名为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
并且必须放置在values目录中。 其次,字符串数组必须命名为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");
}
}
假设一次仅连接一台设备,此功能将告诉您何时连接或断开了设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.