import React from "react" import useBleStore from "./ExpoBleStore" import { BleProviderConfig } from "./ExpoBle.types" import { ExpoBleConnector } from "./ExpoBleConnector" // import { BluetoothStateManager } from "react-native-bluetooth-state-manager" const INITIAL_CONFIG: BleProviderConfig = { autoScan: true, showNamedOnly: true, } /** * BleProvider sets up a singleton BLE dataflow subscription for the app. * Place this at the root of your component tree (e.g. in _layout.tsx or App.tsx). */ export const BleProvider: React.FC<{ children: React.ReactNode; config?: BleProviderConfig }> = ({ children, config }) => { const setTrigger = useBleStore((state) => state.setTrigger) const setShowNamedOnly = useBleStore((state) => state.setShowNamedOnly) const resetConnections = useBleStore((state) => state.resetConnections) // Merge config with INITIAL_CONFIG for fallback const mergedConfig = { ...INITIAL_CONFIG, ...config } React.useEffect(() => { // BluetoothStateManager.requestToEnable() const subscription = ExpoBleConnector.onStateChange((state) => { if (state === "PoweredOn") { ExpoBleConnector.startScan() // Bluetooth is on, do nothing } else { resetConnections() } }) return () => { subscription.remove() } }, []) React.useEffect(() => { if (mergedConfig.autoScan) { ExpoBleConnector.startScan() } if (typeof mergedConfig.showNamedOnly === "boolean") { setShowNamedOnly(mergedConfig.showNamedOnly) } }, [mergedConfig.autoScan, mergedConfig.showNamedOnly, setShowNamedOnly]) React.useEffect(() => { const unsubscribe = ExpoBleConnector.startDataFlow( (v: any) => { if (typeof v === "string" && v.startsWith("trigger_")) { setTrigger(v === "trigger_pushed") ExpoBleConnector.startRFIDTrigger(v === "trigger_pushed") } }, ["notification"] ) return () => { unsubscribe() } }, [setTrigger]) return <>{children} } export default BleProvider