Skip to content

Commit

Permalink
✅ Display the result of the callback in the list + test button
Browse files Browse the repository at this point in the history
We don't know what the result will look like when there are multiple beacons with the same UUID, so we just stringify and display the result.
We cannot test this without having an actual BLE beacon, so I have added a "fake callback button to simulate a callback"

Changes:
- stringify the result in `didDetermineStateForRegion` and pass it in to
  `setRangeStatus`
- Change the BluetoothCard to display the `device.result` as Card.Content
- Add a new button to fake a callback for a device by getting the delegate and
  invoking the `didDetermineStateForRegion` method on it with the device
- Remove the existing result in the device before invoking the callback to
  avoid nested results

Testing done:
e-mission/e-mission-docs#1062 (comment)
  • Loading branch information
shankari committed Mar 29, 2024
1 parent 58b2b0a commit 29e434c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
23 changes: 22 additions & 1 deletion www/js/bluetooth/BluetoothCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Card, List, useTheme } from 'react-native-paper';
import { Card, List, Text, Button, useTheme } from 'react-native-paper';
import { StyleSheet } from 'react-native';

type Props = any;
Expand All @@ -23,6 +23,19 @@ const BluetoothCard = ({ device, isClassic, isScanningBLE }: Props) => {
bgColor = device.in_range ? `rgba(200,250,200,1)` : `rgba(250,200,200,1)`;
}

async function fakeCallback() {
// If we don't do this, the results start accumulating in the device object
// first call, we put a result into the device
// second call, the device already has a result, so we put another one in...
const deviceWithoutResult = {...device};
deviceWithoutResult.result = undefined;
window['cordova'].plugins.locationManager.getDelegate().didDetermineStateForRegion({
region: deviceWithoutResult,
eventType: "didDetermineStateForRegion",
state: "CLRegionStateInside"
});
}

return (
<Card style={{ backgroundColor: bgColor, ...cardStyles.card }}>
<Card.Title
Expand All @@ -31,6 +44,14 @@ const BluetoothCard = ({ device, isClassic, isScanningBLE }: Props) => {
subtitle={`Configured major ${device.major} and minor ${device.minor}`} // e.g.,
left={() => <List.Icon icon={device.in_range ? 'access-point' : 'access-point-off'} />}
/>
<Card.Content>
<Text style={{ borderColor: colors.primary }} variant="bodyMedium">{device.result}</Text>
<Button
mode="elevated"
onPress={fakeCallback}>
Fake callback
</Button>
</Card.Content>
</Card>
);
};
Expand Down
14 changes: 8 additions & 6 deletions www/js/bluetooth/BluetoothScanPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ const BluetoothScanPage = ({ ...props }: any) => {
}
}

function setRangeStatus(uuid: string, status: boolean) {
function setRangeStatus(uuid: string, result: string, status: boolean) {
setSampleBLEDevices((prevDevices) => ({
...prevDevices,
[uuid]: {
...prevDevices[uuid],
result: result,
in_range: status,
},
}));
Expand All @@ -104,16 +105,17 @@ const BluetoothScanPage = ({ ...props }: any) => {
delegate.didDetermineStateForRegion = function (pluginResult: BLEPluginCallback) {
// `stateInside`is returned when the user enters the beacon region
// `StateOutside` is either (i) left region, or (ii) started scanner (outside region)
const pluginResultStr = JSON.stringify(pluginResult, null, 2);
if (pluginResult.state == 'CLRegionStateInside') {
// need toUpperCase(), b/c callback returns with only lowercase values...
setRangeStatus(pluginResult.region.uuid.toUpperCase(), true);
setRangeStatus(pluginResult.region.uuid.toUpperCase(), pluginResultStr, true);
} else if (pluginResult.state == 'CLRegionStateOutside') {
setRangeStatus(pluginResult.region.uuid.toUpperCase(), false);
setRangeStatus(pluginResult.region.uuid.toUpperCase(), pluginResultStr, false);
}
logDebug('[BLE] didDetermineStateForRegion');
logDebug(JSON.stringify(pluginResult, null, 2));
logDebug(pluginResultStr);
window['cordova'].plugins.locationManager.appendToDeviceLog(
'[DOM] didDetermineStateForRegion: ' + JSON.stringify(pluginResult, null, 2),
'[DOM] didDetermineStateForRegion: ' + pluginResultStr,
);
};

Expand Down Expand Up @@ -287,7 +289,7 @@ const BluetoothScanPage = ({ ...props }: any) => {
<TextInput
label="New UUID (mandatory)"
value={newUUID || ''}
onChangeText={(t) => setNewUUID(t)}
onChangeText={(t) => setNewUUID(t.toUpperCase())}
/>
<TextInput
label="Major (optional)"
Expand Down
2 changes: 1 addition & 1 deletion www/js/types/BluetoothDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type BLEBeaconDevice = {
type_name?: string; // e.g., "BeaconRegion"; used for callback
};
export type BLEDeviceList = {
[key: string]: { identifier: string; minor: number; major: number; in_range: boolean };
[key: string]: { identifier: string; minor: number; major: number; result: string, in_range: boolean };
};

export type BLEPluginCallback = {
Expand Down

0 comments on commit 29e434c

Please sign in to comment.