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、在雀神麻雀科技公司插件功能辅助器技巧...
如何分辨真伪!圣游科技,四川游... 如何分辨真伪!圣游科技,四川游戏家园通用辅助在,详细插件(有挂规律)四川游戏家园通用辅助在脚本下载中...
一分钟教你!德扑之星插件,途游... 一分钟教你!德扑之星插件,途游辅助器,详细器(讲解有挂)1、德扑之星插件模拟器是什么优化,德扑之星插...
必备教程!wepoker俱乐部... 必备教程!wepoker俱乐部开挂,四川游戏家园辅助软件,详细软件(讲解有挂)1、该软件可以轻松地帮...
我来教大家!胡乐辅助脚本是真的... 我来教大家!胡乐辅助脚本是真的假的,科乐游戏辅助脚本视频,详细脚本(有挂方略);1、操作简单,无需胡...
今日公布!青橙竞技游戏辅助,纳... 今日公布!青橙竞技游戏辅助,纳祥游戏脚本,详细工具(有挂方略)1、玩家可以在青橙竞技游戏辅助线上大神...
技巧知识分享!朋友局辅助器免费... 技巧知识分享!朋友局辅助器免费版,小程序牵手跑得有辅助器,详细工具(有挂规律);1、金币登录送、破产...