nativeScript-ibeacon
Nativescript 的 iBeacon 扫描支持
npm i --save nativescript-ibeacon

npm npm

NativeScript iBeacon

让您搜索 iBeacon,管理权限。iOS 和 Android。

安装它

tns plugin add nativescript-ibeacon

使用它

在 Android Manifest 中添加权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

您也可以使用 android.permission.ACCESS_FINE_LOCATION 而不是 android.permission.ACCESS_COARSE_LOCATION

在 iOS Info.plist 中添加键
后台使用
<key>NSLocationAlwaysUsageDescription</key>
<string>My description explaining why I need it</string>
前台使用
<key>NSLocationWhenInUseUsageDescription</key>
<string>My description explaining why I need it</string>
使用它
通过传递回调和选项创建对象
import {NativescriptIbeacon, BeaconCallback, BeaconLocationOptions, BeaconLocationOptionsIOSAuthType, BeaconLocationOptionsAndroidAuthType, BeaconRegion, Beacon } from 'nativescript-ibeacon';

let callback: BeaconCallback = {
onBeaconManagerReady(): void {
// start ranging and/or monitoring only when the beacon manager is ready
this.nativescriptIbeacon.startRanging(this.region);
this.nativescriptIbeacon.startMonitoring(this.region);
},
didRangeBeaconsInRegion: function(region: BeaconRegion, beacons: Beacon[]) {

},
didFailRangingBeaconsInRegion: function(region: BeaconRegion, errorCode: number, errorDescription: string) {

}
}

let options: BeaconLocationOptions = {
iOSAuthorisationType: BeaconLocationOptionsIOSAuthType.Always,
androidAuthorisationType: BeaconLocationOptionsAndroidAuthType.Coarse,
androidAuthorisationDescription: "Location permission needed"
};

let nativescriptIbeacon = new NativescriptIbeacon(callback, options);

let region = new BeaconRegion("HelloID", "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6");
请求权限并绑定 BeaconManager

我们需要确保有权限,并需要通过调用 bind() 来准备 BeaconManager。

if (!nativescriptIbeacon.isAuthorised()) {
console.log("NOT Authorised");
nativescriptIbeacon.requestAuthorization()
.then(() => {
console.log("Authorised by the user");
nativescriptIbeacon.bind();

}, (e) => {
console.log("Authorisation denied by the user");
})
} else {
console.log("Already authorised");
nativescriptIbeacon.bind();
}

当 BeaconManager 准备好时,会调用事件 onBeaconManagerReady()。之后我们可以调用 startRanging(region)startMonitoring(region)

如果我们调用 startRanging(region)startMonitoring(region) 在调用 bind() 之前,它将内部调用,并在 BeaconManager 准备好后再注册区域。

停止搜索
nativescriptIbeacon.stopRanging(region);
停止监控
nativescriptIbeacon.stopMonitoring(region);
解绑

要销毁 BeaconManager,请调用 unbind() 方法

nativescriptIbeacon.unbind();

关于 Beacon 类的说明

共享的 Beacon 类包含以下值

public proximityUUID: string;
public major: number;
public minor: number;

public rssi: number;
public distance_proximity: number;  // distance in Android, proximity in iOS
public txPower_accuracy: number;  // txPower in Android, accuracy in iOS

如您所见,iOS 和 Android 平台在最后两个值上有所不同。在使用该类时请记住这一点。

在 iOS 上,接近度的值为

public enum CLProximity : Int {
    case unknown  // =0
    case immediate  // =1
    case near  // =2
    case far  // =3
}    

The value in this property gives a general sense of the relative distance to the beacon. 
Use it to quickly identify beacons that are nearer to the user rather than farther away.

在 iOS 上,精度描述如下

Indicates the one sigma horizontal accuracy in meters. Use this property to differentiate between beacons with the same proximity value. Do not use it to identify a precise location for the beacon. Accuracy values may fluctuate due to RF interference.
A negative value in this property signifies that the actual accuracy could not be determined.

public typealias CLLocationAccuracy = Double

运行演示

请注意,您需要真实的设备来测试接近度,否则您将无法测试 beacon ranging。

要运行演示,请使用以下命令之一

npm run demo.ios.device

npm run demo.android.device

其他命令

  • 更好的演示
  • 调试
  • 更多选项