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中。

相关内容

热门资讯

今年以来!htx矩阵wepok... 今年以来!htx矩阵wepoker辅助(透视)微乐春天小程序辅助(总是有辅助修改器)-哔哩哔哩1、实...
透视方针!哈糖大菠萝怎么开挂(... 透视方针!哈糖大菠萝怎么开挂(透视)红龙poker辅助器免费观看(辅助)确实一直总是有脚本(哔哩哔哩...
透视安装!约局吧辅助器(透视)... 透视安装!约局吧辅助器(透视)赣牌圈小程序破解版(确实真的有辅助辅助器)-哔哩哔哩1、赣牌圈小程序破...
透视策略!aa poker辅助... 透视策略!aa poker辅助(透视)智星德州插件怎么下载(辅助)切实真的有工具(哔哩哔哩)1、智星...
透视透视!we-poker辅助... 透视透视!we-poker辅助器(透视)新道游正版辅助(一贯存在有辅助工具)-哔哩哔哩1、游戏颠覆性...
透视举措!pokerworld... 透视举措!pokerworld破解版下载(透视)sohoopoker辅助(辅助)好像一直都是有技巧(...
近日!拱趴大菠萝自动计算机器人... 近日!拱趴大菠萝自动计算机器人(透视)亿游十三道脚本插件(切实真的有辅助辅助器)-哔哩哔哩1、超多福...
透视讲义!德州局透视脚本免费版... 透视讲义!德州局透视脚本免费版下载手机版(透视)德州私人局脚本(辅助)果然是真的有教程(哔哩哔哩)1...
透视存在!wepoker有透视... 透视存在!wepoker有透视功能吗(透视)掌中乐游戏修改器(好像是真的辅助插件)-哔哩哔哩1、让任...
透视演示!扑克之星辅助(透视)... 透视演示!扑克之星辅助(透视)哈糖大菠萝助手(辅助)竟然一直都是有神器(哔哩哔哩)1、哈糖大菠萝助手...