Skip to content

Commit 97e78d1

Browse files
committed
add types
1 parent 12d5827 commit 97e78d1

5 files changed

Lines changed: 393 additions & 45 deletions

File tree

examples/samples/ranging.android.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This documentation is linked to the sample code [ranging.android.js](./ranging.a
66

77
## 1- start detection for beacon of your choice
88

9-
Before starting tell the library what kind of beacon you want to manage.
9+
Before starting, tell the library what kind of beacon you want to manage.
1010

1111
```javascript
1212
// dealing with iBeacons:

lib/module.types.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// @flow
2+
3+
export type BeaconRegion = {
4+
identifier: string,
5+
uuid: string,
6+
minor?: number,
7+
major?: number
8+
};
9+
10+
11+
// iOS only
12+
export type AuthorizationStatus =
13+
| 'authorizedAlways'
14+
| 'authorizedWhenInUse'
15+
| 'denied'
16+
| 'notDetermined'
17+
| 'restricted';
18+
19+
// iOS only
20+
type GetAuthorizationCallback = (status: AuthorizationStatus) => void;
21+
22+
23+
// android only
24+
export const PARSER_IBEACON: string = 'm:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24';
25+
export const PARSER_ESTIMOTE: string = 'm:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24';
26+
export const ALTBEACON: string = 'm:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25';
27+
export const EDDYSTONE_TLM: string = 'x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15';
28+
export const EDDYSTONE_UID: string = 's:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19';
29+
export const EDDYSTONE_URL: string = 's:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v';
30+
31+
// android only
32+
export const transmissionSupport: Array<string> = [
33+
'SUPPORTED',
34+
'NOT_SUPPORTED_MIN_SDK',
35+
'NOT_SUPPORTED_BLE',
36+
'DEPRECATED_NOT_SUPPORTED_MULTIPLE_ADVERTISEMENTS',
37+
'NOT_SUPPORTED_CANNOT_GET_ADVERTISER',
38+
'NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS'
39+
];
40+
41+
type transmissionSupportValue = string;
42+
43+
44+
export type Parser = string | number;
45+
46+
export type BeaconsManagerIOS = {
47+
// specific to iOS:
48+
requestAlwaysAuthorization: () => void,
49+
requestWhenInUseAuthorization: () => void,
50+
getAuthorizationStatus: (cb: GetAuthorizationCallback) => void,
51+
52+
// common with android:
53+
startMonitoringForRegion: (region: BeaconRegion) => void,
54+
startRangingBeaconsInRegion: (region: BeaconRegion) => void,
55+
stopMonitoringForRegion: (region: BeaconRegion) => void,
56+
stopRangingBeaconsInRegion: (region: BeaconRegion) => void
57+
};
58+
59+
60+
export type BeaconsManagerANDROID = {
61+
// specific to android:
62+
setHardwareEqualityEnforced: (flag: boolean) => void,
63+
addParser: (parser: Parser) => void,
64+
setBackgroundScanPeriod: (period: number) => void,
65+
setBackgroundBetweenScanPeriod: (period: number) => void,
66+
setForegroundScanPeriod: (period: number) => void,
67+
setRssiFilter: (filterType: number, avgModifier: number) => void,
68+
getRangedRegions: (value?: any) => void,
69+
ARMA_RSSI_FILTER: string,
70+
RUNNING_AVG_RSSI_FILTER: string,
71+
getMonitoredRegions: (value?: any) => void,
72+
checkTransmissionSupported: (status: any) => any,
73+
74+
// common with iOS:
75+
startMonitoring: (
76+
regionId: string,
77+
uuid: string,
78+
minor?: number,
79+
major?: number,
80+
resolve: () => any,
81+
reject: () => any
82+
) => void,
83+
84+
startRanging: (
85+
regionId: string,
86+
uuid?: string,
87+
resolve: () => any,
88+
reject: () => any
89+
) => void,
90+
91+
stopMonitoring: (
92+
regionId: string,
93+
uuid: string,
94+
minor?: number,
95+
major?: number,
96+
resolve: () => any,
97+
reject: () => any
98+
) => void,
99+
100+
stopRanging: (
101+
regionId: string,
102+
uuid?: string,
103+
resolve: () => any,
104+
reject: () => any
105+
) => void
106+
};

lib/new.module.android.js

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
// @flow
2+
3+
import { NativeModules } from 'react-native';
4+
5+
import type {
6+
BeaconRegion,
7+
BeaconsManagerANDROID
8+
} from './module.types';
9+
import {
10+
PARSER_IBEACON,
11+
PARSER_ESTIMOTE,
12+
ALTBEACON,
13+
EDDYSTONE_TLM,
14+
EDDYSTONE_UID,
15+
EDDYSTONE_URL,
16+
transmissionSupport
17+
} from './module.types';
18+
19+
const BeaconsManager: BeaconsManagerANDROID = NativeModules.BeaconsManagerModule;
20+
21+
22+
const ARMA_RSSI_FILTER = BeaconsManager.ARMA_RSSI_FILTER;
23+
const RUNNING_AVG_RSSI_FILTER = BeaconsManager.RUNNING_AVG_RSSI_FILTER;
24+
25+
function setHardwareEqualityEnforced(e: boolean): void {
26+
BeaconsManager.setHardwareEqualityEnforced(e);
27+
}
28+
29+
/**
30+
* set beacon layout for iBeacon
31+
*
32+
*/
33+
function detectIBeacons(): void {
34+
BeaconsManager.addParser(PARSER_IBEACON);
35+
}
36+
37+
/**
38+
* set beacon layout for alBeacon
39+
*
40+
*/
41+
function detectAltBeacons(): void {
42+
BeaconsManager.addParser(ALTBEACON);
43+
}
44+
45+
/**
46+
* set beacon layout for estimote
47+
*
48+
*/
49+
function detectEstimotes(): void {
50+
BeaconsManager.addParser(PARSER_ESTIMOTE);
51+
}
52+
53+
/**
54+
* set beacon layout for eddystone UID
55+
*
56+
*/
57+
function detectEddystoneUID(): void {
58+
BeaconsManager.addParser(EDDYSTONE_UID);
59+
}
60+
61+
/**
62+
* set beacon layout for eddystone URL
63+
*
64+
*/
65+
function detectEddystoneURL(): void {
66+
BeaconsManager.addParser(EDDYSTONE_URL);
67+
}
68+
69+
/**
70+
* set beacon layout for eddystone TLM
71+
*
72+
*/
73+
function detectEddystoneTLM(): void {
74+
BeaconsManager.addParser(EDDYSTONE_TLM);
75+
}
76+
77+
/**
78+
* set beacon for custom layout
79+
*
80+
*/
81+
function detectCustomBeaconLayout(parser: number): void {
82+
BeaconsManager.addParser(parser);
83+
}
84+
85+
function setBackgroundScanPeriod(period: number): void {
86+
BeaconsManager.setBackgroundScanPeriod(period);
87+
}
88+
89+
function setBackgroundBetweenScanPeriod(period: number): void {
90+
BeaconsManager.setBackgroundBetweenScanPeriod(period);
91+
}
92+
93+
function setForegroundScanPeriod(period: number): void {
94+
BeaconsManager.setForegroundScanPeriod(period);
95+
}
96+
97+
function setRssiFilter(filterType: number, avgModifier: number): void {
98+
BeaconsManager.setRssiFilter(filterType, avgModifier);
99+
}
100+
101+
function getRangedRegions(): Promise<any> {
102+
return new Promise((resolve, reject) => {
103+
BeaconsManager.getRangedRegions(resolve);
104+
});
105+
}
106+
107+
/**
108+
* get monitored regions
109+
*
110+
* @returns {Promise<Array<BeaconRegion>>} promise resolve to an array of monitored regions
111+
*/
112+
function getMonitoredRegions(): Promise<Array<BeaconRegion>> {
113+
return new Promise((resolve, reject) => {
114+
BeaconsManager.getMonitoredRegions(resolve);
115+
});
116+
}
117+
118+
/**
119+
* check if beacon support transmission
120+
*
121+
* @returns {Promise<number>} promise resolve to an integer
122+
*/
123+
function checkTransmissionSupported(): Promise<number> {
124+
return new Promise((resolve, reject) => {
125+
BeaconsManager.checkTransmissionSupported(status => resolve(transmissionSupport[status]));
126+
});
127+
}
128+
129+
130+
131+
/**
132+
* start monitoring for a region
133+
*
134+
* @param {Object: BeaconRegion} region region to monitor (identifier + uuid -> major and minor are optional)
135+
* @returns {Promise<any>} promise resolves to void or error
136+
*/
137+
function startMonitoringForRegion(region: BeaconRegion): Promise<any> {
138+
return new Promise(
139+
(resolve, reject) => {
140+
// NOTE: major and minor are optional values: if user don't assign them we have to send a null value (not undefined):
141+
BeaconsManager.startMonitoring(
142+
region.identifier,
143+
region.uuid,
144+
region.minor ? region.minor : -1,
145+
region.major ? region.major : -1,
146+
resolve,
147+
reject
148+
);
149+
}
150+
);
151+
}
152+
153+
/**
154+
* stops monittorings for a region
155+
*
156+
* @param {BeaconRegion} region region (see BeaconRegion type)
157+
* @returns {Promise<any>} promise resolves to void or error
158+
*/
159+
function stopMonitoringForRegion(region: BeaconRegion): Promise<any> {
160+
return new Promise(
161+
(resolve, reject) => {
162+
BeaconsManager.stopMonitoring(
163+
region.identifier,
164+
region.uuid,
165+
region.minor ? region.minor : -1,
166+
region.major ? region.major : -1,
167+
resolve,
168+
reject
169+
);
170+
}
171+
);
172+
}
173+
174+
/**
175+
* start ranging a region (with optional UUID)
176+
*
177+
* @param {string} regionId specified region to range
178+
* @param {string} [beaconsUUID] optional UUID
179+
* @returns {Promise<any>} promise resolves to void or error
180+
*/
181+
function startRangingBeaconsInRegion(regionId: string, beaconsUUID?: string): Promise<any> {
182+
return new Promise(
183+
(resolve, reject) => {
184+
BeaconsManager.startRanging(regionId, beaconsUUID, resolve, reject);
185+
}
186+
);
187+
}
188+
189+
/**
190+
* Stops the range scan for beacons
191+
*
192+
* @param {string} regionId specified region to stop scan
193+
* @param {string} beaconsUUID optional UUID within the specified region
194+
* @returns {Promise<any>} promise: resolves to void when successful
195+
*/
196+
function stopRangingBeaconsInRegion(regionId: string, beaconsUUID?: string): Promise<any> {
197+
return new Promise(
198+
(resolve, reject) => {
199+
BeaconsManager.stopRanging(regionId, beaconsUUID, resolve, reject);
200+
}
201+
);
202+
}
203+
204+
export default {
205+
setHardwareEqualityEnforced,
206+
detectIBeacons,
207+
detectAltBeacons,
208+
detectEstimotes,
209+
detectEddystoneUID,
210+
detectEddystoneTLM,
211+
detectEddystoneURL,
212+
detectCustomBeaconLayout,
213+
setBackgroundScanPeriod,
214+
setBackgroundBetweenScanPeriod,
215+
setForegroundScanPeriod,
216+
setRssiFilter,
217+
checkTransmissionSupported,
218+
getRangedRegions,
219+
ARMA_RSSI_FILTER,
220+
RUNNING_AVG_RSSI_FILTER,
221+
222+
getMonitoredRegions,
223+
224+
// common iOS:
225+
startMonitoringForRegion,
226+
startRangingBeaconsInRegion,
227+
stopMonitoringForRegion,
228+
stopRangingBeaconsInRegion,
229+
};

0 commit comments

Comments
 (0)