Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Bluetooth Contact Tracing V1. #279

Merged
merged 21 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
46ab412
Running build on develop.
vitorpamplona Mar 24, 2020
1c74427
Keeping action to Master
vitorpamplona Mar 24, 2020
e5c1c55
Starting Bluetooth Beacon Broadcasting.
vitorpamplona Mar 24, 2020
02a8621
Merge remote-tracking branch 'upstream/develop' into develop
vitorpamplona Mar 24, 2020
32ffe54
Other ID was too big.
vitorpamplona Mar 24, 2020
56e46a3
Only runs Bluetooth on Android
vitorpamplona Mar 24, 2020
d60f31b
Changing Package version to 0.0.4 to pass tests on Phones without Blu…
vitorpamplona Mar 25, 2020
097f3bf
Merge remote-tracking branch 'upstream/develop' into develop
vitorpamplona Mar 25, 2020
be3713a
0.5.7
vitorpamplona Mar 25, 2020
94556fb
Merge remote-tracking branch 'upstream/develop' into develop
vitorpamplona Mar 25, 2020
3f59309
0.5.8
vitorpamplona Mar 25, 2020
cdf68ee
Merge remote-tracking branch 'upstream/develop' into develop
vitorpamplona Mar 26, 2020
7e127b6
Migrating Bluetooth to 0.0.5
vitorpamplona Mar 26, 2020
e6a2cc6
Removing the tag for just build number increments on new versions.
vitorpamplona Mar 26, 2020
4c71c02
0.5.9
vitorpamplona Mar 26, 2020
6eb2148
Merge remote-tracking branch 'upstream/develop' into develop
vitorpamplona Mar 27, 2020
2b20a21
0.5.10
vitorpamplona Mar 27, 2020
c969c89
Saving contacts and Requesting permissions to turn bluetooth on
vitorpamplona Mar 29, 2020
e8018a3
Fully functioning version of Bluetooth on Android
vitorpamplona Mar 29, 2020
e482b67
Merge remote-tracking branch 'upstream/develop' into develop
vitorpamplona Mar 29, 2020
050e682
Merge branch 'develop' into develop
vitorpamplona Mar 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 77 additions & 149 deletions app/services/BroadcastingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down