android - Android 蓝牙 - 为蓝牙发现的设备的 UUID 获取 NULL
问题描述
我想在不配对的情况下在两个蓝牙设备之间共享数据,并且在两部手机中都将安装我的应用程序,所以我使用了这种方法(https://arxiv.org/ftp/arxiv/papers/1507/1507.00650.pdf)蓝牙设备使用 UUID 编号发布服务列表。
这是如何注册服务=====>
String uuid = "f2989d52-b3d6-4205-b34d-04f35ea264e5";
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket btSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("MYAPP", UUID.fromString(uuid))
回调的意图===>
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter1);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_UUID);
registerReceiver(mReceiver, filter2);
之后我打电话
bluetoothAdapter.startDiscovery(); // to start discovery of devices
处理意图的代码==>
private final BroadcastReceiver mReceiver = new
BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.e("SUNIL","receiver");
String action = intent.getAction();
Log.e("SUNIL","receiver action"+action);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Boolean uuid1 = device.fetchUuidsWithSdp();
mDeviceList.add(device);
Log.e("SUNIL","device uuid result"+uuid1);
Log.e("SUNIL","device"+device.getAddress()+device.getUuids()+device.getBondState());
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// discovery has finished, give a call to fetchUuidsWithSdp on first device in list.
if (!mDeviceList.isEmpty()) {
BluetoothDevice device = mDeviceList.remove(0);
boolean result = device.fetchUuidsWithSdp();
Log.e("SUNIL", "device uuid result223" + result+"==uuid finished"+device.getUuids());
}
}
else if (BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e("SUNIL","device.getUuids()"+device.getUuids());
Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
if(uuids != null) {
for (Parcelable ep : uuids) {
if (uuids != null) {
String uuid = ep.toString();
Log.e("SUNIL", "uuid===>" + uuid+"===="+device.getName());
**getting uuid null here**
}
}
}
}
}
};
请帮助我,我是这个蓝牙 API 的新手, 我已经用 redmi 6A 和 redmi 6 PRO 设备验证了
解决方案
推荐阅读
- twilio - 使用 Twilio Autopilot 发送电子邮件
- highcharts - 如何让 Highcharts Tilemap 填满整个空间?
- python - Python:将新字典附加到 json 文件
- coldfusion - 寻找有关如何使用 cfc 处理 Coldfusion 会话超时的建议
- variables - FOP 到 PDF - 在 FOP 标签(URL 标签)中使用变量
- arrays - 在不使用临时数组且不声明可分配变量的情况下从数组中删除 1 个元素是否是一种好方法?
- javascript - 我需要用javascript打印1-100,这样每个数字都有一个等级(100-A、99-A等)
- node.js - 如何检查我的 RESTful API 是否连接到 Firebase 实时数据库
- go - 一旦通道中有新值,就杀死一个 goroutine
- c# - 是否有页面布局用户控件,我可以在其中添加自己的控件?