|
| 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