android AudioDeviceCallback将连接的WearOS手表返回为AudioDeviceInfo.TYPE_BLUETOOTH_SCO

62o28rlo  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(159)

我正在研究检测当前活动音频输出设备的功能。我使用的是AudioManagerregisterAudioDeviceCallback方法。我面临的问题是,它返回了我的WearOS设备,该设备当前连接到手机作为可能的音频输出之一:

它被检测为TYPE_BLUETOOTH_SCO,并寻找我就像任何蓝牙耳机。显然,手表的工作原理和耳机不一样,那么有没有办法从结果中过滤掉它呢?谢谢。

mspsb9vt

mspsb9vt1#

我的解决方案是使用AudioDeviceInfo.getAddress()BluetoothAdapter.getBondedDevices()中找到AudioDeviceInfo对应的BluetoothDevice,然后查看BluetoothClass.getDeviceClass()以确定设备类。
Kotlin示例:

/**
 * Attempts to determine if the BLUETOOTH device is a wearable device that was mistakenly treated as an audio output.
 * if any attempt fails, returns true
 */
fun isAudioDevice(audioDeviceInfo: AudioDeviceInfo, bluetoothManager: BluetoothManager): Boolean {
    val nonAudioDeviceBluetoothClasses = listOf(
        BluetoothClass.Device.COMPUTER_DESKTOP,
        BluetoothClass.Device.COMPUTER_HANDHELD_PC_PDA,
        BluetoothClass.Device.COMPUTER_LAPTOP,
        BluetoothClass.Device.COMPUTER_PALM_SIZE_PC_PDA,
        BluetoothClass.Device.COMPUTER_SERVER,
        BluetoothClass.Device.COMPUTER_UNCATEGORIZED,
        BluetoothClass.Device.COMPUTER_WEARABLE,
        BluetoothClass.Device.HEALTH_BLOOD_PRESSURE,
        BluetoothClass.Device.HEALTH_DATA_DISPLAY,
        BluetoothClass.Device.HEALTH_GLUCOSE,
        BluetoothClass.Device.HEALTH_PULSE_OXIMETER,
        BluetoothClass.Device.HEALTH_PULSE_RATE,
        BluetoothClass.Device.HEALTH_THERMOMETER,
        BluetoothClass.Device.HEALTH_UNCATEGORIZED,
        BluetoothClass.Device.HEALTH_WEIGHING,
        BluetoothClass.Device.PERIPHERAL_KEYBOARD,
        BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING,
        BluetoothClass.Device.PERIPHERAL_NON_KEYBOARD_NON_POINTING,
        BluetoothClass.Device.PERIPHERAL_POINTING,
        BluetoothClass.Device.PHONE_CELLULAR,
        BluetoothClass.Device.PHONE_CORDLESS,
        BluetoothClass.Device.PHONE_ISDN,
        BluetoothClass.Device.PHONE_MODEM_OR_GATEWAY,
        BluetoothClass.Device.PHONE_SMART,
        BluetoothClass.Device.PHONE_UNCATEGORIZED,
        BluetoothClass.Device.TOY_CONTROLLER,
        BluetoothClass.Device.TOY_DOLL_ACTION_FIGURE,
        BluetoothClass.Device.TOY_GAME,
        BluetoothClass.Device.TOY_ROBOT,
        BluetoothClass.Device.TOY_UNCATEGORIZED,
        BluetoothClass.Device.TOY_VEHICLE,
        BluetoothClass.Device.WEARABLE_GLASSES,
        BluetoothClass.Device.WEARABLE_HELMET,
        BluetoothClass.Device.WEARABLE_JACKET,
        BluetoothClass.Device.WEARABLE_PAGER,
        BluetoothClass.Device.WEARABLE_UNCATEGORIZED,
        BluetoothClass.Device.WEARABLE_WRIST_WATCH,
    )
    val address = audioDeviceInfo.address
    val bluetoothDevice = bluetoothManager.adapter.bondedDevices.find { it.address == address }
    return nonAudioDeviceBluetoothClasses.contains(bluetoothDevice?.bluetoothClass?.deviceClass).not()
}

尽管这个解决方案更多的是一种变通方法,而且本身可能不可靠。我还建议看看AudioDeviceInfo.getCommunicationDevice()(在API级别31中添加)或mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO)。我怀疑WearOS设备永远不会在那里。

相关问题