简体   繁体   English

Android 蓝牙设备 Kotlin

[英]Android Bluetooth devices in Kotlin

pairedDevices=mBluetoothAdapter?.bondedDevices
for (d in listOfNotNull(pairedDevices)) {
    blueArray?.add(d._____)
}

blueArray is an ArrayList in which i want to store the string name of available bluetooth devices but - d.getName() is not working blueArray是一个ArrayList ,我想在其中存储可用蓝牙设备的字符串名称,但是 - d.getName()不工作

What can i do?我能做什么?

listOfNotNull() in this case will simply create a list with the sole element of what getBondedDevices() returns. 在这种情况下, listOfNotNull()将仅使用getBondedDevices()返回的唯一元素创建一个列表。 You're creating a List<Set<BluetoothDevice>> and trying to call getName() on the Set . 您正在创建List<Set<BluetoothDevice>>并尝试在Set上调用getName()

Might want to do something like this instead: 可能想做这样的事情:

for (device in mBluetoothAdapter.bondedDevices) {
    blueArray.add(device.name)
}

You should declare pairedDevice as set of BluetoothDevice then iterate through the set and add to your array您应该将 pairedDevice 声明为 BluetoothDevice 的集合,然后遍历该集合并添加到您的数组中

val pairedDevice: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
        if (pairedDevice != null){
            var listDeviceName = arrayListOf<String>()
            try {
                pairedDevice.forEachIndexed { index, device ->
                    listDeviceName.add(index, device.name)
                }catch (e:IndexOutOfBoundsException){
                Log.e("TAG", "indexOutOfBond",e)
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM