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、玩家必看科普!wep...
玩家必看分享!竞技联盟破解版最... 玩家必看分享!竞技联盟破解版最新版(辅助挂)切实真的有挂(真实有挂)1、超多福利:超高返利,海量正版...
必备攻略!wepoker透视脚... 必备攻略!wepoker透视脚本网页(辅助挂)确实是有挂(有挂猫腻)1、游戏颠覆性的策略玩法,独创攻...
玩家必备教程!德普辅助软件(辅... 玩家必备教程!德普辅助软件(辅助挂)竟然是真的有挂(有挂攻略)1、每一步都需要思考,不同水平的挑战会...
揭秘一下!wepoker辅助器... 揭秘一下!wepoker辅助器最新版本更新内容(辅助挂)一贯是有挂(揭秘有挂)1、wepoker辅助...
热点推荐!德州局怎么透视(辅助... 热点推荐!德州局怎么透视(辅助挂)切实是真的有挂(有挂秘笈)1、点击下载安装,德州局怎么透视脚本分类...
玩家科普!pokemmo内置修... 玩家科普!pokemmo内置修改器(辅助挂)一贯存在有挂(有人有挂)1、下载好pokemmo内置修改...
最新研发!约局吧能不能开挂(辅... 最新研发!约局吧能不能开挂(辅助挂)切实有挂(有挂规律)1、下载好约局吧能不能开挂脚本下载之后点击打...