NativeScript 上下文 API
为您的 NativeScript 应用轻松访问上下文信息
npm i --save nativescript-context-apis

NativeScript 上下文 API

npm npm Build Status DOI

为您的 NativeScript 应用轻松访问上下文信息。您的应用需要知道用户的位置吗?查看他们周围的 Wi-Fi AP?或许附近的 BLE 设备?或者可能想知道他们在做什么活动?如果对这些问题的任何一个答案是肯定的,那么这个插件就是您的选择。

目前我们提供

信息来源 描述 Android iOS
当前用户位置和位置流(带有距离过滤) 粗略和精细位置报告。我们提供由 nativescript-geolocation 插件定义的功能集,并通过 RxJS Observable 流扩展位置流。通过获取包含在 Observable 中的用户位置,您将能够过滤它们,从少量中选择最佳位置,或者通过 RxJS Operators 简单地控制流。
粗略和中等粒度人类活动检测 粗略活动检测(用户处于静止、行走、跑步、骑自行车或驾车状态)将在用户开始或结束活动时通知您的应用,并记录活动发生的时间。中等粒度检测允许您指定检测间隔,并允许您在活动期间进行过滤。例如,当车辆在交通灯处停下时,插件将报告从驾车状态到静止状态的转换,这是粗略活动检测机制无法实现的。更多信息 计划中
列出当前附近的 Wi-Fi AP(也称为 Wi-Fi 指纹)并获取指纹更新 简单和批量 Wi-Fi 指纹报告。我们提供两种获取关于附近 Wi-Fi AP 更新的方式:以固定、较快的速率(可能包含重复)和以最低较慢的速率(确保所有报告的扫描都是新的)。后者可以配置为提供连续的单次扫描流(无分组)或提供每批 2(中间分组)或 4(最大分组)指纹的扫描,符合最新的 Android OS 限制。类似于位置更新,Wi-Fi 扫描可以通过 RxJS Observable 传递,因此允许在上面的它们上应用所有强大的 RxJS Operators
列出当前附近的 BLE 设备并获取关于它们存在的更新 简单和批量 BLE 扫描报告。可以配置扫描间隔和模式,使其适用于多种用例。通过结合 0 报告间隔的扫描和低延迟模式,设备可以在检测到后立即报告。而通过较大的报告间隔和低功耗模式,可以实现更节能的策略。类似于位置和 Wi-Fi 指纹更新,BLE 扫描更新通过 RxJS Observable 传递,因此为处理检测到的设备列表提供了很大的灵活性。

我们计划在未来提供

  • 对设备上传感器的底层访问(加速度计、陀螺仪、指南针等)。
  • 在 iOS 上也进行人类活动检测。
  • 一种全新的基于实时传感器监控的细粒度人类活动检测机制。
  • 用户位置反向地理编码。
  • 当前用户位置的天气情况。

先决条件

仅限Android

由于此插件依赖于@triniwiz/nativescript-couchbase,因此至少需要Android SDK 22 (5.1)

Google Play服务冲突

鉴于我们依赖于nativescript-geolocation并使用Google Play服务API在Android设备上进行活动检测,您可能会发现Google Play服务版本与其他已安装插件冲突。

为了避免这种情况,我们建议您强制指定特定的Google Play服务版本。为了获得更好的人类活动检测功能,强烈推荐使用版本17或更高版本。为此,请在您的app/App_Resources/Android/before-plugins.gradle文件中指定Google Play服务版本号(如果该文件不存在,请创建它)

android {  
// other things here

project.ext {
googlePlayServicesVersion = "17.+"
}
}

权限

接下来,对于此插件提供的每个信息源,您可以在位于App_Resources/Android/src/main目录中的您的应用程序的AndroidManifest.xml中看到需要添加的权限。

注意:如果您的应用程序需要使用此插件提供的多个信息源,我们建议您不要直接复制和粘贴以下各节的全部内容到您的应用程序清单中。请仔细检查是否有重复的权限(例如,用于位置和Wi-Fi都需要的位置权限),以避免可能的清单合并错误。

点击下面的可折叠部分,查看每个信息源所需的具体权限

地理定位

为了在Android中访问地理定位,您需要将以下权限添加到您的应用程序的AndroidManifest.xml

