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

相关内容

热门资讯

第四分钟解密!填大坑小程序辅助... 第四分钟解密!填大坑小程序辅助器,新九哥开挂(都是真的是有挂)-哔哩哔哩1、完成填大坑小程序辅助器透...
两分钟详细!微信小程序多乐辅助... 两分钟详细!微信小程序多乐辅助器,新悠悠辅助(果然是有挂)-哔哩哔哩1、微信小程序多乐辅助器系统规律...
七分钟外挂!宝宝浙江游戏开挂器... 七分钟外挂!宝宝浙江游戏开挂器,潮汕汇辅助神器(真是是真的挂)-哔哩哔哩1、宝宝浙江游戏开挂器系统规...
第5分钟外挂!南通长牌辅助,猎... 第5分钟外挂!南通长牌辅助,猎鱼达人破解版无限弹头(好像真的是有挂)-哔哩哔哩1、猎鱼达人破解版无限...
六分钟关于!创思维激k看底牌辅... 六分钟关于!创思维激k看底牌辅助,微友三代辅助(确实真的是有挂)-哔哩哔哩1、全新机制【创思维激k看...
第7分钟总结!今日长牌破解,中... 第7分钟总结!今日长牌破解,中至赣州冲关小程序辅助(其实存在有挂)-哔哩哔哩1、任何中至赣州冲关小程...
两分钟解密!阿拉游戏免费辅助器... 两分钟解密!阿拉游戏免费辅助器,水鱼辅助软件下载(原来有挂)-哔哩哔哩1)阿拉游戏免费辅助器辅助挂:...
第四分钟揭幕!微乐家乡麻辣自建... 第四分钟揭幕!微乐家乡麻辣自建房,闲聚app辅助软件(确实存在有挂)-哔哩哔哩运微乐家乡麻辣自建房辅...
7分钟解谜!闲逸平台辅助功能入... 7分钟解谜!闲逸平台辅助功能入口在哪里,心悦填大坑辅助(一直是有挂)-哔哩哔哩闲逸平台辅助功能入口在...
第十分钟详情!宝宝浙江游戏有挂... 第十分钟详情!宝宝浙江游戏有挂吗,微信小程序游戏破解器(总是真的有挂)-哔哩哔哩1、操作简单,无需注...