feat(companion): implement ble

This commit is contained in:
2026-01-12 22:24:33 +00:00
parent 8305726f83
commit 75cfbe8dd4
55 changed files with 3457 additions and 142 deletions

View File

@@ -0,0 +1,61 @@
import type { EventSubscription } from "expo-modules-core";
import { create } from "zustand";
import { fullFeedFixture } from "@/lib/ble/fixtures";
import { Ble, defaultBleState } from "@aris/ble";
import type { BleStatePayload } from "@aris/ble";
export type BleStoreState = BleStatePayload & {
isSupported: boolean;
isReady: boolean;
initialize: () => void;
setAdvertisingEnabled: (enabled: boolean) => void;
setWifiRequested: (requested: boolean) => void;
sendFixtureFeedNow: () => void;
};
let subscription: EventSubscription | null = null;
const noopSubscription: EventSubscription = {
remove: () => undefined,
};
export const useBleStore = create<BleStoreState>((set, get) => ({
...defaultBleState,
isSupported: Ble.isSupported,
isReady: false,
initialize: () => {
if (!Ble.isSupported || get().isReady) {
return;
}
const state = Ble.getState();
set({ ...state, isReady: true });
subscription?.remove();
subscription =
Ble.addStateListener?.((nextState) => {
set({ ...nextState });
}) ?? noopSubscription;
Ble.start();
},
setAdvertisingEnabled: (enabled) => {
if (!Ble.isSupported) {
return;
}
set({ advertisingEnabled: enabled });
Ble.setAdvertisingEnabled(enabled);
},
setWifiRequested: (requested) => {
if (!Ble.isSupported) {
return;
}
set({ wifiRequested: requested });
Ble.setWifiRequested(requested);
},
sendFixtureFeedNow: () => {
if (!Ble.isSupported) {
return;
}
const payload = JSON.stringify(fullFeedFixture);
Ble.sendOpaque(payload, 1);
},
}));

View File

@@ -0,0 +1,15 @@
import { create } from 'zustand';
export interface BearState {
bears: number;
increasePopulation: () => void;
removeAllBears: () => void;
updateBears: (newBears: number) => void;
}
export const useStore = create<BearState>((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 }),
updateBears: (newBears) => set({ bears: newBears }),
}));