簡體   English   中英

如何檢測Android Wear設備何時斷開連接?

[英]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. 搖籃。 您需要確保移動版本和可穿戴版本具有相同的應用程序ID,相同的版本代碼,相同的版本名稱,以及可能具有相同的播放服務版本。 如果使用項目gradle文件保存這些值並讓每個模塊引用這些值,則此方法更易於處理。

在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
}
  1. 清單。 隨着Play服務的新更新, 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>

onCapabilityChangedintent-filtercom.google.android.gms.wearable.CAPABILITY_CHANGED 除此之外,還需要告知意圖過濾器數據方案和主機。 這可以只是data android:scheme="wear" android:host="*" 可以為此意圖過濾器省略pathPrefix 請注意, com.google.android.gms.wearable.DATA_CHANGEDcom.google.android.gms.wearable.MESSAGE_RECEIVED的Intent過濾器需要定義pathPrefix才能在服務中調用它們各自的功能。

  1. 功能文件。 為了啟動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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM