Android/Kotlin: 显示已配对蓝牙设备的列表
创始人
2024-08-20 07:00:40
0

以下是一个示例代码,用于显示已配对蓝牙设备的列表。

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var listView: ListView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        listView = findViewById(R.id.listView)
        showPairedDevices()
    }

    private fun showPairedDevices() {
        val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
        val pairedDevices: Set? = bluetoothAdapter?.bondedDevices

        val deviceList = ArrayList()

        if (pairedDevices != null) {
            for (device: BluetoothDevice in pairedDevices) {
                deviceList.add(device.name)
            }
        }

        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, deviceList)
        listView.adapter = adapter
    }
}

在此示例中,我们首先获取默认的蓝牙适配器,并使用bondedDevices方法获取已配对的设备列表。然后,我们将设备名称添加到一个字符串列表中,并使用ArrayAdapter将列表显示在ListView中。

相关内容

热门资讯

透视游戏!hh poker软件... 透视游戏!hh poker软件(透视)果然真的有辅助app(有挂方式)-哔哩哔哩1、hh poker...
此事引发广泛关注!fishpo... 此事引发广泛关注!fishpoker透视(透视)其实真的是有辅助安装(存在有挂)-哔哩哔哩1、许多玩...
透视有挂!pokemmo辅助工... 透视有挂!pokemmo辅助工具(透视)原来存在有辅助插件(真的有挂)-哔哩哔哩1.pokemmo辅...
透视肯定!智星德州插件2024... 透视肯定!智星德州插件2024最新版(透视)切实有辅助插件(有挂功能)-哔哩哔哩智星德州插件2024...
有消息称!we-poker是什... 有消息称!we-poker是什么软件(透视)本来真的有辅助软件(有挂教程)-哔哩哔哩1、打开软件启动...
据通报!佛手在线大菠萝智能辅助... 据通报!佛手在线大菠萝智能辅助器(透视)确实是真的辅助安装(有挂总结)-哔哩哔哩1、操作简单,无需佛...
透视规律!wepoker辅助器... 透视规律!wepoker辅助器(透视)果然存在有辅助神器(有挂细节)-哔哩哔哩1、操作简单,无需we...
无独有偶!poker worl... 无独有偶!poker world辅助器(透视)竟然有辅助app(有挂实锤)-哔哩哔哩1、poker ...
透视透视!htx矩阵wepok... 透视透视!htx矩阵wepoker辅助(透视)果然存在有辅助平台(有挂细节)-哔哩哔哩1、进入游戏-...
截至发稿!wejoker辅助脚... 截至发稿!wejoker辅助脚本(透视)确实存在有辅助app(真的有挂)-哔哩哔哩1)wejoker...