【发布时间】:2020-11-02 03:50:30
【问题描述】:
我在 Nativescript 中使用 this plugin 来使用 Beacon,代码如下:
import {Observable} from 'tns-core-modules/data/observable';
import {
BeaconRegion, Beacon, BeaconCallback,
BeaconLocationOptions, BeaconLocationOptionsIOSAuthType, BeaconLocationOptionsAndroidAuthType
} from 'nativescript-ibeacon/nativescript-ibeacon.common';
import {NativescriptIbeacon} from 'nativescript-ibeacon';
export default class BeaconAdapter extends Observable implements BeaconCallback{
private nativescriptIbeacon: NativescriptIbeacon;
public message: string = 'Init';
private region: BeaconRegion = null;
constructor(id: string, uuid: string) {
super();
let options: BeaconLocationOptions = {
iOSAuthorisationType: BeaconLocationOptionsIOSAuthType.Always,
androidAuthorisationType: BeaconLocationOptionsAndroidAuthType.Fine,
androidAuthorisationDescription: 'Location permission needed'
};
this.nativescriptIbeacon = new NativescriptIbeacon(this, options);
this.region = new BeaconRegion(id, uuid, 100, 1);
}
start() {
this.message = 'start';
if (!this.nativescriptIbeacon.isAuthorised()) {
console.log('NOT Authorised');
this.nativescriptIbeacon.requestAuthorization()
.then(() => {
console.log('Authorised by the user');
this.nativescriptIbeacon.bind();
}, (e) => {
console.log('Authorisation denied by the user');
})
} else {
console.log('Already authorised');
this.nativescriptIbeacon.bind();
}
}
stop() {
this.message = 'stop';
this.nativescriptIbeacon.stopRanging(this.region);
this.nativescriptIbeacon.stopMonitoring(this.region);
this.nativescriptIbeacon.unbind();
}
onBeaconManagerReady(): void {
console.log('onBeaconManagerReady');
this.nativescriptIbeacon.startRanging(this.region);
this.nativescriptIbeacon.startMonitoring(this.region);
}
didRangeBeaconsInRegion(region: BeaconRegion, beacons: Beacon[]): void {
//console.log('didRangeBeaconsInRegion: ' + region.identifier + ' - ' + beacons.length);
//this.message = 'didRangeBeaconsInRegion: ' + (new Date().toDateString());
for (let beacon of beacons) {
console.log('B: ' + beacon.proximityUUID + ' - ' + beacon.major + ' - ' + beacon.minor + ' - ' + beacon.distance_proximity + ' - ' + beacon.rssi + ' - ' + beacon.txPower_accuracy );
}
}
didFailRangingBeaconsInRegion(region: BeaconRegion, errorCode: number, errorDescription: string): void {
console.log('didFailRangingBeaconsInRegion: ' + region.identifier + ' - ' + errorCode + ' - ' + errorDescription);
}
didEnterRegion(region: BeaconRegion) {
//console.log(region);
console.log('Did enter Region ' + region.identifier);
}
didExitRegion(region: BeaconRegion) {
//console.log(region);
console.log('Did leave Region ' + region.identifier);
}
}
但是当它开始监控或测距时,即使我在我正在测试的设备旁边有一个发射设备,也不会检测到任何设备
还有一个问题,当应用程序在后台运行时,应用程序崩溃并出现此错误(使用此插件)
不允许启动服务 Intent 应用在后台 uid UidRecord
【问题讨论】:
标签: android ios mobile ibeacon nativescript-vue