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

相关内容

热门资讯

昨日!newpoker脚本,微... 昨日!newpoker脚本,微信小程序微乐辅助器教程图片,关于脚本(有挂规律)-哔哩哔哩1、玩家可以...
脚本了解!aapoker免费透... 脚本了解!aapoker免费透视脚本,aapoker辅助工具安全吗(透视)都是真的是有神器(哔哩哔哩...
透视讲解!hhpoker真的假... 透视讲解!hhpoker真的假的,微信微乐小程序有窍门吗,总结透视(发现有挂)-哔哩哔哩1、任何hh...
透视揭露!hhpoker德州挂... 透视揭露!hhpoker德州挂真的有吗,hhpoker辅助软件下载(透视)其实是真的器(哔哩哔哩)脚...
透视挂!wepoker怎么发冤... 透视挂!wepoker怎么发冤家牌,微乐兰州麻将小程序辅助,推荐脚本(有挂技巧)-哔哩哔哩1、wep...
脚本分享!hhpoker免费透... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
今天下午!wepoker手机版... 今天下午!wepoker手机版透视脚本,手机微乐小程序破解器如何使用,关于脚本(有挂分析)-哔哩哔哩...
脚本详细!德普之星透视辅助插件... 脚本详细!德普之星透视辅助插件,德普辅助器怎么用(透视)切实是真的工具(哔哩哔哩)1、免费脚本咨询教...
最终!竞技联盟透视插件,微乐辅... 最终!竞技联盟透视插件,微乐辅助脚本,开挂透视(真是有挂)-哔哩哔哩1、下载好竞技联盟透视插件脚本下...
脚本揭露!wpk透视辅助下载,... 脚本揭露!wpk透视辅助下载,wpk作必弊(透视)确实存在有插件(哔哩哔哩)1、任何透视是真的假的的...