我正在创建蓝牙扫描仪应用程序并尝试查找可用的设备进行配对。我有一个蓝牙耳机,我试图在 android 10 上运行该应用程序。
权限在清单中设置
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
应用程序包含一个简单的按钮,单击该按钮我开始发现蓝牙设备
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
val bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().bluetoothLeScanner
scanBluettoth.setOnClickListener({
bluetoothLeScanner.startScan(leScanCallback)
})
回调发现
val leScanCallback: ScanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
super.onScanResult(callbackType, result)
Log.e("device ", "D ".plus(result.device.name))
}
}
如果我在这里遗漏了什么,有人可以帮助我吗?