CompanionDeviceService 没有启动

问题描述 投票:0回答:1

我正在尝试设置一个经典的蓝牙配套设备实现,但是只要设备在范围内甚至连接/断开连接,就永远不会调用该服务

这是在清单中定义服务的方式以及权限

<uses-permission android:name="android.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE"/>
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND"/>
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND"/>

<service
        android:name=".MyCompanionService"
        android:label="My Companion Service"
        android:exported="true"
        android:permission="android.permission.BIND_COMPANION_DEVICE_SERVICE">
        <intent-filter>
            <action android:name="android.companion.CompanionDeviceService" />
        </intent-filter>
    </service>

和服务类

@RequiresApi(Build.VERSION_CODES.S)
class MyCompanionService: CompanionDeviceService() {

override fun onRebind(intent: Intent?) {
    super.onRebind(intent)
    Log.d("MyCompanionService", "Rebind ${intent.toString()}")
}

override fun onUnbind(intent: Intent?): Boolean {
    Log.d("MyCompanionService", "Unbind ${intent.toString()}")
    return super.onUnbind(intent)
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Log.d("MyCompanionService", "onStartCommand ${intent.toString()}")
    return super.onStartCommand(intent, flags, startId)
}

override fun onStart(intent: Intent?, startId: Int) {
    super.onStart(intent, startId)
    Log.d("MyCompanionService", "onStart ${intent.toString()}")
}

override fun onCreate() {
    super.onCreate()
    Log.d("MyCompanionService", "onCreate")
}

override fun onDeviceAppeared(address: String) {
    super.onDeviceAppeared(address)
    Log.d("MyCompanionService", "onDeviceAppeared ${address}")
}

override fun onDeviceAppeared(associationInfo: AssociationInfo) {
    super.onDeviceAppeared(associationInfo)
    Log.d("MyCompanionService", "onDeviceAppeared ${associationInfo}")
}

override fun onDeviceDisappeared(associationInfo: AssociationInfo) {
    super.onDeviceDisappeared(associationInfo)
    Log.d("MyCompanionService", "onDeviceDisappeared ${associationInfo}")
}

override fun onDeviceDisappeared(address: String) {
    super.onDeviceDisappeared(address)
    Log.d("MyCompanionService", "onDeviceDisappeared ${address}")
}

override fun onDestroy() {
    super.onDestroy()
    Log.d("MyCompanionService", "onDestroy")
}
}

根据配套设备管理器的说法,这就是我设置如何观察设备的方式

private fun startCompanionAppPair() {
    val deviceFilter: BluetoothDeviceFilter = BluetoothDeviceFilter.Builder().build()

    val pairingRequest: AssociationRequest = AssociationRequest.Builder()
        .addDeviceFilter(deviceFilter)
        .build()

    deviceManager.associate(
        pairingRequest,
        object : CompanionDeviceManager.Callback() {
            @Deprecated("Deprecated in Java")
            override fun onDeviceFound(chooserLauncher: IntentSender) {
                startIntentSenderForResult(
                    chooserLauncher,
                    SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0
                )
            }

            override fun onFailure(error: CharSequence?) {
                // Handle the failure.
                println(error.toString())
            }
        }, null
    )

}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when (requestCode) {
        SELECT_DEVICE_REQUEST_CODE -> when (resultCode) {
            Activity.RESULT_OK -> {
                // The user chose to pair the app with a Bluetooth device.
                //val deviceToPair: ScanResult? =
                val deviceToPair: BluetoothDevice? = data?.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE)
                deviceToPair?.let { device ->
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
                        device.createBond()
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
                            deviceManager.startObservingDevicePresence(device.address)
                    }
                    // Continue to interact with the paired device.
                }
            }
        }
        else -> super.onActivityResult(requestCode, resultCode, data)
    }
}

我觉得这里缺少一些东西导致了这个问题,请注意,我目前正在使用应用程序和 Android 13 设备作为配套设备在设备上使用 Android 12 进行测试

android bluetooth bluetooth-lowenergy
1个回答
0
投票

startObservingDevicePresence
内呼叫
CompanionDeviceManager.Callback()
。根据 Android API 级别,在
onAssociationCreated
onDeviceFound
内。

在活动结果中调用

startObservingDevicePresence
对我不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.