[英]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.