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

相关内容

热门资讯

科普分享!wepoker买钻石... 科普分享!wepoker买钻石有用吗(辅助挂)其实真的有挂(有挂技巧辅助脚本)1、不需要AI权限,帮...
揭秘!hhpoker有没有作弊... 揭秘!hhpoker有没有作弊辅助(辅助挂)一贯真的是有挂(有挂讲解辅助插件)1、点击下载安装,脚本...
传递经验!pokemmo内置修... 传递经验!pokemmo内置修改器(辅助挂)总是存在有挂(有挂透明挂辅助app);透视方法中分为三种...
每日必看!wepoker买钻石... 每日必看!wepoker买钻石有用吗(辅助挂)原来真的是有挂(有挂辅助辅助脚本)1、该软件可以轻松地...
揭秘几款!德普之星透视辅助插件... 揭秘几款!德普之星透视辅助插件(辅助挂)好像是有挂(有挂技巧辅助app)1、许多玩家不知道辅助怎么退...
每日必备!wepoker怎么破... 每日必备!wepoker怎么破解游戏(辅助挂)确实存在有挂(有人有挂辅助攻略)1、免费脚本咨询教程、...
实操分享!hardrock透视... 实操分享!hardrock透视工具(辅助挂)都是是真的有挂(的确有挂辅助攻略)1、透视辅助软件激活码...
揭秘几款!wepoker免费脚... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
分享开挂内幕!哈糖大菠萝有挂吗... 分享开挂内幕!哈糖大菠萝有挂吗5个常用方法(辅助挂)本来是真的有挂(有挂分享辅助技巧)1、实时透视辅...
2分钟细说!wepokerpl... 2分钟细说!wepokerplus透视脚本免费(辅助挂)真是真的有挂(有挂总结辅助软件)1、实时透视...