-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1128 from the-bay-kay/bluetooth_scanner
☰♭ Adding a Bluetooth Scanner to dev options
- Loading branch information
Showing
10 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import { Card, List } from 'react-native-paper'; | ||
import { StyleSheet } from 'react-native'; | ||
|
||
type Props = any; | ||
const BluetoothCard = ({ device }: Props) => { | ||
return ( | ||
<Card style={cardStyles.card}> | ||
<Card.Title | ||
title={`Name: ${device.name}`} | ||
titleVariant="titleLarge" | ||
subtitle={`ID: ${device.id}`} | ||
left={() => <List.Icon icon={device.is_paired ? 'bluetooth' : 'bluetooth-off'} />} | ||
/> | ||
</Card> | ||
); | ||
}; | ||
|
||
export const cardStyles = StyleSheet.create({ | ||
card: { | ||
position: 'relative', | ||
alignSelf: 'center', | ||
marginVertical: 10, | ||
}, | ||
cardContent: { | ||
flex: 1, | ||
width: '100%', | ||
}, | ||
}); | ||
|
||
export default BluetoothCard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React, { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { StyleSheet, Modal, ScrollView, SafeAreaView, View } from 'react-native'; | ||
import gatherBluetoothData from './bluetoothScanner'; | ||
import { logWarn, displayError, displayErrorMsg } from '../plugin/logger'; | ||
import BluetoothCard from './BluetoothCard'; | ||
import { Appbar, useTheme, Button } from 'react-native-paper'; | ||
|
||
/** | ||
* The implementation of this scanner page follows the design of | ||
* `www/js/survey/enketo/EnketoModal.tsx`! | ||
* | ||
* Future work may include refractoring these files to be implementations of a | ||
* single base "pop-up page" component | ||
*/ | ||
|
||
const BluetoothScanPage = ({ ...props }: any) => { | ||
const { t } = useTranslation(); | ||
const [logs, setLogs] = useState<string[]>([]); | ||
const [isScanning, setIsScanning] = useState(false); | ||
const { colors } = useTheme(); | ||
|
||
// Function to run Bluetooth Classic test and update logs | ||
const runBluetoothTest = async () => { | ||
// Classic not currently supported on iOS | ||
if (window['cordova'].platformId == 'ios') { | ||
displayErrorMsg('Sorry, iOS is not supported!', 'OSError'); | ||
return; | ||
} | ||
|
||
try { | ||
let response = await window['cordova'].plugins.BEMDataCollection.bluetoothScanPermissions(); | ||
if (response != 'OK') { | ||
displayErrorMsg('Please Enable Bluetooth!', 'Insufficient Permissions'); | ||
return; | ||
} | ||
} catch (e) { | ||
displayError(e, 'Insufficient Permissions'); | ||
return; | ||
} | ||
|
||
try { | ||
setIsScanning(true); | ||
const newLogs = await gatherBluetoothData(t); | ||
setLogs(newLogs); | ||
} catch (error) { | ||
logWarn(error); | ||
} finally { | ||
setIsScanning(false); | ||
} | ||
}; | ||
|
||
const BluetoothCardList = ({ devices }) => ( | ||
<div> | ||
{devices.map((device) => { | ||
if (device) { | ||
return <BluetoothCard device={device} />; | ||
} | ||
return null; | ||
})} | ||
</div> | ||
); | ||
|
||
const BlueScanContent = () => ( | ||
<div style={{ height: '100%' }}> | ||
<Appbar.Header | ||
statusBarHeight={0} | ||
elevated={true} | ||
style={{ height: 46, backgroundColor: colors.surface }}> | ||
<Appbar.BackAction | ||
onPress={() => { | ||
props.onDismiss?.(); | ||
}} | ||
/> | ||
<Appbar.Content title={t('bluetooth.scan-debug-title')} titleStyle={{ fontSize: 17 }} /> | ||
</Appbar.Header> | ||
<View style={s.btnContainer}> | ||
<Button mode="elevated" onPress={runBluetoothTest} textColor={colors.primary} style={s.btn}> | ||
{isScanning ? t('bluetooth.is-scanning') : t('bluetooth.scan-for-bluetooth')} | ||
</Button> | ||
</View> | ||
<BluetoothCardList devices={logs} /> | ||
</div> | ||
); | ||
|
||
return ( | ||
<> | ||
<Modal {...props} animationType="slide"> | ||
<SafeAreaView style={{ flex: 1 }}> | ||
<ScrollView style={{ flex: 1 }} contentContainerStyle={{ flex: 1 }}> | ||
<BlueScanContent /> | ||
</ScrollView> | ||
</SafeAreaView> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
const s = StyleSheet.create({ | ||
btnContainer: { | ||
padding: 16, | ||
justifyContent: 'center', | ||
}, | ||
btn: { | ||
height: 38, | ||
fontSize: 11, | ||
margin: 4, | ||
}, | ||
}); | ||
|
||
export default BluetoothScanPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { logDebug, displayError } from '../plugin/logger'; | ||
import { BluetoothClassicDevice } from '../types/bluetoothTypes'; | ||
|
||
/** | ||
* gatherBluetoothData scans for viewable Bluetooth Classic Devices | ||
* @param t is the i18next translation function | ||
* @returns an array of strings containing device data, formatted ['ID: id Name: name'] | ||
*/ | ||
export default function gatherBluetoothData(t): Promise<string[]> { | ||
return new Promise((resolve, reject) => { | ||
logDebug('Running bluetooth discovery test!'); | ||
|
||
// Device List "I/O" | ||
function updatePairingStatus(pairingType: boolean, devices: Array<BluetoothClassicDevice>) { | ||
devices.forEach((device) => { | ||
device.is_paired = pairingType; | ||
}); | ||
return devices; | ||
} | ||
|
||
// Plugin Calls | ||
const unpairedDevicesPromise = new Promise((res, rej) => { | ||
window['bluetoothClassicSerial'].discoverUnpaired( | ||
(devices: Array<BluetoothClassicDevice>) => { | ||
res(updatePairingStatus(false, devices)); | ||
}, | ||
(e: Error) => { | ||
displayError(e, 'Error'); | ||
rej(e); | ||
}, | ||
); | ||
}); | ||
|
||
const pairedDevicesPromise = new Promise((res, rej) => { | ||
window['bluetoothClassicSerial'].list( | ||
(devices: Array<BluetoothClassicDevice>) => { | ||
res(updatePairingStatus(true, devices)); | ||
}, | ||
(e: Error) => { | ||
displayError(e, 'Error'); | ||
rej(e); | ||
}, | ||
); | ||
}); | ||
|
||
Promise.all([unpairedDevicesPromise, pairedDevicesPromise]) | ||
.then((logs: Array<any>) => { | ||
resolve(logs.flat()); | ||
}) | ||
.catch((e) => { | ||
reject(e); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { useState } from 'react'; | ||
import SettingRow from './SettingRow'; | ||
import BluetoothScanPage from '../bluetooth/BluetoothScanPage'; | ||
|
||
const BluetoothScanSettingRow = ({}) => { | ||
const [bluePageVisible, setBluePageVisible] = useState<boolean>(false); | ||
|
||
async function openPopover() { | ||
setBluePageVisible(true); | ||
} | ||
|
||
return ( | ||
<> | ||
<SettingRow | ||
textKey="control.bluetooth-scan" | ||
iconName="bluetooth-settings" | ||
action={openPopover}></SettingRow> | ||
<BluetoothScanPage visible={bluePageVisible} onDismiss={() => setBluePageVisible(false)} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default BluetoothScanSettingRow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Device data, as defined in BluetoothClassicSerial's docs | ||
export type BluetoothClassicDevice = { | ||
class: number; | ||
id: string; | ||
address: string; | ||
name: string; | ||
is_paired?: boolean; // We keep track of this, BCS doesn't | ||
}; |