小编use*_*321的帖子

列出BLE设备Android-Kotlin

我正在尝试使用Kotlin列出Android设备上的所有BLE设备(Java版本也不起作用),但是我没有得到任何设备或任何回电,除了“扫描已开始” “清单上我有正确的使用许可。

这是我正在尝试的当前最少代码。但是,即使来自Google的示例代码也列出了所有设备。我在Android版本8.1.0的Pixel上运行。我可以在iOS上使用基本的BLE设备列表(Swift)运行它!

 private val bleScanner = object :ScanCallback() {
    override fun onScanResult(callbackType: Int, result: ScanResult?) {
        super.onScanResult(callbackType, result)
        Log.d("DeviceListActivity","onScanResult: ${result?.device?.address} - ${result?.device?.name}")
    }

    override fun onBatchScanResults(results: MutableList<ScanResult>?) {
        super.onBatchScanResults(results)
        Log.d("DeviceListActivity","onBatchScanResults:${results.toString()}")
    }

    override fun onScanFailed(errorCode: Int) {
        super.onScanFailed(errorCode)
        Log.d("DeviceListActivity", "onScanFailed: $errorCode")
    }

}

private val bluetoothLeScanner: BluetoothLeScanner
    get() {
        val bluetoothManager = applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        val bluetoothAdapter = bluetoothManager.adapter
        return bluetoothAdapter.bluetoothLeScanner
    }

class ListDevicesAdapter(context: Context?, resource: Int) : ArrayAdapter<String>(context, resource)

override fun onCreate(savedInstanceState: Bundle?) {
    Log.d("DeviceListActivity", "onCreate()")
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_device_list)


} …
Run Code Online (Sandbox Code Playgroud)

android bluetooth kotlin bluetooth-lowenergy

5
推荐指数
1
解决办法
1896
查看次数

标签 统计

android ×1

bluetooth ×1

bluetooth-lowenergy ×1

kotlin ×1