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

相关内容

热门资讯

透视好牌"wepok... 透视好牌"wepoker辅助透视软件"详细辅助扑克教程(代打辅助机器人神器)是一款可以让一直输的玩家...
透视模拟器"WePO... 透视模拟器"WePOker有没有透视方法"详细辅助2025新版教程(辅助器安装包定制方法)是一款可以...
透视教学"hhpok... 1、透视教学"hhpoker免费辅助器"详细辅助技巧教程(智能辅助插件技巧)。2、hhpoker免费...
透视有挂"we-po... 透视有挂"we-poker靠谱吗"详细辅助技巧教程(辅助机器人透明);we-poker靠谱吗软件透明...
透视有挂"wejok... 透视有挂"wejoker辅助软件价格"详细辅助揭秘教程(游戏安装教程透视);支持2-10人实时对战,...
透视好友房"wepo... 透视好友房"wepoker私人局辅助挂"详细辅助黑科技教程(可以免费玩神器);致您一封信;亲爱wep...
透视挂"约局吧如何查... 透视挂"约局吧如何查看是否有挂"详细辅助教你攻略(有辅助器透明);科技安装教程;136704302。...
透视规律"约局吧德州... 透视规律"约局吧德州透视"详细辅助解说技巧(游戏下载方法);小薇(透视辅助)致您一封信;亲爱约局吧德...
透视讲解"德州私人局... 透视讲解"德州私人局怎么透视"详细辅助技巧教程(好友房开挂教程);支持多人共享记分板与复盘,通过邀请...
透视挂透视"soho... 透视挂透视"sohoo开挂辅助"详细辅助曝光教程(透视脚本免费技巧);原来确实真的有挂(需添加指定薇...