>这里是代码
beacon-provider.ts>>
initialise(): any {
let promise = new Promise((resolve, reject) => {
if (this.platform.is('cordova')) {
IBeacon.enableBluetooth();
this.delegate = IBeacon.Delegate();
this.delegate.didRangeBeaconsInRegion()
.subscribe(
data => {
this.events.publish('didRangeBeaconsInRegion', data);
},
error => console.error()
);
this.region = IBeacon.BeaconRegion('deskBeacon', '24DDF411-8CF1-440C-87CD-E368DAF9C93E');
IBeacon.startRangingBeaconsInRegion(this.region)
.then(
() => {
resolve(true);
},
error => {
console.error('Failed to begin monitoring: ', error);
resolve(false);
}
);
} else {
console.error("This application needs to be running on a device");
resolve(false);
}
});
return promise;
}
}
首页.ts>>
export class HomePage {
beacons: BeaconModel[] = [];
zone: any;
constructor(public navCtrl: NavController, public platform: Platform, public beaconProvider: BeaconProvider, public events: Events) {
this.zone = new NgZone({ enableLongStackTrace: false });
}
ionViewDidLoad() {
this.platform.ready().then(() => {
this.beaconProvider.initialise().then((isInitialised) => {
if (isInitialised) {
this.listenToBeaconEvents();
}
});
});
}
listenToBeaconEvents() {
this.events.subscribe('didRangeBeaconsInRegion', (data) => {
this.zone.run(() => {
this.beacons = [];
let beaconList = data.beacons;
beaconList.forEach((beacon) => {
let beaconObject = new BeaconModel(beacon);
this.beacons.push(beaconObject);
});
});
});
}
}
在此代码中,alert(JSON.stringify(data))
的结果为:
{"eventType":"didRangeBeaconslnRegion","region":{"identifier":"desk beacon","uuid":"24DDF411-8CF1-440C-87CD-E368DAF9C93E","typeName":"BeaconRegion"}, "beacons":[]}
字段data.beacons
为空。
问题出在哪里?
还有一个问题,我首先尝试BLE中央插件,但是,当我使用 BLE 中央插件时,我得到信号,但它没有给我主要,如果我从广告中获得这个值,次要值?
有很多事情可能会导致此行为:
- 验证蓝牙是否已打开
- 验证你的应用是否已被授予检测蓝牙设备所需的运行时位置权限。 转到"设置"-">应用"->"[你的应用名称]"-">权限",并确保在开关打开的情况下看到"位置"条目。
- 使用现成的检测器应用程序验证您的信标是否确实正在传输您期望的标识符. 在这里试试我的定位应用: https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=en