我正在尝试使用Ionic Native IBeacons插件在Ionic 3.6项目上实现屏幕上的IBeacons。
这是我的代码:
scanIBeacons(){
this.ibeacon.requestAlwaysAuthorization();
this.ibeacon.isBluetoothEnabled().then(isEnabled => {
console.log("BLUETOOTH ENABLED " + isEnabled)
this.isBluetooth = isEnabled;
if(isEnabled){
if(!this.didRangeBeaconsInRegion) {
let delegate = this.ibeacon.Delegate();
this.didRangeBeaconsInRegion = delegate.didRangeBeaconsInRegion()
.subscribe(
data => {
console.log('## didRangeBeaconsInRegion: ' + JSON.stringify(data))
this.updateUI(data)
},
error => console.error()
);
}
//range all my individual beacons
for(let myBeacon of this.mybeacons){
this.rangeBeacon(myBeacon.major, myBeacon.minor);
}
}
else{
if(this.didRangeBeaconsInRegion)
this.didRangeBeaconsInRegion.unsubscribe();
this.didRangeBeaconsInRegion = null;
//stop range all my individual beacons
for(let myBeacon of this.mybeacons){
this.stopRangeBeacon(myBeacon.major, myBeacon.minor);
}
}
})
}
rangeBeacon(major:number, minor:number){
let beaconRegion = this.ibeacon.BeaconRegion(major + "-" + minor,'UUID',major, minor);
this.ibeacon.startRangingBeaconsInRegion(beaconRegion)
.then(
() => console.log('%%%%%%Native layer recieved the request to ranging%%%%%%'),
error => console.error('%%%%%%Native layer failed to begin ranging%%%%%%: ', error)
);
}
stopRangeBeacon(major:number, minor:number){
let beaconRegion = this.ibeacon.BeaconRegion(major + "-" + minor,'UUID',major, minor);
this.ibeacon.stopRangingBeaconsInRegion(beaconRegion)
.then(
() => console.log('%%%%%%Native layer recieved the request to ranging%%%%%%'),
error => console.error('%%%%%%Native layer failed to begin ranging%%%%%%: ', error)
);
}
如果我启动了蓝牙激活的应用程序,它开始正确测量IBeacons,但是当我按下de bluetooth toggle关闭然后再次打开它停止测距时,它仍然接收测距事件但是信标数组是空的。要解决的唯一方法是完全关闭应用程序并再次打开它。
我已经尝试删除范围监听器,停止所有范围添加并重新启动它而不起作用。
如果我在关闭蓝牙的情况下启动应用程序,然后在这种情况下打开正常工作。
会出错的是什么?
编辑:此问题仅发生在Android 7上