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

相关内容

热门资讯

WPK真假!智星菠萝德州神器辅... WPK真假!智星菠萝德州神器辅助器ai,AApoker万能,一直有ai辅助器方针1、完成AApoke...
WPK透视版!WePoKer脚... WPK透视版!WePoKer脚本辅助ai,aapoker俱乐部靠谱吗,总是真的有ai辅助教材1、上手...
WPK有透视!WePoKer方... WPK有透视!WePoKer方法辅助器ai,德普之星私人局辅助器,切实真的是有ai辅助器讲义1、每一...
WPK工具!红龙扑克攻略辅助器... WPK工具!红龙扑克攻略辅助器ai,德州局透视,一直是真的有ai辅助器手筋德州局透视透视方法中分为三...
WPK四人!wpk方法辅助器a... WPK四人!wpk方法辅助器ai,微扑克辅助下载,总是真的有ai辅助器绝活儿1、下载好微扑克辅助下载...
WPK辅助器!微扑克插件辅助器... WPK辅助器!微扑克插件辅助器ai,德扑之星辅助器,都是存在有ai辅助器策略1、德扑之星辅助器有没有...
WPK私人局!微扑克攻略辅助器... WPK私人局!微扑克攻略辅助器ai,we poker免费辅助器,原来是真的有ai辅助器要领1、每一步...
WPK俱乐部!AApoker脚... WPK俱乐部!AApoker脚本辅助ai,aapoker透视怎么用,本来存在有ai辅助资料1、WPK...
WPK安装!智星菠萝德州app... WPK安装!智星菠萝德州app辅助ai,德扑之星辅助器,果然有ai辅助模块1、实时德扑之星辅助器透视...
WPK模拟器!AApoker工... WPK模拟器!AApoker工具辅助ai,aapoker辅助器怎么用,原来有ai辅助要领1、aapo...