<!-- Always include this permission if your app needs location access -->
<!-- This permission is for "approximate" location data -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- Include only if your app benefits from precise location access. -->
<!-- This permission is for "precise" location data -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required only when requesting background location access on Android 10 (API level 29) and higher. -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

更多信息可以在此处的Android文档中找到。

来源:https://github.com/NativeScript/nativescript-geolocation

人类活动检测

为了在Android中接收人类活动更改,您需要将以下权限添加到您的应用程序的AndroidManifest.xml

<!-- The following two permissions are required if your app wants to receive human activity changes -->
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

更多信息可以在此处的Android文档中找到。

Wi-Fi 扫描更新

为了在Android中接收Wi-Fi扫描更新,您需要将以下权限添加到您的应用程序的AndroidManifest.xml

<!-- ALL the following permissions are required in order to ask and retrieve Wi-Fi scan updates -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

更多信息可以在此处的Android文档中找到。

BLE 扫描更新

为了在Android中接收BLE扫描更新,您需要将以下权限添加到您的应用程序的AndroidManifest.xml

<!-- ALL the following permissions are required in order to ask and retrieve BLE scan updates -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>

更多信息可以在此处的Android文档中找到。

此外,BLE扫描API使用由Nordic Semiconductors开发的知名扫描库。为确保仅当需要访问BLE扫描API时才包含此库,您需要更新位于App_Resources/Android/src文件夹中的应用程序的app.gradle文件,如下所示

// Uncomment
dependencies {
implementation 'no.nordicsemi.android.support.v18:scanner:1.6.0'
}

安装

在项目根目录中运行以下命令。

NS7+

ns plugin add nativescript-context-apis

NS6

tns plugin add nativescript-context-apis@1

(可选)您还需要RxJS来正确处理地理定位流

NS7+

npm install rxjs --save

NS6

npm install rxjs@6 --save

用法

仅用于人类活动检测

为了使活动检测正常工作,我们需要在某个地方将本地和JavaScript/TypeScript代码连接起来。

最佳位置是在您的应用程序的入口点脚本中。您需要向其中添加几行代码

// app.ts / main.ts
// TypeScript App:
import * as app from "tns-core-modules/application";
// or Angular App:
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app/app.module";

// NativeScript Task Context APIs plugin import
// (always between imports and app initialization)
import { contextApis } from "nativescript-context-apis";

contextApis.init().catch((err) => {
// You can catch initialization errors here
});

// TypeScript App:
app.run({ moduleName: "app-root" });
// Angular App:
platformNativeScriptDynamic().bootstrapModule(AppModule);

地理定位

如果您想获取地理定位更新,首先需要检查您是否有权限这样做,如果没有,请按照以下方式请求权限

import { contextApis } from "nativescript-context-apis";

async function checkGeolocationAccessStatus(): Promise<void> {
const provider = contextApis.geolocationProvider;
const isReady = await provider.isReady();
if (!isReady) {
await provider.prepare();
}
}

完成操作后(请注意,isReady()prepare() 方法是异步的),您将能够获取当前用户的位置或位置流。

import { contextApis } from "nativescript-context-apis";

// You can get the current location like this
async function printCurrentLocation() {
const provider = contextApis.geolocationProvider;
const location = await provider.acquireLocation({
highAccuracy: true, // Accuracy is high by default
timeout: 5000, // You can also specify the operation timeout (highly advised)
});
console.log("Current location:", location);
}

// Or a location stream
import { Subscription } from "rxjs";

async function printLocationUpdates(): Promise<Subscription> {
const provider = contextApis.geolocationProvider;

const stream = provider.locationStream({
highAccuracy: true, // Again, accuracy is high by default
stdInterval: 1000, // The location fetch interval
minInterval: 100, // Opportunistic interval (another app asked for a location)
timeout: 5000, // You can also specify the operation timeout (highly advised)
maxAge: 60000, // And filter-out old locations
});

return stream.subscribe({
next: (location) =>
console.log("New location acquired!:", location),
error: (error) =>
console.error("Location updates could not be acquired:", error)
});
}

人类活动检测

如果您想获取粗粒度的人类活动变化,首先您需要检查是否有权限这样做,如果没有,请按照以下方式请求:

import { contextApis } from "nativescript-context-apis";
import { Resolution } from "nativescript-context-apis/activity-recognition";

async function checkActivityRecognitionStatus(): Promise<void> {
const recognizer = contextApis.getActivityRecognizer(Resolution.LOW);
const isReady = recognizer.isReady();
if (!isReady) {
console.log(
`Up to prepare coarse-grained activity recognizer...`
);
await recognizer.prepare();
}
console.log(`Coarse-grained activity recognizer is ready`);
}

注意:如果您想使用中等粒度的识别器,只需将 Resolution.LOW 替换为 Resolution.MEDIUM

然后您将能够添加或删除活动变化监听器。

import { contextApis } from "nativescript-context-apis";
import { Resolution } from "nativescript-context-apis/activity-recognition";

const recognizer = contextApis.getActivityRecognizer(Resolution.LOW);

// Register a listener
const listenerId = recognizer.listenActivityChanges((activityChange) => {
console.log("ActivityChange:", activityChange);
});
console.log(`Coarse-grained activity recognizer has registered a listener (id: ${listenerId})`);

// ...
// Somewhere else
// ...

// Deregister a concrete listener
recognizer.stopListening(listenerId);
// Or all of them
recognizer.stopListening();

注册监听器后,您就可以指示活动识别器开始或停止识别活动变化了。

import { contextApis } from "nativescript-context-apis";
import { Resolution } from "nativescript-context-apis/activity-recognition";

const recognizer = contextApis.getActivityRecognizer(Resolution.LOW);

// Start recognizing
recognizer.startRecognizing();

// ...
// Somewhere else
// ...

// Stop recognizing
recognizer.stopRecognizing();

注意:感谢插件设计,即使在您的应用程序关闭或设备重启的情况下,识别状态也会保留。可以多次调用 startRecognizing 方法。如果您想更改启动参数,请先停止识别。也可以多次调用 stopRecognizing 方法,即使识别器没有运行。

附近的Wi-Fi AP更新

如果您想获取Wi-Fi扫描更新,首先您需要检查是否有权限这样做,如果没有,请按照以下方式请求:

import { contextApis } from "nativescript-context-apis";

async function checkWifiScanAccessStatus(): Promise<void> {
const provider = contextApis.wifiScanProvider;
const isReady = await provider.isReady();
if (!isReady) {
await provider.prepare();
}
}

完成操作后(请注意,isReady()prepare() 方法是异步的),您将能够获取当前附近Wi-Fi AP的信息或Wi-Fi扫描更新的流。

import { contextApis } from "nativescript-context-apis";

// You can get the current nearby Wi-Fi APs' information like this
async function printCurrentNearbyWiFiAPs() {
const provider = contextApis.wifiScanProvider;
const scanResult = await provider.acquireWifiFingerprint(
true // Ensures the scan result is new (true by default),
// thus adhering to Android OS Wi-Fi scanning limits:
// https://developer.android.com.cn/guide/topics/connectivity/wifi-scan#wifi-scan-throttling
);
console.log("Current nearby Wi-Fi APs:", scanResult);
}

// Or a Wi-Fi scan updates stream
import { Subscription } from "rxjs";
import { FingerprintGrouping } from "nativescript-context-apis/wifi";

async function printWifiScanUpdates(): Promise<Subscription> {
const provider = contextApis.wifiScanProvider;

const stream = provider.wifiFingerprintStream({
ensureAlwaysNew: true, // Identical in purpose to the parameter in acquireWifiFingerpint()
grouping: FingerprintGrouping.NONE, // Configure Wi-Fi scan updates batching (see API bellow)
continueOnFailure: true, // Determines if the stream has to report any scanning error and close
});

return stream.subscribe({
next: (fingerprint) =>
console.log("New wifi scan result!:", fingerprint),
error: (error) =>
console.error("Wifi scan result could not be acquired:", error),
});
}

附近的BLE设备更新

如果您想获取BLE扫描更新,首先您需要检查是否有权限这样做,如果没有,请按照以下方式请求:

import { contextApis } from "nativescript-context-apis";

async function checkBleScanAccessStatus(): Promise<void> {
const provider = contextApis.bleScanProvider;
const isReady = await provider.isReady();
if (!isReady) {
await provider.prepare();
}
}

完成操作后(请注意,isReady()prepare() 方法是异步的),您将能够获取当前附近BLE设备的信息或BLE扫描更新的流。

