Android Studio: 列出配对设备
创始人
2024-08-17 02:00:20
0

要在Android Studio中列出配对设备,可以使用以下代码示例:

  1. 在AndroidManifest.xml文件中添加以下权限:


  1. 在需要列出配对设备的Activity中,添加以下代码:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter bluetoothAdapter;
    private ArrayAdapter pairedDevicesAdapter;
    private ArrayList pairedDevicesList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化BluetoothAdapter
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // 初始化配对设备列表
        pairedDevicesList = new ArrayList<>();
        pairedDevicesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pairedDevicesList);

        ListView pairedDevicesListView = findViewById(R.id.paired_devices_listview);
        pairedDevicesListView.setAdapter(pairedDevicesAdapter);

        // 检查设备是否支持蓝牙
        if (bluetoothAdapter == null) {
            // 设备不支持蓝牙
            // 可以在这里做相应的处理
            return;
        }

        // 检查蓝牙是否已启用
        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        } else {
            // 蓝牙已启用,列出配对设备
            listPairedDevices();
        }
    }

    private void listPairedDevices() {
        // 获取已配对设备列表
        Set pairedDevices = bluetoothAdapter.getBondedDevices();

        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                pairedDevicesList.add(device.getName() + "\n" + device.getAddress());
            }
        }

        pairedDevicesAdapter.notifyDataSetChanged();
    }
}

这段代码将在布局中的ListView中显示已配对设备的名称和地址。确保在layout文件中有一个ListView元素,并将其id设置为paired_devices_listview

注意:在使用蓝牙功能之前,请确保在AndroidManifest.xml文件中添加了相应的权限。

相关内容

热门资讯

一起来探讨!四川家园辅助器,德... 一起来探讨!四川家园辅助器,德州私人局怎么透视,2025教程(有挂工具);1、完成四川家园辅助器的残...
最新研发!拼十辅助器,竞技联盟... 最新研发!拼十辅助器,竞技联盟辅助(都是存在有挂)小薇(辅助器软件下载)致您一封信;亲爱竞技联盟辅助...
我来教教大家!陕麻圈延安划水辅... 我来教教大家!陕麻圈延安划水辅助,wepoker插件功能辅助器,扑克教程(有挂实锤)是一款可以让一直...
玩家必看科普!互游辅助,丽水都... 玩家必看科普!互游辅助,丽水都莱辅助软件(好像有挂)1、丽水都莱辅助软件透视辅助软件激活码多个强度级...
分享个大家!小程序牵手跑得有辅... 分享个大家!小程序牵手跑得有辅助器,微乐四川麻将辅助器(果然是真的有挂)1、超多福利:超高返利,海量...
必看攻略!家乡大贰小程序靠谱吗... 必看攻略!家乡大贰小程序靠谱吗,wpk俱乐部怎么作弊,玩家教你(有挂教程)是一款可以让一直输的玩家,...
分享认知!牵手辅助神器下载,微... 分享认知!牵手辅助神器下载,微乐降序自建房辅助app(都是真的是有挂)暗藏猫腻,小编详细说明微乐降序...
2024教程!丽水都莱大菠萝脚... 2024教程!丽水都莱大菠萝脚本辅助,wepoker破解是真的还是假的,新2025教程(揭秘有挂);...
一分钟秒懂!798大菠萝辅助,... 一分钟秒懂!798大菠萝辅助,茶馆游戏辅助(总是是真的有挂)1、金币登录送、破产送、升级送、活动送。...
科技分享!边锋干瞪眼ios辅助... 【福星临门,好运相随】;科技分享!边锋干瞪眼ios辅助微信,佛手大菠萝辅助,必赢方法(新版有挂);暗...