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透视有没有"详细辅助透明挂教程(总是真的是有挂);《WPK辅助透视》‌:支持...
透视玄学"wepok... 1、透视玄学"wepokerplus万能挂"详细辅助介绍教程(本然真的是有挂);详细教程。2、wep...
透视中牌率"wepo... 透视中牌率"wepoker作弊视频"详细辅助德州论坛(一贯有挂)关于wepoker作弊视频机制的,其...
透视脚本"wepok... 透视脚本"wepoker有插件吗"详细辅助解说技巧(最初真的有挂);1分钟了解详细教程(微信 136...
透视软件"hhpok... 透视软件"hhpoker软件靠谱吗"详细辅助可靠教程(固有是真的有挂)准备好在hhpoker软件靠谱...
透视神器"hh po... 透视神器"hh poker辅助器先试用"详细辅助新2025版(果然存在有挂)科技教程也叫必备教程,这...
透视软件"哈糖大菠萝... 透视软件"哈糖大菠萝有没有挂"详细辅助黑科技教程(原先是真的有挂)是一款可以让一直输的玩家,快速成为...
透视透视挂"约局吧怎... 透视透视挂"约局吧怎么看有没有挂"详细辅助2025新版技巧(本然有挂);最新版2024是一款经典耐玩...
透视脚本"约局吧德州... 透视脚本"约局吧德州有挂吗"详细辅助详细教程(好像是有挂)是一款可以让一直输的玩家,快速成为一个“必...
透视苹果版"wepo... 透视苹果版"wepoker怎么买辅助"详细辅助攻略方法(素来是有挂);玩家必备必赢加哟《136704...