import { contextApis } from "nativescript-context-apis";
import { BleScanMode } from "nativescript-context-apis/ble";

// You can get the current nearby BLE devices' information like this
async function printCurrentNearbyBleDevices() {
const provider = contextApis.bleScanProvider;
const scanResult = await provider.acquireBleScan({
scanTime: 5000, // (optional) when not specified it will wait until seeing a device and inmediatelly return,
// otherwise it accumulates the seen devices and outputs after the scan time finishes.
// It may throw a timeout when used in conjunction with a list of iBeacon UUIDs
// (in case no known beacon has been detected in the meantime)
scanMode: BleScanMode.BALANCED, // (optional) Can be LOW_POWER or LOW_LATENCY too. BALANCED by default
iBeaconUuids: [
// (optional) add a list of iBeacon UUIDs if you'
re just looking for known beacons
],
});
console.log("Current nearby BLE devices:", scanResult);
}

// Or a BLE scan updates stream
import { Subscription } from "rxjs";

async function printWifiScanUpdates(): Promise<Subscription> {
const provider = contextApis.bleScanProvider;

const stream = provider.bleScanStream({
reportInterval: 2000, // (optional) when not specified it will output a result as soon as a device is seen,
// otherwise it accumulates the detected devices and outputs when told
scanMode: BleScanMode.LOW_LATENCY, // (optional) same as for the acquire method
iBeaconUuids: [
// (optional) Same as for the acquire method
],
})

return stream.subscribe({
next: (bleScanResult) =>
console.log(
`New ble scan result!: ${JSON.stringify(bleScanResult)}`
),
error: (error) =>
console.error(`Ble scan result could not be acquired: ${error}`),
});
}

注意:请查看插件示例应用程序以获取更多使用细节。

API

方法签名 返回类型 描述
init() Promise<void> 应在应用程序启动时调用。只有当您的应用程序监听人类活动变化时才需要。
geolocationProvider GeolocationProvider 属性用于访问地理位置提供者单例。
getActivityRecognizer(resolution: Resolution) ActivityRecognizer 应在应用程序启动时调用。只有当您的应用程序监听人类活动变化时才需要。
wifiScanProvider WifiScanProvider 属性用于访问Wi-Fi扫描提供者单例。
bleScanProvider BleScanProvider 属性用于访问BLE扫描提供者单例。

点击下面的可折叠部分以查看每个信息源的更多API细节。

地理位置访问

地理定位

属性 类型 描述
latitude number 位置的纬度
longitude number 位置的经度
altitude number 位置的海拔
horizontalAccuracy number 位置的水平精度(以米为单位)
verticalAccuracy number 位置的垂直精度(以米为单位)
speed number 位置修复时的速度(以m/s为单位)
direction number 位置的方位(以度为单位)
timestamp Date 位置修复的时间
方法签名 返回类型 描述
distance(to: Geolocation OR GeolocationLike) number 允许检查从地理位置到另一个地理位置或 GeolocationLike 对象的距离

GeolocationLike (接口)

Geolocation 相同,但纬度和经度是必需的。其余的属性是可选的。

Geolocation获取选项

在请求用户的当前位置之前,可以自定义一些选项以获得预期结果。

属性 类型 描述
highAccuracy boolean 指示是否应使用高精度(耗电)地理位置。默认为true。
timeout number 位置修复最大等待时间。默认为5分钟。
allowBackground boolean (iOS仅限)指示是否将在后台收集位置。默认为false。

注意:这些选项与nativescript-geolocation中的选项相同(只有名称不同)。如有疑问,请检查插件文档。

地理位置流选项

在请求用户位置更新之前,可以自定义一些选项以实现预期结果。

属性 类型 描述
highAccuracy boolean 指示是否应使用高精度(耗电)地理位置。默认为true。
distanceFilter number 用户需要覆盖的米数,然后报告新的位置。默认为无
stdInterval number (仅限Android) 标准位置获取间隔(以毫秒为单位)。默认为1分钟
minInterval number (仅限Android) 机会性位置报告间隔(以毫秒为单位)。默认为5秒。当另一个应用程序以更高间隔请求用户的位置时使用
maxAge number 报告的位置最大年龄(自Date.now()以来的毫秒数)。默认为无限
timeout number 位置修复最大等待时间。默认为5分钟。
allowBackground boolean (iOS仅限)指示是否将在后台收集位置。默认为false。
saveBattery boolean (仅限iOS) 指示当应用程序不再可见时,应暂停位置报告。默认为true

