我目前正在创建一个移动应用程序,以检测蓝牙Eddystone信标。我能够使用react-native-ble-manager检测到信标。但是,由于我熟悉数据的格式(可能是缓冲区/字节),因此无法解析数据。我主要需要从信标获取名称空间以及TLM(遥测)数据。这是我的蓝牙代码的当前实现以及我使用react-native-ble-manager获得的数据。
我尝试查看Buffer库进行解析,但无法使其正常工作。
我当前蓝牙代码的一部分
// BLE Manager
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
// Bind bluetooth discover method
this.handleDiscoverPeripheral = this.handleDiscoverPeripheral.bind(this);
// Add Listener
NativeAppEventEmitter.addListener('BleManagerDiscoverPeripheral', this.handleDiscoverPeripheral);
BleManager.start({ showAlert: false })
.then(() => {
// Success code
console.log('Module initialized');
});
handleDiscoverPeripheral函数
handleDiscoverPeripheral(peripheral) {
console.log('Got ble peripheral', peripheral.id);
let nearbyArr = this.state.sectionData[0].data
let othersArr = this.state.sectionData[1].data
if (this.state.participantMap[peripheral.id] !== undefined) {
let duplicateCheck = nearbyArr.findIndex(function (item) {
return item.beacon === peripheral.id;
})
// console.log('DUP CHECK: ' + duplicateCheck)
// Check if peripheral has not been added into nearbyArr
if (duplicateCheck === -1) {
console.log('Found matching id: ' + peripheral.id)
let dataBytes = (peripheral.advertising.manufacturerData.bytes)
console.log(dataBytes)
let data = String(peripheral.advertising.manufacturerData.data)
console.log(data)
let participant = this.state.participantMap[peripheral.id]
this.setState(state => {
const sectionData = state.sectionData
const nearbyArr = sectionData[0].data
const othersArr = sectionData[1].data
nearbyArr.push(participant)
othersArr.splice(othersArr.findIndex(function (item) {
return item.beacon === peripheral.id;
}), 1);
return sectionData
})
}
}
}
整个外围设备的控制台日志
{ advertising:
{ txPowerLevel: -2147483648,
serviceUUIDs: [ 'feaa' ],
isConnectable: true,
manufacturerData:
{ bytes:
[ 2,
1,
6,
3,
3,
170,
254,
21,
22,
170,
254,
0,
232,
0,
17,
34,
51,
68,
85,
102,
119,
136,
153,
171,
205,
239,
231,
3,
15,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0 ],
data: 'AgEGAwOq/hUWqv4A6AARIjNEVWZ3iJmrze/nAw8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
CDVType: 'ArrayBuffer' } },
rssi: -27,
id: 'AC:23:3F:27:1F:E2',
name: null }
我相信我应该根据Eddystone协议规范来解析制造商数据字节和数据,但是我确实对应该如何去做感到困惑。任何帮助将是巨大的!谢谢:)