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

相关内容

热门资讯

记者揭秘!创思维激k透视挂,雀... 记者揭秘!创思维激k透视挂,雀友会辅助潮汕麻将脚本,详细工具(有挂方法)1、首先打开雀友会辅助潮汕麻...
黑科技技巧!wepoker有脚... 黑科技技巧!wepoker有脚本吗,红龙poker辅助,果然是真的有挂(有挂方式)一、红龙poker...
一分钟快速了解!微信小程序微乐... 一分钟快速了解!微信小程序微乐辅助器教程,微乐家乡官方app下载,详细工具(有挂总结)1)微信小程序...
黑科技辅助挂!wepoker私... 黑科技辅助挂!wepoker私局辅助,德普之星有透视辅助吗,都是真的是有挂(有挂讲解)1、完成德普之...
一分钟快速了解!财神十三张脚本... 一分钟快速了解!财神十三张脚本辅助,小松宿松游戏辅助,详细教程(有挂详情)所有人都在同一条线上,像星...
软件辅助挂!wejoker辅助... 软件辅助挂!wejoker辅助软件,德普之星辅助软件,都是真的有挂(有挂技巧)1、每一步都需要思考,...
必备攻略!微乐陕西小程序破解器... 必备攻略!微乐陕西小程序破解器下载,微信小程序游戏破解微乐游戏,详细教程(有挂分析)1、该软件可以轻...
突发!哈糖大菠萝软件下载,德普... 突发!哈糖大菠萝软件下载,德普之星辅助器,切实存在有挂(有挂详细)1、操作简单,无需德普之星辅助器手...
科普攻略!超凡辅助软件,518... 科普攻略!超凡辅助软件,518互游辅助器下载,详细挂(了解有挂)1、这是跨平台的518互游辅助器下载...
无独有偶!wepoker怎么拿... 无独有偶!wepoker怎么拿到好牌,德普之星透视,本来是真的有挂(有挂分享)1、德普之星透视模拟器...