注意:这些选项与nativescript-geolocation中的选项相同(只有名称不同)。如有疑问,请检查插件文档。

GeolocationProvider

方法签名 返回类型 描述
isReady() boolean 允许检查提供者是否就绪(即,已授予所需权限)
prepare() Promise<boolean> 允许准备提供者以供使用(即,请求所需权限)。警告!只有当您的应用程序对用户可见时才调用此方法
acquireLocation(options: AcquireOptions) Promise<Geolocation> 允许获取用户的当前位置
locationStream(options: StreamOptions) Observable<Geolocation> 允许主动获取用户的位置更新
人类活动识别

可用的识别器分辨率

类型 描述
Resolution.LOW 粗粒度活动识别。活动更改以推送方式传递。
Resolution.MEDIUM 中等粒度活动识别。活动更改以可配置的延迟查询
Resolution.HIGH 即将推出

可用的人类活动

类型 描述
HumanActivity.STILL 未检测到明显的运动
HumanActivity.WALKING 低频步行运动
HumanActivity.RUNNING 高频步行运动
HumanActivity.ON_BICYCLE 在骑自行车时佩戴设备
HumanActivity.IN_VEHICLE 以高速通勤
HumanActivity.TILTING 设备的角度发生了明显变化

可用的活动转换

类型 描述
Transition.STARTED 相关的人类活动已开始
Transition.ENDED 相关的人类活动已结束

ActivityChange

属性 类型 描述
type HumanActivity 检测到更改的人类活动
transition Transition 指示活动是开始还是结束
confidence number 如果底层识别器支持,则检测到的活动的置信度(从0到1)
timestamp Date 指示何时检测到活动更改

活动识别器 StartOptions

属性 类型 描述
detectionInterval number (可选) 允许指定识别器检测间隔(由于基于推送的性质,在Resolution.LOW中忽略)

ActivityRecognizer

方法签名 返回类型 描述
isReady() boolean 允许检查活动识别器是否就绪(即,已授予所需权限)
prepare() Promise<boolean> 允许准备活动识别器以供使用(即,请求所需权限)。警告!只有当您的应用程序对用户可见时才调用此方法
setup() Promise<void> 仅限内部使用。允许调整识别器以适应在应用程序关闭之前的状态
startRecognizing(options?: StartOptions) Promise<void> 告诉活动识别器开始工作
stopRecognizing() Promise<void> 告诉活动识别器停止工作
listenActivityChanges(callback: (activityChange: ActivityChange) => void) number 添加活动更改监听器
stopListening(listenerId?: number) void 删除活动更改监听器。如果没有传递监听器编号,则将删除所有监听器
附近Wi-Fi AP的信息访问

WifiFingerprint

属性 类型 描述
可见 Array 此扫描期间已看到的AP(及其信息)列表
isNew boolean 指示指纹结果是否来自成功的Wi-Fi扫描,或来自缓存的结果(例如,当操作系统节流开始起作用时)
timestamp Date 指纹生成的时间戳。请注意,两个相同的指纹(一个新,另一个缓存)可以有不同的时间戳,但仍然是相同的指纹

WifiApInfo

属性 类型 描述
ssid string AP SSID(如果AP没有广播其SSID,则可以为空)
bssid string AP BSSID(MAC地址)
capabilities string 包含AP安全功能的列表
frequency number AP已看到的频率
centerFreq0 number 请参阅Android文档
centerFreq1 number 请参阅Android文档
bandwidth ChannelBandwidth AP用于广播的带宽(请参阅链接枚举以了解可能支持值)
standard WifiStandard 检测到的AP使用的Wi-Fi标准(请参阅链接枚举以了解可能支持值)
ageMicros number 在检测到此AP之后,经过的微秒数(从手机启动开始计算)
chainsInfo WifiChainInfo 设备用于连接到特定AP的每个链路的详细信息(请参阅链接接口了解更多)
rssi number 在扫描期间,此应用程序可见时的接收信号水平

ChannelBandwidth

类型 描述
ChannelBandwidth.UNSPECIFIED 电话(硬件或软件限制)无法识别信道带宽
ChannelBandwidth.MHZ20 20 MHz带宽
ChannelBandwidth.MHZ40 40 MHz带宽
ChannelBandwidth.MHZ80 80 MHz带宽
ChannelBandwidth.MHZ160 160 MHz带宽
ChannelBandwidth.MHZ80_80 80+80 MHz带宽
ChannelBandwidth.MHZ320 320 MHz带宽

WifiStandard

类型 描述
WifiStandard.UNKNOWN AP正在广播的Wi-Fi标准是未知的(软件限制)
WifiStandard.LEGACY 802.11a/b/g
WifiStandard.N 802.11n
WifiStandard.AC 802.11ac
WifiStandard.AX 802.11ax
WifiStandard.AD 802.11ad
WifiStandard.BE 802.11be

WifiChainInfo

属性 类型 描述
id number 链的顺序ID号(从0开始)
rssi number 此特定链的接收信号强度级别

Wi-Fi指纹获取参数

在请求附近Wi-Fi AP的当前信息之前,可以自定义一些选项,以实现预期的结果。

参数 类型 描述
ensureIsNew boolean 确保扫描结果是新的(默认为true),因此遵守Android OS Wi-Fi扫描限制

Wi-Fi指纹流选项

在请求附近Wi-Fi AP的信息更新之前,可以自定义一些选项,以实现预期的结果。

属性 类型 描述
ensureAlwaysNew boolean 确保流中的每个扫描结果始终是新的(默认为true),因此遵守Android OS Wi-Fi扫描限制
grouping FingerprintGrouping (仅用于ensureAlwaysNew: true。允许指示扫描结果是否必须批量处理以及批量大小的(默认为NONE,即批量大小:1)。请参阅链接枚举以获取更多详细信息。再次由Android OS Wi-Fi扫描节流条件约束。请注意,尽管应用了批量处理,但扫描会像来时一样报告
interval number (仅用于与 ensureAlwaysNew: false 一起使用) 允许手动指定 Wi-Fi 扫描之间的间隔(以毫秒为单位)。不能低于(5000,即5秒)。如果未在手机的开发者设置中禁用扫描节流,则新指纹的获取最大频率为每30秒一次。
continueOnFailure boolean 指示是否应通过流报告扫描失败(中断流)或不报告。

FingerprintGrouping

类型 描述
FingerprintGrouping.NONE 在扫描期间不应用指纹批量处理(新指纹的报告间隔:30秒)。
FingerprintGrouping.INTERMEDIATE 执行两次连续的指纹扫描(时间间隔为5秒)(新指纹的报告间隔:60秒)。
FingerprintGrouping.MAX 执行四次连续的指纹扫描(时间间隔为5秒)(新指纹的报告间隔:2分钟)。

WifiScanProvider

方法签名 返回类型 描述
isReady() boolean 允许检查提供者是否就绪(即,已授予所需权限)
prepare() Promise<boolean> 允许准备提供者以供使用(即,请求所需权限)。警告!只有当您的应用程序对用户可见时才调用此方法
acquireWifiFingerprint(ensureIsNew: 布尔值) Promise<WifiFingerprint> 允许获取附近 Wi-Fi AP 的信息(指纹识别)。
wifiFingerprintStream(options: 流选项) Observable<WifiFingerprint> 允许主动获取附近 Wi-Fi AP 的信息(指纹识别)。
附近 BLE 设备的信息访问

BleScanResult

属性 类型 描述
可见 Array<BleDeviceInfo> 在此次扫描期间看到的 BLE 设备列表(及其信息)。
timestamp Date 此次扫描完成的时间戳。

BleDeviceInfo

属性 类型 描述
address string 设备的 MAC 地址。
name string 设备名称(可以为空)。
advertiseFlags number 指示设备的可发现模式和能力的广告标志(未设置时为 -1)。
advertisingSid number 设备的广告 SID(不存在时为 255)。
advertisingInterval number 范围从 6(7.5ms)到 65536(81918.75ms),单位为 1.25ms(未设置时为 0)。
txPowerLevel number 数据包的传输功率电平,单位为 dBm(未设置时为 -2147483648,即 -inf)。可以通过计算 txPowerLevelrssi 之间的差异来计算路径损耗。
txPower number 传输功率,单位为 dBm。范围从 -127 到 126(未设置时为 127)。
primaryPhy PhyType 可以是:1M,编码或无(当设备不支持检索此信息时)。
secondaryPhy PhyType 可以是:2M,编码或无(当设备不支持检索此信息或 BLE 设备未使用辅助物理通道时)。
serviceUuids Array<string> 设备提供的一组 BLE 服务 UUID(不要与 iBeacon UUID 混淆,这些将在后面出现)。
legacy boolean 当为 true 时,表示检测到的设备的规范早于 BLEv5 规范。
connectable boolean 当为 true 时,表示检测到的设备接受输入连接。
iBeacon IBeaconData 当检测到的设备不广播 iBeacon 数据时为未定义。如果已定义,则包含一个对象,其中包含信标的 UUID、主要和次要编号。
rssi number 在扫描期间,此应用程序可见时的接收信号水平
ageNanos number 自手机启动后看到的设备的时间(纳秒)。

PhyType

类型 描述
PhyType.UNUSED 物理通道未使用或手机无法检测其类型(Android SDK < 26)。
PhyType.LE_1M 设备使用主物理通道。
PhyType.LE_2M 设备使用辅助物理通道。
PhyType.LE_CODED 设备使用相应的物理通道与掩码。

IBeaconData

属性 类型 描述
uuid string iBeacon 部署的 UUID。
major number 用于在部署内识别信标的 iBeacon 主要编号。
minor number 用于在部署内识别信标的 iBeacon 次要编号。

BLE 扫描获取选项

在请求附近 BLE 设备的当前信息之前,可以自定义一些选项以实现预期的结果。

属性 类型 描述
scanTime boolean 指示扫描时等待的时间。当为0时,会等待直到检测到第一个设备。注意:当为0并且已设置至少一个iBeacon部署UUID时,会等待直到看到第一个信标。请谨慎使用。
scanMode BleScanMode 指示要使用的扫描模式(请参阅链接的枚举以获取更多信息)
iBeaconUuids Array<string> 可选地指示要报告包含这些iBeacon部署UUID的扫描结果列表

BleScanMode

类型 描述
BleScanMode.LOW_POWER 扫描0.5秒,然后在运行下一次扫描前等待4.5秒。适用于与扫描时间和间隔大于5秒的组合使用。
BleScanMode.LOW_LATENCY 使用最高频率扫描,无等待。非常耗电,请谨慎使用
BleScanMode.BALANCED 处于其他两种模式之间。扫描2秒,然后等待3秒。提供了很好的可靠性/电池使用平衡。

BLE扫描流选项

在请求有关附近BLE设备的信息更新之前,可以通过自定义一些选项来实现预期结果。

属性 类型 描述
reportInterval boolean 指示在报告自上次成功扫描以来检测到的设备列表之间等待的时间。当为0时,在看到新设备后立即通知。注意:报告频率可能相当高。请谨慎使用。
scanMode BleScanMode 指示要使用的扫描模式(请参阅链接的枚举以获取更多信息)
iBeaconUuids Array<string> 可选地指示要报告包含这些iBeacon部署UUID的扫描结果列表

BleScanProvider

方法签名 返回类型 描述
isReady() boolean 允许检查提供者是否就绪(即,已授予所需权限)
prepare() Promise<boolean> 允许准备提供者以供使用(即,请求所需权限)。警告!只有当您的应用程序对用户可见时才调用此方法
acquireBleScan(options: AcquireOptions) Promise<BleScanResult> 允许在检测到设备或指定扫描时间后立即获取附近BLE设备的信息
wifiFingerprintStream(options: StreamOptions) Observable<BleScanResult> 允许在检测到设备后立即或按指示的报告间隔批量获取附近BLE设备的信息

插件作者

Alberto González Pérez Miguel Matey Sanz

致谢

本插件的开发得益于西班牙政府。具体来说,来自西班牙教育部、文化和体育部(资助编号FPU17/03832),以及“面向社会挑战的R&D+i国家计划”(编号RTI2018-099939-BI-00)。

此项目是从SyMptOMS项目(位于Geotec)中提取的开放源代码摘录。具体来说,它在SyMptOMS移动应用程序中已经重用了两年多,并包含了我们在那里学到的一切。