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

相关内容

热门资讯

透视教程"werpl... 透视教程"werplan有挂吗"详细辅助wpk教程(好像是有挂)是由北京得werplan有挂吗黑科技...
一分钟带你了解!xpoker辅... 一分钟带你了解!xpoker辅助助手,wepoker私人局辅助,技巧教程(有挂辅助);支持2-10人...
透视教程"约局吧如何... 透视教程"约局吧如何查看是否有挂"详细辅助新版2025教程(原生存在有挂)1、点击下载安装,微扑克w...
实测揭晓!uupoker有透视... 1、实测揭晓!uupoker有透视吗,哈糖大菠萝万能挂,插件教程(有挂方法);详细教程。2、哈糖大菠...
透视规律"hhpok... 透视规律"hhpoker有没有作弊挂"详细辅助AA德州教程(确实真的有挂);hhpoker有没有作弊...
盘点一款!模拟器打开hhpok... 盘点一款!模拟器打开hhpoker,wpk官网下载链接,玩家教你(有挂神器);实战中需综合运用上述技...
透视辅助"aapok... 透视辅助"aapoker怎么开辅助器"详细辅助分享教程(确实存在有挂)这是由厦门游乐互动科技有限公司...
玩家必知教程!sohoo开挂辅... 玩家必知教程!sohoo开挂辅助,拱趴大菠萝机器人,必赢方法(有挂软件);1.拱趴大菠萝机器人 ai...
透视计算"hhpok... 透视计算"hhpoker哪个俱乐部靠谱"详细辅助AI教程(最初存在有挂),支持语音通讯、好友开房及战...
重大科普!佛手在线大菠萝智能辅... 重大科普!佛手在线大菠萝智能辅助器,wepoker私人局辅助挂,可靠技巧(有挂技巧)是一款可以让一直...