Skip to content

Commit

Permalink
Updates for Android 12 and general improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbravo94 committed Oct 19, 2022
1 parent 554e33c commit 6a3712f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 79 deletions.
3 changes: 3 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Expand Down
Empty file modified android/gradlew
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e
(cd android && ./gradlew clean assembleRelease)
echo "APK located under ./android/app/build/outputs/apk/release/app-release.apk"
75 changes: 44 additions & 31 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @flow strict-local
*/

import React, {useState, useEffect} from 'react';
import React, { useState, useEffect } from 'react';
import {
SafeAreaView,
StyleSheet,
Expand All @@ -23,16 +23,16 @@ import {
Alert,
LogBox,
} from 'react-native';
import {bytesToString} from 'convert-string';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import { bytesToString } from 'convert-string';
import { Colors } from 'react-native/Libraries/NewAppScreen';

import BleManager from 'react-native-ble-manager/BleManager';
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
import BluetoothStateManager from 'react-native-bluetooth-state-manager';
import RNBluetoothClassic from 'react-native-bluetooth-classic';
import GlucoseReadingRx from './GlucoseReadingRx';
import {getAllRecords} from './RecordsCmdTx';
import { getAllRecords } from './RecordsCmdTx';

// TODO Fix warning properly instead of hiding
LogBox.ignoreLogs(['new NativeEventEmitter']);
Expand Down Expand Up @@ -261,8 +261,39 @@ const App = () => {
Alert.alert('Guide', steps.join('\n'));
};

const requestPermission = async (permission) => {
const checkResult = await PermissionsAndroid.check(permission);

if (!checkResult) {
const requestResult = await PermissionsAndroid.request(permission);

if (requestResult) {
console.log('User accept');
} else {
console.log('User refuse');
}
}
}

const requestPermissions = async () => {
if (Platform.OS === 'android') {

if (Platform.Version >= 23) {
await requestPermission(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
}

if (Platform.Version >= 31) {
await requestPermission(PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN);
await requestPermission(PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE);
await requestPermission(PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT);
}
}
}

useEffect(() => {
BleManager.start({showAlert: false});
requestPermissions();

BleManager.start({ showAlert: false });

const subscriptions = [];

Expand Down Expand Up @@ -291,24 +322,6 @@ const App = () => {
),
);

if (Platform.OS === 'android' && Platform.Version >= 23) {
PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
).then(result => {
if (!result) {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
).then(result => {
if (result) {
console.log('User accept');
} else {
console.log('User refuse');
}
});
}
});
}

return () => {
subscriptions.forEach(subscription => subscription.remove());
};
Expand All @@ -320,7 +333,7 @@ const App = () => {
const color = item.connected ? 'green' : '#fff';
return (
<TouchableHighlight onPress={() => connectPeripheral(item)}>
<View style={[styles.row, {backgroundColor: color}]}>
<View style={[styles.row, { backgroundColor: color }]}>
<Text
style={{
fontSize: 12,
Expand Down Expand Up @@ -368,37 +381,37 @@ const App = () => {
</View>
)}
<View style={styles.body}>
<View style={{margin: 10}}>
<View style={{ margin: 10 }}>
<Button
title={'Scan Bluetooth (' + (isScanning ? 'on' : 'off') + ')'}
onPress={() => startScan()}
/>
</View>

<View style={{margin: 10}}>
<View style={{ margin: 10 }}>
<Button title={'Open Settings'} onPress={() => openSettings()} />
</View>

<View style={{margin: 10}}>
<View style={{ margin: 10 }}>
<Button title={'Info'} onPress={() => showInfo()} />
</View>

{connectedPeripheralId ? (
<View style={{margin: 10}}>
<View style={{ margin: 10 }}>
<Button title={'Read Bluetooth'} onPress={() => read()} />
</View>
) : null}

{list.length == 0 && (
<View style={{flex: 1, margin: 20}}>
<Text style={{textAlign: 'center'}}>No peripherals</Text>
<View style={{ flex: 1, margin: 20 }}>
<Text style={{ textAlign: 'center' }}>No peripherals</Text>
</View>
)}
</View>
</ScrollView>
<FlatList
data={list}
renderItem={({item}) => renderItem(item)}
renderItem={({ item }) => renderItem(item)}
keyExtractor={item => item.id}
/>
</SafeAreaView>
Expand Down
106 changes: 58 additions & 48 deletions src/GlucoseReadingRx.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
import {getShort, getSfloat16} from './DataTypeHelper';
import { getShort, getSfloat16 } from './DataTypeHelper';

// Based on https://github.com/NightscoutFoundation/xDrip/blob/c48ef307c6dd28f6f6027a775a2b82c89a5b281d/app/src/main/java/com/eveningoutpost/dexdrip/glucosemeter/GlucoseReadingRx.java#L14
export default class GlucoseReadingRx {
constructor(packet) {
if (packet.length >= 14) {
const data = packet;

// Commented because some glucose devices do not have this restriction
/*
if (packet.length < 14) {
return;
}
*/

const flags = data[0];
const timeOffsetPresent = (flags & 0x01) > 0;
const typeAndLocationPresent = (flags & 0x02) > 0;
const concentrationUnitKgL = (flags & 0x04) === 0;
const sensorStatusAnnunciationPresent = (flags & 0x08) > 0;
const contextInfoFollows = (flags & 0x10) > 0;
const data = packet;

this.sequence = getShort(data, 1);
this.year = getShort(data, 3);
const flags = data[0];
const timeOffsetPresent = (flags & 0x01) > 0;
const typeAndLocationPresent = (flags & 0x02) > 0;
const concentrationUnitKgL = (flags & 0x04) === 0;
const sensorStatusAnnunciationPresent = (flags & 0x08) > 0;
const contextInfoFollows = (flags & 0x10) > 0;

this.month = data[5];
this.day = data[6];
this.hour = data[7];
this.minute = data[8];
this.second = data[9];
this.sequence = getShort(data, 1);
this.year = getShort(data, 3);

let index = 10;
if (timeOffsetPresent) {
this.offset = getShort(data, index);
index += 2;
}
this.month = data[5];
this.day = data[6];
this.hour = data[7];
this.minute = data[8];
this.second = data[9];

if (concentrationUnitKgL) {
const kgl = getSfloat16(data, index);
this.mgdl = kgl * 100000;
} else {
const mol = getSfloat16(data, index);
const MMOLL_TO_MGDL = 18.0182;
this.mgdl = mol * 1000 * MMOLL_TO_MGDL;
}
let index = 10;
if (timeOffsetPresent) {
this.offset = getShort(data, index);
index += 2;
}

if (concentrationUnitKgL) {
const kgl = getSfloat16(data, index);
this.mgdl = kgl * 100000;
} else {
const mol = getSfloat16(data, index);
const MMOLL_TO_MGDL = 18.0182;
this.mgdl = mol * 1000 * MMOLL_TO_MGDL;
}

if (typeAndLocationPresent) {
const typeAndLocation = data[index];
this.sampleLocation = (typeAndLocation & 0xf0) >> 4;
this.sampleType = typeAndLocation & 0x0f;
index++;
}
//Applying JavaScript precision fix
this.mgdl = parseFloat(parseFloat(this.mgdl).toFixed(5));

if (sensorStatusAnnunciationPresent) {
this.status = data[index];
}
index += 2;

// TODO Implement offset
const date = new Date(
this.year,
this.month - 1,
this.day,
this.hour,
this.minute,
this.second,
);
if (typeAndLocationPresent) {
const typeAndLocation = data[index];
this.sampleLocation = (typeAndLocation & 0xf0) >> 4;
this.sampleType = typeAndLocation & 0x0f;
index++;
}

this.time = date.toLocaleString('de-DE', {timeZone: 'UTC'});
if (sensorStatusAnnunciationPresent) {
this.status = data[index];
}

// TODO Implement offset
const date = new Date(
this.year,
this.month - 1,
this.day,
this.hour,
this.minute,
this.second,
);

this.time = date.toLocaleString('de-DE', { timeZone: 'UTC' });
}

toString() {
Expand Down

0 comments on commit 6a3712f

Please sign in to comment.