用react native检测信标的uid和名称



我从计算机模拟了一个信标,我正试图从我的react native应用程序中读取信标信息(uid,标识符等)。

我决定使用这个库https://github.com/dotintent/react-native-ble-plx作为库。

我面临的问题是,虽然它显示一些设备,当我扫描,名称,uid和其他信息显示为空。

为例:

'device', { serviceUUIDs: null,
isConnectable: null,
overflowServiceUUIDs: null,
txPowerLevel: null,
serviceData: null,
manufacturerData: 'TEACFCd6h5jcoxKqh9ACQqwTAAOBqZYcxQ==',
name: null,
mtu: 23,
rssi: -47,
solicitedServiceUUIDs: null,
localName: null,
id: '32:BD:32:6C:E9:C2',
这是我的代码
const bluetoothInstance = new BleManager();
const scanAndConnect = () => {
bluetoothInstance.startDeviceScan(null, { allowDuplicates: true }, (error, device) => {
console.log('device', device);
console.log('error', error);
if (error) {
// Handle error (scanning will be stopped automatically)
return;
}
if (device?.name === 'MyProjectName') {
bluetoothInstance.stopDeviceScan();
} else {
// bluetoothInstance.stopDeviceScan();
}
});
};
useEffect(() => {
bluetoothInstance.onStateChange((state) => {
console.log('state', state);
if (state === 'PoweredOn') {
scanAndConnect();
}
}, true);
}, []);

如何读取信标uid和名称?你还有其他图书馆可以推荐吗?还是代码中缺少了什么?如有任何帮助,不胜感激。

扫描代码可能工作正常。您要查找的信息要么不总是存在,要么在不同的字段中存在。

iBeacon Proximity UUID实际上嵌入在manufacturerData字段中。但在iOS设备上,作为一种安全机制,该字段会被操作系统删除。苹果禁止使用CoreBluetooth框架(react-react-native-ble-plx在引擎盖下使用)来检测iBeacon。在iOS上,你必须使用CoreLocation。为此,您可以尝试react-native-beacons-manager。对于Android, react-native-ble-plx可以很好地检测iBeacon,但你必须自己从manyfacturerData字段中解析出beacon字段。

只有在主广告包之前最近收到了包含该名称的扫描响应包时,才填充蓝牙名称(在名称字段中)。目前还不清楚iOS将如何处理被禁止的iBeacon广告,但它可能对非iBeacon广告有效。在安卓系统上,它也能很好地工作。只是不要期望它在任何一个平台上都100%被填充。

最新更新