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

相关内容

热门资讯

重大来袭!wpk私人局有透视吗... 很多人使用云端 AI 时,都担心wpk私人局有透视吗、内部资料上传带来隐私风险。这款本地运行的 AI...
我来分享!wpk真的有透视嘛,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
必赢方法!wpk辅助是什么,微... wpk辅助是什么AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,...
实测必看!wepoker私人局... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟带你了解!wepoker... 一分钟带你了解!wepoker开脚本视频,德扑之星开挂透视教程神器(从来真的有透视挂)主打本地运行的...
推荐攻略!微扑克辅助下载,德普... 推荐攻略!微扑克辅助下载,德普之星确实有挂安装(最初存在有辅助器)AI 是一款可以让一直输的玩家修复...
热门推荐!we-poker辅助... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
一分钟带你了解!WPK脚本,w... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
揭秘攻略!wpk软件是正规的吗... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
今日重大通报!wepoker透... 今日重大通报!wepoker透视脚本安卓,微扑克透视软件工具(往昔真的是有开挂)AI 是一款可以让一...