【问题标题】:Bluetooth scanner not discovering devices蓝牙扫描仪未发现设备
【发布时间】:2020-12-02 10:01:23
【问题描述】:

我正在创建蓝牙扫描仪应用并尝试查找可用的设备进行配对。我有一个蓝牙耳机,我试图在 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))
    }
}

如果我在这里遗漏了什么,有人可以帮助我吗?

【问题讨论】:

  • 您是否已授予位置权限??从设置

标签: android bluetooth bluetoothlescanner


【解决方案1】:

设备上的位置信息是否已关闭? 必须为 Android 10 启用位置信息才能获得扫描结果。

还要记得请求许可

if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
   requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERM);
}

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多