We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在使用蓝牙的过程中,我们需要获取蓝牙设备的Mac地址。在Android设备上,onBluetoothDeviceFound方法中,deviceId 是蓝牙设备的Mac地址。而在 iOS设备上,deviceId则是蓝牙设备的uuid。我们想要在iOS设备上获取Mac地址,就需要自己想办法。
Mac
Android
onBluetoothDeviceFound
deviceId
iOS
uuid
通过查阅一些相关资料,发现有些蓝牙设备有180A这个服务id,该服务id中的2A23特征值可以获取蓝牙设备的Mac地址。具体操作参考下面的代码:
180A
id
2A23
function array2String(buffer) { let hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return `${hexArr[7]}:${hexArr[6]}:${hexArr[5]}:${hexArr[2]}:${hexArr[1]}:${hexArr[0]}` } // 连接蓝牙 wx.createBLEConnection({ deviceId, success: res => { // 获取服务id wx.getBLEDeviceServices({ deviceId, success: res => { let serviceId = '' for (let i = 0, len = res.services.length; i < len; i++) { // 使用包含 180A 的服务id if (~res.services[i].uuid.indexOf('180A')) { serviceId = res.services[i].uuid // 获取对应的特征值 wx.getBLEDeviceCharacteristics({ deviceId, serviceId, success: res => { let characteristicId = '' for (let i = 0, len = res.characteristics.length; i < len; i++) { // 使用含有 2A23 对应的特征值 if (~res.characteristics[i].uuid.indexOf('2A23')) { characteristicId = res.characteristics[i].uuid } } wx.readBLECharacteristicValue({ deviceId, serviceId, characteristicId, success: res => { console.log('mac read ----------', res) } }) wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId, state: true, }) wx.onBLECharacteristicValueChange(function (characteristic) { // 当特征值是查询 Mac 地址时 if (~characteristic.characteristicId.indexOf('2A23')) { let macInfo = (array2String(characteristic.value)).toUpperCase() console.log('mac info -----------', macInfo) } }) } }) } } } }) } })
当获取到蓝牙设备Mac地址后,我们一般还是要继续使用蓝牙的。这个时候需要重新获取蓝牙设备的服务id,以及新的特征值。
wx.getBLEDeviceCharacteristics({ deviceId, // 新的服务id, 通过 getBLEDeviceServices 获取 serviceId: newServiceId, success: res => { wx.notifyBLECharacteristicValueChange({ state: true, deviceId, serviceId: newServiceId, // 新的特征值 characteristicId: newCharacteristicId }) // 监听低功耗蓝牙连接的错误事件 wx.onBLEConnectionStateChange(res => { }) } })
The text was updated successfully, but these errors were encountered:
No branches or pull requests
遇到的问题
在使用蓝牙的过程中,我们需要获取蓝牙设备的

Mac
地址。在Android
设备上,onBluetoothDeviceFound
方法中,deviceId
是蓝牙设备的Mac
地址。而在iOS
设备上,deviceId
则是蓝牙设备的uuid
。我们想要在iOS
设备上获取Mac
地址,就需要自己想办法。解决的方法
通过查阅一些相关资料,发现有些蓝牙设备有
180A
这个服务id
,该服务id
中的2A23
特征值可以获取蓝牙设备的Mac
地址。具体操作参考下面的代码:后续
当获取到蓝牙设备
Mac
地址后,我们一般还是要继续使用蓝牙的。这个时候需要重新获取蓝牙设备的服务id
,以及新的特征值。The text was updated successfully, but these errors were encountered: