diff --git a/app/services/BroadcastingService.js b/app/services/BroadcastingService.js index b3451fcee3..8a922ce02c 100644 --- a/app/services/BroadcastingService.js +++ b/app/services/BroadcastingService.js @@ -14,7 +14,7 @@ var lastSeen = {}; const c5_MINS = 1000 * 60 * 5; const c28_DAYS = 1000 * 60 * 60 * 24 * 28; -const c1_HOUR = 1000 * 60; +const c1_HOUR = 1000 * 60 * 60; const MANUFACTURER_ID = 0xff; const MANUFACTURER_DATA = [12, 23, 56]; @@ -142,94 +142,30 @@ function loadLastUUIDAndBroadcast() { function broadcast(currentUUID) { if (!currentUUID) return; // does not allow to start without UUID - // Do not run on iOS for now. - if (Platform.OS === 'android') { - //console.log('[Bluetooth]', nowStr(), currentUUID, 'Broadcasting'); - AndroidBLEAdvertiserModule.setCompanyId(MANUFACTURER_ID); - AndroidBLEAdvertiserModule.broadcast(currentUUID, MANUFACTURER_DATA) - .then(success => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Broadcasting Sucessful', - success, - ), - ) - .catch(error => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Broadcasting Error', - error, - ), - ); + //console.log('[Bluetooth]', nowStr(), currentUUID, 'Broadcasting'); + AndroidBLEAdvertiserModule.setCompanyId(MANUFACTURER_ID); + AndroidBLEAdvertiserModule.broadcast(currentUUID, MANUFACTURER_DATA) + .then(success => console.log('[Bluetooth]', nowStr(), currentUUID, 'Broadcasting Sucessful', success)) + .catch(error => console.log('[Bluetooth]', nowStr(), currentUUID, 'Broadcasting Error', error)); - //console.log('[Bluetooth]', nowStr(), currentUUID, "Starting Scanner"); - AndroidBLEAdvertiserModule.scan(MANUFACTURER_DATA, {}) - .then(success => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Scan Successful', - success, - ), - ) - .catch(error => - console.log('[Bluetooth]', nowStr(), currentUUID, 'Scan Error', error), - ); - } + //console.log('[Bluetooth]', nowStr(), currentUUID, "Starting Scanner"); + AndroidBLEAdvertiserModule.scan(MANUFACTURER_DATA, {}) + .then(success => console.log('[Bluetooth]', nowStr(), currentUUID, "Scan Successful", success)) + .catch(error => console.log('[Bluetooth]', nowStr(), currentUUID, "Scan Error", error)); } function stopBroadcast(currentUUID) { if (!currentUUID) return; // does not allow to start without UUID - // Do not run on iOS for now. - if (Platform.OS === 'android') { - //console.log('[Bluetooth]', nowStr(), currentUUID, 'Stopping Broadcast'); - AndroidBLEAdvertiserModule.stopBroadcast() - .then(success => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Stop Broadcast Successful', - success, - ), - ) - .catch(error => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Stop Broadcast Error', - error, - ), - ); + //console.log('[Bluetooth]', nowStr(), currentUUID, 'Stopping Broadcast'); + AndroidBLEAdvertiserModule.stopBroadcast() + .then(success => console.log('[Bluetooth]', nowStr(), currentUUID, "Stop Broadcast Successful", success)) + .catch(error => console.log('[Bluetooth]', nowStr(), currentUUID, "Stop Broadcast Error", error)); - //console.log('[Bluetooth]', nowStr(), currentUUID, "Stopping Scanning"); - AndroidBLEAdvertiserModule.stopScan() - .then(success => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Stop Scan Successful', - success, - ), - ) - .catch(error => - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Stop Scan Error', - error, - ), - ); - } + //console.log('[Bluetooth]', nowStr(), currentUUID, "Stopping Scanning"); + AndroidBLEAdvertiserModule.stopScan() + .then(success => console.log('[Bluetooth]', nowStr(), currentUUID, "Stop Scan Successful", success)) + .catch(error => console.log('[Bluetooth]', nowStr(), currentUUID, "Stop Scan Error", error)); } function generateNewUUIDAndBroadcast() { @@ -245,73 +181,67 @@ function generateNewUUIDAndBroadcast() { } export default class BroadcastingServices { - static isBTActive() { - if (AndroidBLEAdvertiserModule == undefined) { - return; - } - AndroidBLEAdvertiserModule.getAdapterState() - .then(result => { - console.log( - '[Bluetooth]', - nowStr(), - currentUUID, - 'Bluetooth State', - result, - ); - if (result != 12) { - setTimeout( - () => - Alert.alert( - 'Private Kit requires bluetooth to be enabled', - 'Would you like to enable Bluetooth?', - [ - { - text: 'Yes', - onPress: () => AndroidBLEAdvertiserModule.enableAdapter(), - }, - { - text: 'No', - onPress: () => console.log('No Pressed'), - style: 'cancel', - }, - ], - ), - 1000, - ); - return false; - } else { - return true; - } - }) - .catch(error => { - return false; - console.log('[Bluetooth]', nowStr(), currentUUID, 'BT Not Enabled'); - }); + static askBTActive() { + setTimeout(() => + Alert.alert( + 'Private Kit requires bluetooth to be enabled', + 'Would you like to enable Bluetooth?', + [ + { + text: 'Yes', + onPress: () => AndroidBLEAdvertiserModule.enableAdapter(), + }, + { + text: 'No', + onPress: () => console.log('User does not want to activate Bluetooth'), + style: 'cancel', + }, + ], + ), + 1000); } static start() { - const eventEmitter = new NativeEventEmitter( - NativeModules.AndroidBLEAdvertiserModule, - ); - onBTStatusChange = eventEmitter.addListener('onBTStatusChange', status => { - if (status.enabled) BroadcastingServices.startAndSetCallbacks(); - else BroadcastingServices.stopAndClearCallbacks(); - }); - - if (!BroadcastingServices.isBTActive()) return; + // Do not run on iOS for now. + if (Platform.OS === 'android') { + const eventEmitter = new NativeEventEmitter(NativeModules.AndroidBLEAdvertiserModule); + onBTStatusChange = eventEmitter.addListener('onBTStatusChange', (status) => { + console.log('[Bluetooth]', nowStr(), currentUUID, 'Bluetooth Status Change', status); + if (status.enabled) + BroadcastingServices.startAndSetCallbacks(); + else + BroadcastingServices.stopAndClearCallbacks(); + }); - BroadcastingServices.startAndSetCallbacks(); + AndroidBLEAdvertiserModule.getAdapterState().then(result => { + console.log('[Bluetooth]', nowStr(), currentUUID, "isBTActive", result) + if (result === "STATE_ON") { + BroadcastingServices.startAndSetCallbacks(); + } else { + BroadcastingServices.askBTActive() + } + }).catch(error => { + console.log('[Bluetooth]', nowStr(), currentUUID, "BT Not Enabled") + }); + } } static stop() { - if (onBTStatusChange) { - onBTStatusChange.remove(); - onBTStatusChange = null; - } - - if (!BroadcastingServices.isBTActive()) return; + if (Platform.OS === 'android') { + if (onBTStatusChange) { + onBTStatusChange.remove(); + onBTStatusChange = null; + } - BroadcastingServices.stopAndClearCallbacks(); + AndroidBLEAdvertiserModule.getAdapterState().then(result => { + console.log('[Bluetooth]', nowStr(), currentUUID, "isBTActive", result) + if (result === "STATE_ON") { + BroadcastingServices.stopAndClearCallbacks(); + } + }).catch(error => { + console.log('[Bluetooth]', nowStr(), currentUUID, "BT Not Enabled") + }); + } } static startAndSetCallbacks() { @@ -320,14 +250,12 @@ export default class BroadcastingServices { BroadcastingServices.stopAndClearCallbacks(); } - // listening event. - const eventEmitter = new NativeEventEmitter( - NativeModules.AndroidBLEAdvertiserModule, - ); - onDeviceFound = eventEmitter.addListener('onDeviceFound', event => { - //console.log('[Bluetooth]', nowStr(), currentUUID, 'New Device', event); - if (event.serviceUuids && event.serviceUuids.length > 0) - saveContact({ uuid: event.serviceUuids[0] }); + // listening event. + const eventEmitter = new NativeEventEmitter(NativeModules.AndroidBLEAdvertiserModule); + onDeviceFound = eventEmitter.addListener('onDeviceFound', (event) => { + console.log('[Bluetooth]', nowStr(), currentUUID, 'New Device', event); + if (event.serviceUuids && event.serviceUuids.length > 0) + saveContact({ uuid: event.serviceUuids[0]}); }); // Get a Valid UUID and start broadcasting and scanning. diff --git a/package.json b/package.json index ba8608883e..0852e5273a 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "react-native": "0.61.5", "react-native-app-intro-slider": "^3.0.0", "react-native-background-timer": "^2.2.0", - "react-native-ble-advertiser": "0.0.9", + "react-native-ble-advertiser": "0.0.10", "react-native-extended-stylesheet": "^0.12.0", "react-native-fs": "^2.16.6", "react-native-gesture-handler": "~1.5.0",