Web 蓝牙:如何检测 iBeacon?
Web bluetooth: How do I detect iBeacon?
我一直在摆弄新的网络蓝牙功能。我有这些估计信标之一:http://developer.estimote.com/
我知道我的信标的 uuid。这是我正在使用的代码(它是一个 angular 应用程序,因此 $scope,$window):
$scope.runBT = runBT;
function runBT() {
let mobile = getMobileOperatingSystem();
if (mobile === 'Android' || mobile === 'iOS') {
$window.navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']
})
.then(device => {
console.log('FOUND DEVICE: ', device);
device.watchAdvertisements();
device.addEventListener('advertisementreceived', interpretIBeacon);
})
.catch(error => { console.log(error); });
}
}
function interpretIBeacon(event) {
var rssi = event.rssi;
var appleData = event.manufacturerData.get(0x004C);
if (appleData.byteLength != 23 ||
appleData.getUint16(0, false) !== 0x0215) {
console.log({isBeacon: false});
}
var uuidArray = new Uint8Array(appleData.buffer, 2, 16);
var major = appleData.getUint16(18, false);
var minor = appleData.getUint16(20, false);
var txPowerAt1m = -appleData.getInt8(22);
console.log({
isBeacon: true,
uuidArray,
major,
minor,
pathLossVs1m: txPowerAt1m - rssi});
}
不清楚问题出在哪里,但这里有一些提示:
了解 Web 蓝牙 API 是一组正在积极开发中的建议标准,支持仅限于 Google Chrome 的某些版本,并作为垫片Noble.js 蓝牙中央模块。如果您正在使用 Angular,则需要使用后一个垫片来使其工作,也许您已经在使用了。您可以在这里阅读更多内容:https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web
如果您达到了 interpretIBeacon
函数的极限,那么这只是将字节解析出来的问题,您似乎正在做这件事。您可以在我的回答中看到更多关于信标字节布局的信息:
您不想将信标 UUID 作为服务进行过滤,因此您需要删除 optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']
。信标 ProximityUUID 与 GATT ServiceUUID 不同,即使它们表面上具有相同的格式。您正在寻找 制造商 广告而非 *GATT 服务** 广告的信标蓝牙广告。两种广告类型不同,但上面显示的 API 应该 return 来自两种类型。
遗憾的是,watchAdvertisements
方法尚未实现。您可能需要查看 https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md 上的实施状态页面,了解 Chrome 和其他浏览器何时支持此方法。
我一直在摆弄新的网络蓝牙功能。我有这些估计信标之一:http://developer.estimote.com/
我知道我的信标的 uuid。这是我正在使用的代码(它是一个 angular 应用程序,因此 $scope,$window):
$scope.runBT = runBT;
function runBT() {
let mobile = getMobileOperatingSystem();
if (mobile === 'Android' || mobile === 'iOS') {
$window.navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']
})
.then(device => {
console.log('FOUND DEVICE: ', device);
device.watchAdvertisements();
device.addEventListener('advertisementreceived', interpretIBeacon);
})
.catch(error => { console.log(error); });
}
}
function interpretIBeacon(event) {
var rssi = event.rssi;
var appleData = event.manufacturerData.get(0x004C);
if (appleData.byteLength != 23 ||
appleData.getUint16(0, false) !== 0x0215) {
console.log({isBeacon: false});
}
var uuidArray = new Uint8Array(appleData.buffer, 2, 16);
var major = appleData.getUint16(18, false);
var minor = appleData.getUint16(20, false);
var txPowerAt1m = -appleData.getInt8(22);
console.log({
isBeacon: true,
uuidArray,
major,
minor,
pathLossVs1m: txPowerAt1m - rssi});
}
不清楚问题出在哪里,但这里有一些提示:
了解 Web 蓝牙 API 是一组正在积极开发中的建议标准,支持仅限于 Google Chrome 的某些版本,并作为垫片Noble.js 蓝牙中央模块。如果您正在使用 Angular,则需要使用后一个垫片来使其工作,也许您已经在使用了。您可以在这里阅读更多内容:https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web
如果您达到了
interpretIBeacon
函数的极限,那么这只是将字节解析出来的问题,您似乎正在做这件事。您可以在我的回答中看到更多关于信标字节布局的信息:您不想将信标 UUID 作为服务进行过滤,因此您需要删除
optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']
。信标 ProximityUUID 与 GATT ServiceUUID 不同,即使它们表面上具有相同的格式。您正在寻找 制造商 广告而非 *GATT 服务** 广告的信标蓝牙广告。两种广告类型不同,但上面显示的 API 应该 return 来自两种类型。
遗憾的是,watchAdvertisements
方法尚未实现。您可能需要查看 https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md 上的实施状态页面,了解 Chrome 和其他浏览器何时支持此方法。