Android Companion设备配对无法检测到蓝牙设备的问题求助
Android Companion设备配对无法检测到蓝牙设备的问题求助
大家好,我现在正在尝试用Companion Device Pairing功能让两台蓝牙设备配对,其中一台是Android 15系统,另一台是Android 16,已经开启了定位服务,但目前遇到了设备检测不到的问题,以下是我的代码(注:最后部分似乎被截断了,比如pairedDevices.fi,后续会补充完整),希望各位大佬帮忙看看哪里出问题了🥺
我的代码实现
class MainActivity : AppCompatActivity() { private var isBluetoothEnabled = false private var isClient = false private val bluetoothPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> val allGranted = permissions.all { it.value } if (allGranted) { enableBluetooth() // 权限通过后启动蓝牙 } else { isBluetoothEnabled = false } } private fun checkAndRequestBluetoothPermissions() { val neededPermissions = mutableListOf<String>() Log.i("here123","1") when { Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { // Android 12+ 所需权限 neededPermissions.add(Manifest.permission.BLUETOOTH_CONNECT) neededPermissions.add(Manifest.permission.BLUETOOTH_SCAN) } else -> { // Android 11及以下无需运行时权限 return } } Log.i("here123","2") val notGranted = neededPermissions.filter { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED } Log.i("here123","3") if (notGranted.isNotEmpty()) { Log.i("here123","4") bluetoothPermissionLauncher.launch(notGranted.toTypedArray()) } else { Log.i("here123","5") enableBluetooth() } } fun isLocationServiceEnabled(): Boolean { val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) } private fun enableBluetooth(){ if (!mBluetoothAdapter.isEnabled) { Log.i("here123","6") val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) Log.i("here123","7") enableBluetoothLauncher.launch(enableBtIntent) } else { isBluetoothEnabled = true } } private val enableBluetoothLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> isBluetoothEnabled = if (result.resultCode == RESULT_OK) { // 用户已开启蓝牙 true } else { // 用户取消或蓝牙未开启 false } Log.i("here123","8") if (!isLocationServiceEnabled()) { val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS) startActivity(intent) } Log.i("here123","9") } val classicBluetoothDeviceFilter = BluetoothDeviceFilter.Builder() .build() val leBluetoothDeviceFilter = BluetoothLeDeviceFilter.Builder() .build() private val pairingRequest: AssociationRequest = AssociationRequest.Builder() .addDeviceFilter(classicBluetoothDeviceFilter) .addDeviceFilter(leBluetoothDeviceFilter) .setSingleDevice(false) .build() private val deviceManager: CompanionDeviceManager by lazy { getSystemService(COMPANION_DEVICE_SERVICE) as CompanionDeviceManager } val mBluetoothAdapter: BluetoothAdapter by lazy { val java = BluetoothManager::class.java getSystemService(java)!!.adapter } val executor: Executor = Executor { it.run() } private val chooserLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> if (result.resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { val deviceToPair: BluetoothDevice? = result.data?.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE) val isConnected = deviceToPair?.createBond() Toast.makeText(this, "Connected: $isConnected", Toast.LENGTH_SHORT).show() } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) { val deviceToPair: BluetoothDevice? = result.data?.getParcelableExtra( CompanionDeviceManager.EXTRA_DEVICE, BluetoothDevice::class.java ) val isConnected = deviceToPair?.createBond() Toast.makeText(this, "Connected: $isConnected", Toast.LENGTH_SHORT).show() } } else { Log.i("Hereeee2", "User cancelled device selection") } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } checkAndRequestBluetoothPermissions() findViewById<Button>(R.id.connect).setOnClickListener { if(isBluetoothEnabled && isLocationServiceEnabled()){ findDevices() // 此处调用了findDevices,但该函数的实现未贴全 } else { Toast.makeText(this, "Please grant bluetooth permissions", Toast.LENGTH_SHORT).show() } } findViewById<Button>(R.id.send).setOnClickListener { if(isBluetoothEnabled && isLocationServiceEnabled()){ isClient = true val pairedDevices = mBluetoothAdapter.bondedDevices val device = pairedDevices.fi // 代码此处截断 } } } }
我已经做的操作
- 确认两台设备都开启了蓝牙
- 开启了定位服务
- 已经申请了
BLUETOOTH_CONNECT和BLUETOOTH_SCAN权限
自己琢磨的可能问题点(不确定对不对)
- 设备过滤器太宽泛:我设置的
classicBluetoothDeviceFilter和leBluetoothDeviceFilter都是空的,没有任何过滤条件,是不是Companion Device Manager不知道该找哪种设备? - Android 16的权限变化:Android 16会不会需要额外的蓝牙相关权限?比如定位权限?
- 设备可见性:是不是另一台设备没有设置为可发现状态?
findDevices()函数的实现:我这个函数的代码没贴全,是不是调用CompanionDeviceManager.associate()的时候有问题?
希望大家能给的建议
- 针对我的代码,哪些地方需要修改才能让设备被检测到?
- Android 15/16适配Companion Device Pairing需要注意哪些特殊点?
- 有没有其他排查方法可以确定问题出在哪?
提前谢谢大家了🙏




