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... 辅助透视"wepokerh5破解"详细辅助必赢教程(辅助工具技巧);人气非常高,ai更新快且高清可以...
透视中牌率"htx矩... 透视中牌率"htx矩阵wepoker辅助"详细辅助透视教程(私人局透视技巧)是一款可以让一直输的玩家...
透视能赢"wepok... 透视能赢"wepoker科技辅助器"详细辅助AA德州教程(辅助器透明);玩家必备必赢加哟《13670...
透视插件"wejok... 透视插件"wejoker辅助软件"详细辅助教你攻略(游戏下载透视)1、很好的工具软件,可以解锁游戏的...
透视智能ai"wep... 这是一款非常优秀的wepoker怎么看底牌 ia辅助检测软件,能够让你了解到wepoker怎么看底牌...
透视最新"hhpok... 透视最新"hhpoker透视工具"详细辅助透明教程(插件功能辅助器方法);小薇(透视辅助)致您一封信...
透视安装"wepok... 1、透视安装"wepoker有辅助功能吗"详细辅助AA德州教程(高级辅助透明)。2、wepoker有...
透视软件"wepok... 透视软件"wepoker手机版辅助"详细辅助AI教程(可以增加运气辅助);支持2-10人实时对战,虚...
透视存在"黑侠破解w... 1、透视存在"黑侠破解wepoker"详细辅助可靠教程(辅助器v3.3教程)2、进入游戏-大厅左侧-...
透视有挂"wepok... 透视有挂"wepoker辅助器安装包"详细辅助可靠教程(辅助器下载神器)关于wepoker辅助器安装...