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

Bluetooth Broadcasting on Android. #193

Merged
merged 7 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Android and iOS build on MacOS

on:
push:
branches: [ master ]
branches: [ develop ]
pull_request:
branches: [ master ]
branches: [ develop ]

jobs:
build:
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:name=".MainApplication"
android:label="@string/app_name"
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
compileSdkVersion = 28
buildToolsVersion = "28.0.3"
targetSdkVersion = 28
minSdkVersion = 16
minSdkVersion = 23
supportLibVersion = "28.0.0"
googlePlayServicesVersion = "11+"
firebaseVersion = "11+"
Expand Down
125 changes: 125 additions & 0 deletions app/services/BroadcastingService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import {
GetStoreData,
SetStoreData
} from '../helpers/General';

import BackgroundTimer from 'react-native-background-timer';
import UUIDGenerator from 'react-native-uuid-generator';
import Moment from 'moment';

import AndroidBLEAdvertiserModule from 'react-native-ble-advertiser'

var currentUUID = null;

function saveContact(contact) {
// Persist this contact data in our local storage of time/lat/lon values

GetStoreData('CONTACT_DATA')
.then(contactArrayString => {

var contactArray;
if (contactArrayString !== null) {
contactArray = JSON.parse(contactArrayString);
} else {
contactArray = [];
}

// Always work in UTC, not the local time in the contactData
var nowUTC = new Date().toISOString();
var unixtimeUTC = Date.parse(nowUTC);
var unixtimeUTC_28daysAgo = unixtimeUTC - (60 * 60 * 24 * 1000 * 28);

// Save the contact using the current lat-lon and the
// calculated UTC time (maybe a few milliseconds off from
// when the GPS data was collected, but that's unimportant
// for what we are doing.)
console.log('[GPS] Saving point:', contactArray.length);
var lat_lon_time = {
"uuid": contact["uuid"],
"time": unixtimeUTC
};
contactArray.push(lat_lon_time);

SetStoreData('CONTACT_DATA', contactArray);
});
}

function saveMyUUID(me) {
// Persist this contact data in our local storage of time/lat/lon values

GetStoreData('MY_UUIDs')
.then(myUUIDArrayString => {
var myUUIDArray;
if (myUUIDArrayString !== null) {
myUUIDArray = JSON.parse(myUUIDArrayString);
} else {
myUUIDArray = [];
}

// Always work in UTC, not the local time in the contactData
var nowUTC = new Date().toISOString();
var unixtimeUTC = Date.parse(nowUTC);
var unixtimeUTC_28daysAgo = unixtimeUTC - (60 * 60 * 24 * 1000 * 28);

var uuid_time = {
"uuid": me["uuid"],
"time": unixtimeUTC
};
console.log('[GPS] Saving myUUID:', Moment(unixtimeUTC).format('MMM Do, H:mma'), me["uuid"], myUUIDArray.length);
myUUIDArray.push(uuid_time);

SetStoreData('MY_UUIDs', myUUIDArray);
});
}

function loadLastUUIDAndBroadcast() {
GetStoreData('MY_UUIDs')
.then(myUUIDArrayString => {
var myUUIDArray;
if (myUUIDArrayString !== null) {
myUUIDArray = JSON.parse(myUUIDArrayString);
console.log("Loading last uuid ", myUUIDArray[myUUIDArray.length-1].uuid);
currentUUID = myUUIDArray[myUUIDArray.length-1].uuid;
broadcast();
} else {
generateNewUUIDAndBroadcast();
}
});
}

function broadcast() {
// Do not run on iOS for now.
if (Platform.OS === 'android') {
console.log("Broadcasting: ", currentUUID);
AndroidBLEAdvertiserModule.setCompanyId(0xFF);
AndroidBLEAdvertiserModule.broadcastPacket(currentUUID, [12,23,56])
.then((sucess) => {
console.log("Broadcasting Sucessful", sucess);
}).catch(error => console.log("Broadcasting Error", error));
}
}

function generateNewUUIDAndBroadcast() {
UUIDGenerator.getRandomUUID((uuid) => {
currentUUID = uuid;
saveMyUUID({'uuid':uuid});
broadcast();
});
}

export default class BroadcastingServices {
static start() {
loadLastUUIDAndBroadcast();

BackgroundTimer.runBackgroundTimer(() => {
generateNewUUIDAndBroadcast();
}, 1000 * 60 * 60); // Every hour, change UUID

console.log("Starting Bluetooth");
}

static stop(nav) {
console.log("Stopping Bluetooth");
BackgroundTimer.stopBackgroundTimer();
}
}
13 changes: 11 additions & 2 deletions app/views/LocationTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from 'react-native-popup-menu';
import colors from '../constants/colors';
import LocationServices from '../services/LocationService';
import BroadcastingServices from '../services/BroadcastingService';
import BackgroundGeolocation from '@mauron85/react-native-background-geolocation';
import exportImage from './../assets/images/export.png';
import news from './../assets/images/newspaper.png';
Expand Down Expand Up @@ -79,7 +80,10 @@ class LocationTracking extends Component {
}

willParticipate = () => {
SetStoreData('PARTICIPATE', 'true').then(() => LocationServices.start());
SetStoreData('PARTICIPATE', 'true').then(() => {
LocationServices.start();
BroadcastingServices.start();
});

// Check and see if they actually authorized in the system dialog.
// If not, stop services and set the state to !isLogging
Expand All @@ -91,6 +95,7 @@ class LocationTracking extends Component {
});
} else if (authorization === BackgroundGeolocation.NOT_AUTHORIZED) {
LocationServices.stop(this.props.navigation);
BroadcastingServices.stop(this.props.navigation);
this.setState({
isLogging: false,
});
Expand All @@ -107,14 +112,18 @@ class LocationTracking extends Component {
}

willParticipate = () => {
SetStoreData('PARTICIPATE', 'true').then(() => LocationServices.start());
SetStoreData('PARTICIPATE', 'true').then(() => {
LocationServices.start();
BroadcastingServices.start();
});
this.setState({
isLogging: true,
});
};

setOptOut = () => {
LocationServices.stop(this.props.navigation);
BroadcastingServices.stop(this.props.navigation);
this.setState({
isLogging: false,
});
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
"@react-navigation/stack": "5.1.1",
"i18next": "^19.3.3",
"lodash": "4.17.15",
"moment": "^2.24.0",
"patch-package": "^6.2.1",
"postinstall-postinstall": "^2.0.0",
"react": "16.9.0",
"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.4",
"react-native-extended-stylesheet": "^0.12.0",
"react-native-fs": "^2.16.6",
"react-native-gesture-handler": "~1.5.0",
Expand All @@ -40,6 +43,7 @@
"react-native-safe-area-context": "0.6.0",
"react-native-screens": "2.0.0-alpha.12",
"react-native-share": "^3.1.0",
"react-native-uuid-generator": "^6.1.1",
"react-native-webview": "^8.1.2",
"react-native-zip-archive": "^5.0.1",
"rn-fetch-blob": "^0.12.0"
Expand Down