Skip to content

Commit

Permalink
Roughly implemented the bluetooth scanner into the bluetooth page. Cr…
Browse files Browse the repository at this point in the history
…eated bluetoothScanner.ts which houses the plugin scan request. I then added in functionality to the BluetoothScanPage to output the data that we get from the scan function.
  • Loading branch information
louisg1337 committed Feb 10, 2024
1 parent 4dc111f commit 2cf7882
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
33 changes: 32 additions & 1 deletion www/js/bluetooth/BluetoothScanPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useContext } from 'react';
import React, { useContext, useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { AppContext } from '../App';
import { StyleSheet, Text, Modal, ScrollView, SafeAreaView, Pressable } from 'react-native';
import { gatherData } from './blueoothScanner';

/**
* The implementation of this scanner page follows the design of
Expand All @@ -13,6 +14,23 @@ import { StyleSheet, Text, Modal, ScrollView, SafeAreaView, Pressable } from 're
const BluetoothScanPage = ({ ...props }: any) => {
const { t } = useTranslation();
const context = useContext(AppContext); // May not be necessary
const [logs, setLogs] = useState<string[]>([]);

useEffect(() => {

}, []);

// Function to run Bluetooth test and update logs
const runBluetoothTest = async () => {
try {
setLogs(["Loading..."]);
const newLogs = await gatherData();
setLogs(newLogs);
} catch (error) {
console.error(error);
// Handle error
}
};

const BlueScanContent = (
<div style={{ height: '100%' }}>
Expand All @@ -28,6 +46,16 @@ const BluetoothScanPage = ({ ...props }: any) => {
}
</header>
<Text>{'Add Scanner Components here!'}</Text>
<button style={s.btn} onClick={() => window['cordova'].plugins.BEMDataCollection.bluetoothScanPermissions()}>
<Text>{'Permissions'}</Text>
</button>
<button style={s.btn} onClick={runBluetoothTest}>
<Text>{'Scan'}</Text>
</button>
<h3>Console Output:</h3>
{logs.map((log, index) => (
<p key={index}>{log}</p>
))}
</div>
);

Expand Down Expand Up @@ -56,6 +84,9 @@ const s = StyleSheet.create({
alignItems: 'center',
padding: 0,
},
btn: {
display: 'flex'
}
});

export default BluetoothScanPage;
23 changes: 23 additions & 0 deletions www/js/bluetooth/blueoothScanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function gatherData(): Promise<string[]> {
return new Promise((resolve, reject) => {
let logs: string[] = [];

window['bluetoothSerial'].discoverUnpaired(
(devices) => {
logs.push("Successfully scanned, results...");
devices.forEach(function (device) {
logs.push("ID: " + device.id + " Name: " + device.name);
});
resolve(logs);
},
(failure) => {
logs.push("Failed!");
logs.push("ERROR: " + failure);
console.debug("ERROR: " + failure);
reject(new Error(failure));
}
);
});
}

export { gatherData };

0 comments on commit 2cf7882

Please sign in to comment.