AndroidStudio中无法发现可用的蓝牙设备错误。
创始人
2024-08-21 07:30:17
0
  1. 在AndroidManifest.xml中添加以下权限:

  1. 检查是否已经在AndroidManifest.xml中添加了上述权限。

  2. 在MainActivity.java文件中添加以下代码,以检查蓝牙是否已被启用:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // 设备不支持蓝牙 Log.e(TAG, "Device does not support Bluetooth"); } else { if (!bluetoothAdapter.isEnabled()) { // 蓝牙未启用,提示用户启用蓝牙 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { // 蓝牙已启用,可以扫描设备 } }

  1. 进行蓝牙设备搜索:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.startDiscovery();

  1. 将以下代码添加到MainActivity.java文件中,以接收已被发现的设备:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // 当发现设备时 if (BluetoothDevice.ACTION_FOUND.equals(action)) { // 从Intent中获取BluetoothDevice对象 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 将设备名称和地址存储到数组中 mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } };

  1. 在MainActivity.java文件中,将以下代码添加到onCreate()方法的最后一行,以注册广播接收器:

// 注册广播接收器以查找在扫描期间发现的设备 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter);

  1. 最后,在MainActivity.java文件中,

相关内容

热门资讯

透视攻略!hhpoker有辅助... 透视攻略!hhpoker有辅助吗,hhpoker破解工具(透视)一直存在有脚本工具(哔哩哔哩)1、每...
透视举措!竞技联盟破解版最新版... 透视举措!竞技联盟破解版最新版,hhpoker是正品吗(透视)确实有挂(哔哩哔哩)1)竞技联盟破解版...
透视要领!pokemmo免费脚... 透视要领!pokemmo免费脚本,wepoker辅助器安装包定制(透视)原来是真的挂(哔哩哔哩)1、...
透视妙招!德州透视hhpoke... 透视妙招!德州透视hhpoker,hhpoker辅助器(透视)切实存在有脚本攻略(哔哩哔哩)进入游戏...
透视绝活儿!we poker辅... 透视绝活儿!we poker辅助器下载,德州机器人代打脚本(透视)一贯真的是有挂(哔哩哔哩)1、德州...
透视项目!有哪些免费的wpk作... 透视项目!有哪些免费的wpk作必弊码,wejoker手机版透视脚本(透视)总是真的有挂(哔哩哔哩)一...
透视模板!wpk透视插件,红龙... 透视模板!wpk透视插件,红龙poker作必弊指令(透视)总是是真的挂(哔哩哔哩)1、金币登录送、破...
透视指引!哈糖大菠萝破解器,h... 透视指引!哈糖大菠萝破解器,hhpoker作必弊实战视频(透视)好像是有挂(哔哩哔哩)1)hhpok...
透视学习!hhpoker德州有... 透视学习!hhpoker德州有挂吗,wepoker辅助软件价格(透视)切实真的是有挂(哔哩哔哩)1、...
透视指南!aapoker脚本怎... 透视指南!aapoker脚本怎么用,pokeplus脚本(透视)本来真的是有挂(哔哩哔哩)1)aap...