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文件中添加了相应的权限。

相关内容

热门资讯

7分钟了解!微信小程序功夫川免... 7分钟了解!微信小程序功夫川免费辅助!一直一直都是有辅助脚本(有挂细节)-哔哩哔哩1、游戏颠覆性的策...
一分钟了解!潮娱乐鱼虾蟹公式辅... 一分钟了解!潮娱乐鱼虾蟹公式辅助软件!真是是有辅助技巧(有挂方略)-哔哩哔哩1、让任何用户在无需潮娱...
第四分钟了解!欢乐卡五星插件!... 第四分钟了解!欢乐卡五星插件!果然是有辅助技巧(有挂解惑)-哔哩哔哩1、下载好欢乐卡五星插件透视辅助...
九分钟了解!哈糖大菠萝万能挂!... 九分钟了解!哈糖大菠萝万能挂!一贯真的是有辅助神器(有挂教学)-哔哩哔哩哈糖大菠萝万能挂透视方法中分...
第8分钟了解!蜀山四川辅助脚本... 第8分钟了解!蜀山四川辅助脚本多少钱!本来是真的有辅助方法(果真有挂)-哔哩哔哩1、起透看视 蜀山四...
第6分钟了解!万能微信小游戏辅... 第6分钟了解!万能微信小游戏辅助器!确实真的有辅助app(有挂技术)-哔哩哔哩1、任何万能微信小游戏...
1分钟了解!潮汕来物局开挂方法... 1分钟了解!潮汕来物局开挂方法!其实存在有辅助神器(果真有挂)-哔哩哔哩1、每一步都需要思考,不同水...
第1分钟了解!佛手十三道破解版... 第1分钟了解!佛手十三道破解版安装!确实一直都是有辅助方法(有挂工具)-哔哩哔哩1、许多玩家不知道佛...
1分钟了解!赣南辅助软件哪个好... 1分钟了解!赣南辅助软件哪个好!果然是有辅助app(有挂猫腻)-哔哩哔哩在进入赣南辅助软件哪个好软件...
九分钟了解!欢聚水鱼只能辅助教... 九分钟了解!欢聚水鱼只能辅助教程!确实一直都是有辅助神器(有挂详细)-哔哩哔哩欢聚水鱼只能辅助教程能...