蓝牙扫描仪未发现设备 [英] Bluetooth scanner not discovering devices
本文介绍了蓝牙扫描仪未发现设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个蓝牙扫描仪应用程序,并试图找到可用的设备进行配对.我有一个蓝牙耳机,试图在Android 10上运行该应用程序.
I am creating a Bluetooth scanner app and trying to find the available devices to pair. I have a Bluetooth headset which I am trying to find running the application on 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"/>
应用程序包含一个简单的按钮,单击该按钮我就开始发现蓝牙设备
App contains a simple button on whose click I start discovery for bluetooth device
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))
}
}
如果我在这里想念东西,有人可以帮我吗?
Can someone help me out if I am missing something here?
推荐答案
设备上的位置是否关闭?必须为Android 10启用位置才能获取扫描结果.
Is location on the device turned off? Location has to be enabled for Android 10 to get scan results.
也请记住也要寻求许可
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERM);
}
这篇关于蓝牙扫描仪未发现设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文