From a11b6c16c2ce56d50584208001d4ce3c500845cb Mon Sep 17 00:00:00 2001 From: Katie Rischpater <98350084+the-bay-kay@users.noreply.github.com> Date: Fri, 22 Mar 2024 19:35:10 -0700 Subject: [PATCH] Added scan stop, color to indicate beacon status - The color of beacon cards will now change depending on whether or not they are within range - Refactored scan button - Added ability to stop scanning for BLE Beacons --- www/i18n/en.json | 3 +- www/js/bluetooth/BluetoothCard.tsx | 13 +++- www/js/bluetooth/BluetoothScanPage.tsx | 101 +++++++++++++++++-------- 3 files changed, 81 insertions(+), 36 deletions(-) diff --git a/www/i18n/en.json b/www/i18n/en.json index c0a69a3ad..ffbfb8ea8 100644 --- a/www/i18n/en.json +++ b/www/i18n/en.json @@ -238,7 +238,8 @@ }, "scan": { "for-ble": "Scan for BLE Beacons", - "for-bluetooth": "Scan for Classic Devices" + "for-bluetooth": "Scan for Classic Devices", + "stop": "Stop Scanning" }, "is-scanning": "Scanning...", "device-info": { diff --git a/www/js/bluetooth/BluetoothCard.tsx b/www/js/bluetooth/BluetoothCard.tsx index 6db1d3823..4af8240c4 100644 --- a/www/js/bluetooth/BluetoothCard.tsx +++ b/www/js/bluetooth/BluetoothCard.tsx @@ -1,9 +1,10 @@ import React from 'react'; -import { Card, List } from 'react-native-paper'; +import { Card, List, useTheme } from 'react-native-paper'; import { StyleSheet } from 'react-native'; type Props = any; -const BluetoothCard = ({ device, isClassic }: Props) => { +const BluetoothCard = ({ device, isClassic, isScanningBLE }: Props) => { + const { colors } = useTheme(); if (isClassic) { return ( @@ -16,8 +17,14 @@ const BluetoothCard = ({ device, isClassic }: Props) => { ); } + + let bgColor = colors.onPrimary; // 'rgba(225,225,225,1)' + if (isScanningBLE) { + bgColor = device.in_range ? `rgba(200,250,200,1)` : `rgba(250,200,200,1)`; + } + return ( - + { in_range: false, }, }); - const [isScanningClassic, setIsScanningClassic] = useState(false); + const [isScanningBLE, setIsScanningBLE] = useState(false); const [isClassic, setIsClassic] = useState(false); const { colors } = useTheme(); @@ -49,8 +49,9 @@ const BluetoothScanPage = ({ ...props }: any) => { ...device, })); } + // Function to run Bluetooth Classic test and update logs - const runBluetoothClassicTest = async () => { + async function runBluetoothClassicTest() { // Classic not currently supported on iOS if (window['cordova'].platformId == 'ios') { displayErrorMsg('Sorry, iOS is not supported!', 'OSError'); @@ -77,23 +78,22 @@ const BluetoothScanPage = ({ ...props }: any) => { } finally { setIsScanningClassic(false); } - }; - - const runBLETest = async () => { - //await startBLEScanning(); - BeaconMonitor(); // Will combine BeaconMonitor & StartBLE Scanning, if possible - }; + } function setRangeStatus(uuid: string, status: boolean) { - setSampleBLEDevices((prevDevices) => { - const newList = prevDevices; - newList[uuid].in_range = status; - return newList; - }); + setSampleBLEDevices((prevDevices) => ({ + ...prevDevices, + [uuid]: { + ...prevDevices[uuid], + in_range: status, + }, + })); } // BLE LOGIC - const BeaconMonitor = () => { + async function startBeaconScanning() { + setIsScanningBLE(true); + let delegate = new window['cordova'].plugins.locationManager.Delegate(); delegate.didDetermineStateForRegion = function (pluginResult: BLEPluginCallback) { @@ -147,7 +147,27 @@ const BluetoothScanPage = ({ ...props }: any) => { }) .done(); }); - }; + } + + async function stopBeaconScanning() { + setIsScanningBLE(false); + + beaconsToArray().forEach((sampleBeacon: BLEBeaconDevice) => { + setRangeStatus(sampleBeacon.uuid, false); // "zero out" the beacons + const beaconRegion = new window['cordova'].plugins.locationManager.BeaconRegion( + sampleBeacon.identifier, + sampleBeacon.uuid, + sampleBeacon.major, + sampleBeacon.minor, + ); + window['cordova'].plugins.locationManager + .stopMonitoringForRegion(beaconRegion) + .fail(function (e) { + logWarn(e); + }) + .done(); + }); + } const switchMode = () => { setIsClassic(!isClassic); @@ -172,13 +192,43 @@ const BluetoothScanPage = ({ ...props }: any) => {
{beaconsAsArray.map((beacon) => { if (beacon) { - return ; + return ; } })}
); }; + const ScanButton = () => { + if (isClassic) { + return ( + + + + ); + } + // else, if BLE + return ( + + + + ); + }; + const BlueScanContent = () => (
{ - - - +
);