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

相关内容

热门资讯

外挂课程!约局吧app有挂吗,... 外挂课程!约局吧app有挂吗,拱趴大菠萝机器人-其实一直总是有辅助脚本(哔哩哔哩)1、拱趴大菠萝机器...
外挂技法!佛手在线大菠萝智能辅... 外挂技法!佛手在线大菠萝智能辅助器,拱趴大菠萝万能辅助器-原来真的有辅助方法(哔哩哔哩)暗藏猫腻,小...
外挂绝活!德普之星私人局辅助器... 外挂绝活!德普之星私人局辅助器,拱趴大菠萝挂怎么安装-原来是有辅助app(哔哩哔哩)1、拱趴大菠萝挂...
外挂模块!德州透视插件,佛手大... 外挂模块!德州透视插件,佛手大菠萝13道挂哪里-好像是真的有辅助工具(哔哩哔哩)1、佛手大菠萝13道...
外挂手册!xpoker辅助,德... 外挂手册!xpoker辅助,德普之星的辅助工具介绍-确实真的有辅助软件(哔哩哔哩)1、在xpoker...
外挂妙招!德普之星透视,德普之... 外挂妙招!德普之星透视,德普之星私人局辅助器-总是是有辅助app(哔哩哔哩)所有人都在同一条线上,像...
外挂烘培!哈糖大菠萝能开挂吗,... 外挂烘培!哈糖大菠萝能开挂吗,pokermaster辅助器-本来一直都是有辅助插件(哔哩哔哩)1、起...
外挂手段!德州真人透视脚本,悦... 您好,德州真人透视脚本这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
外挂手册!德州真人透视脚本,p... 外挂手册!德州真人透视脚本,pokermaster破解版-好像存在有辅助技巧(哔哩哔哩)德州真人透视...
外挂资料!红龙poker辅助平... 外挂资料!红龙poker辅助平台,红龙poker有辅助吗-真是真的有辅助教程(哔哩哔哩)1、下载好红...