【问题标题】:Beacon monitoring is not working in nativescript vue信标监控在 nativescript vue 中不起作用
【发布时间】: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


    【解决方案1】:

    需要执行的一些基本故障排除步骤:

    1. 检查运行时看到的日志消息。这可能会为您提供失败的线索。

    2. 确认您的应用是否已在您的应用设置中成功获得位置权限。这样做的说明因平台而异。如果配置正确,您应该会在授予位置的设置中看到应用权限列表中的位置条目。

    3. 验证您的 uuid、major 和 minor 在此语句中是否正确:new BeaconRegion(id, uuid, 100, 1) -- 如果您在同一部手机上运行像 Locate Beacon 这样的现成信标检测器,它是否会检测到具有相同标识符的信标?

    【讨论】:

      【解决方案2】:

      解决方案:

      1. 添加到app.gradle

        dependencies {
               compile 'org.altbeacon:android-beacon-library:2.15.1'
        }
        
      2. 添加这个插件:nativescript-community/ble

      3. 添加到 AndroidManifest 权限:ACCESS_FINE_LOCATION

      在我的情况下,此解决方案检测到我的信标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-25
        • 1970-01-01
        • 2020-08-05
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 2020-08-07
        相关资源
        最近更新 更多