Skip to content

Commit

Permalink
feat: support connecting to BLE devices from the app
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Mar 16, 2024
1 parent 5b0d650 commit 800e791
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
18 changes: 13 additions & 5 deletions manager-ui/src/screens/bluetoothSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@ import { DiscoveredDevice } from '../types/soundcore-lib';
import BluetoothIcon from '@mui/icons-material/Bluetooth';

export const BluetoothSearchScreen: React.FC = () => {
const { isLoading, startScan, latestScanResults } = useSoundcoreStore(
const { isLoading, startScan, latestScanResults, connectDevice } = useSoundcoreStore(
useShallow((state) => ({
isLoading: state.isLoading,
startScan: state.startScan,
latestScanResults: state.latestScan
latestScanResults: state.latestScan,
connectDevice: state.connectDevice
}))
);

const [selectedDevice, setSelectedDevice] = React.useState<DiscoveredDevice>();
const [selectedDevice, setSelectedDevice] = React.useState<DiscoveredDevice | null>(null);

const connectFabClick = () => {
console.log(selectedDevice);
if (!selectedDevice) return;
connectDevice(selectedDevice);
};

const searchFabClick = () => {
startScan();
setSelectedDevice(null);
};

// useEffect(() => {
Expand Down Expand Up @@ -68,12 +75,13 @@ export const BluetoothSearchScreen: React.FC = () => {
size="medium"
color="primary"
aria-label="add"
disabled={!selectedDevice}
sx={{ position: 'absolute', bottom: 16, right: 16 }}>
Connect
<ArrowForwardIcon sx={{ ml: 1 }} />
</Fab>
<Fab
onClick={() => startScan()}
onClick={() => searchFabClick()}
variant="extended"
size="medium"
color="primary"
Expand Down
10 changes: 10 additions & 0 deletions manager-ui/src/stores/bluetoothSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export const createBluetoothSlice: StateCreator<BluetoothSlice> = (set, _get) =>
console.error(`Could not start scan. ${error}`);
set({ hasFailed: true });
});
},
connectDevice: (device: DiscoveredDevice) => {
useAsyncBridgeRequest({ command: 'connect', payload: device })
.then(() => {
set({ connectedDevices: [..._get().connectedDevices, device.descriptor.addr] });
})
.catch((error) => {
console.error(`Could not connect to device. ${error}`);
});
}
});

Expand All @@ -27,4 +36,5 @@ export interface BluetoothSlice {
connectedDevices: Array<BluetoothAdrr>;
setLatestScan: (scanRes: DiscoveredDevice[]) => void;
startScan: () => void;
connectDevice: (device: DiscoveredDevice) => void;
}
6 changes: 6 additions & 0 deletions soundcore-lib/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ impl<Connection> SoundcoreBLEDevice<Connection>
Ok(initial_state)
}

// TODO: Change the strategy to:
// 1. Send the state request packet
// 2. Check if the state is received within a certain time frame and retry if not
// 3. If the state is received, check if we have a SN
// 4. If not send a SN request packet
// 5. Resolve the state and the model
async fn fetch_initial_state(
connection: &Connection,
byte_channel: &mut mpsc::Receiver<Vec<u8>>,
Expand Down

0 comments on commit 800e791

Please sign in to comment.