|
| 1 | +import bridge from '@expressms/smartapp-bridge' |
| 2 | +import { |
| 3 | + BleDeviceCallback, |
| 4 | + BluetoothBleDeviceFoundEvent, |
| 5 | + BluetoothDiscoverGattServicesResponse, |
| 6 | + BluetoothScanBleDevicesResponse, |
| 7 | + BluetoothStatusResponse, |
| 8 | + ERROR_CODES, |
| 9 | + METHODS, |
| 10 | +} from '../../types' |
| 11 | + |
| 12 | +const deviceCallbacks: Array<BleDeviceCallback> = [] |
| 13 | + |
| 14 | +/** |
| 15 | + * Install bridge event listener for founded devices during scan |
| 16 | + */ |
| 17 | +const installBridgeEventListener = () => { |
| 18 | + if (!bridge) return |
| 19 | + |
| 20 | + bridge.onReceive(event => { |
| 21 | + if (event.type !== 'ble_device_found') return |
| 22 | + |
| 23 | + const { |
| 24 | + payload: { bleDevice }, |
| 25 | + } = event as BluetoothBleDeviceFoundEvent |
| 26 | + |
| 27 | + deviceCallbacks.map(cb => cb(bleDevice)) |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Add callback to list |
| 33 | + */ |
| 34 | +const addDeviceCallback = (callback?: BleDeviceCallback) => { |
| 35 | + if (callback) deviceCallbacks.push(callback) |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Remove callback from list |
| 40 | + */ |
| 41 | +const removeDeviceCallback = (callback?: BleDeviceCallback) => { |
| 42 | + if (!callback) return |
| 43 | + |
| 44 | + const index = deviceCallbacks.findIndex(cb => cb === callback) |
| 45 | + if (index !== -1) deviceCallbacks.splice(index, 1) |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Enable bluetooth |
| 50 | + * @returns Promise that'll be fullfilled on success, otherwise rejected with reason |
| 51 | + */ |
| 52 | +export const enable = (): Promise<BluetoothStatusResponse> => { |
| 53 | + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) |
| 54 | + |
| 55 | + return bridge |
| 56 | + .sendClientEvent({ |
| 57 | + method: METHODS.ENABLE_BLUETOOTH, |
| 58 | + params: {}, |
| 59 | + }) |
| 60 | + .then(event => event as BluetoothStatusResponse) |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Scan BLE devices nearby |
| 65 | + * @param scanDuration Duration to scan devices in milliseconds |
| 66 | + * @param deviceCallback Callback that triggered on device found |
| 67 | + * @returns Promise that'll be fullfilled with `payload.bleDevices` on success, otherwise rejected with reason |
| 68 | + */ |
| 69 | +export const scanBleDevices = ({ |
| 70 | + scanDuration, |
| 71 | + deviceCallback, |
| 72 | +}: { |
| 73 | + scanDuration: number |
| 74 | + deviceCallback?: BleDeviceCallback |
| 75 | +}): Promise<BluetoothScanBleDevicesResponse> => { |
| 76 | + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) |
| 77 | + |
| 78 | + addDeviceCallback(deviceCallback) |
| 79 | + |
| 80 | + return bridge |
| 81 | + .sendClientEvent({ |
| 82 | + method: METHODS.SCAN_BLE_DEVICES, |
| 83 | + params: { |
| 84 | + scanDuration, |
| 85 | + }, |
| 86 | + }) |
| 87 | + .then(event => { |
| 88 | + removeDeviceCallback(deviceCallback) |
| 89 | + return event as BluetoothScanBleDevicesResponse |
| 90 | + }) |
| 91 | + .catch(() => { |
| 92 | + removeDeviceCallback(deviceCallback) |
| 93 | + return Promise.reject() |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Connect bluetooth BLE device |
| 99 | + * @param bleDeviceAddress Address of the BLE device |
| 100 | + * @returns Promise that'll be fullfilled on success, otherwise rejected with reason |
| 101 | + */ |
| 102 | +export const connectBleDevice = ({ |
| 103 | + bleDeviceAddress, |
| 104 | +}: { |
| 105 | + bleDeviceAddress: string |
| 106 | +}): Promise<BluetoothStatusResponse> => { |
| 107 | + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) |
| 108 | + |
| 109 | + return bridge |
| 110 | + .sendClientEvent({ |
| 111 | + method: METHODS.CONNECT_BLE_DEVICE, |
| 112 | + params: { |
| 113 | + bleDeviceAddress, |
| 114 | + }, |
| 115 | + }) |
| 116 | + .then(event => event as BluetoothStatusResponse) |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Disonnect bluetooth BLE device |
| 121 | + * @param bleDeviceAddress Address of the BLE device |
| 122 | + * @returns Promise that'll be fullfilled on success, otherwise rejected with reason |
| 123 | + */ |
| 124 | +export const disconnectBleDevice = ({ |
| 125 | + bleDeviceAddress, |
| 126 | +}: { |
| 127 | + bleDeviceAddress: string |
| 128 | +}): Promise<BluetoothStatusResponse> => { |
| 129 | + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) |
| 130 | + |
| 131 | + return bridge |
| 132 | + .sendClientEvent({ |
| 133 | + method: METHODS.DISCONNECT_BLE_DEVICE, |
| 134 | + params: { |
| 135 | + bleDeviceAddress, |
| 136 | + }, |
| 137 | + }) |
| 138 | + .then(event => event as BluetoothStatusResponse) |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Discover services and characteristics of the BLE device |
| 143 | + * @param bleDeviceAddress Address of the BLE device |
| 144 | + * @returns Promise that'll be fullfilled with `payload.gattServices` on success, otherwise rejected with reason |
| 145 | + */ |
| 146 | +export const discoverGattServices = ({ |
| 147 | + bleDeviceAddress, |
| 148 | +}: { |
| 149 | + bleDeviceAddress: string |
| 150 | +}): Promise<BluetoothDiscoverGattServicesResponse> => { |
| 151 | + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) |
| 152 | + |
| 153 | + return bridge |
| 154 | + .sendClientEvent({ |
| 155 | + method: METHODS.DISCOVER_BLE_GATT_SERVICES, |
| 156 | + params: { |
| 157 | + bleDeviceAddress, |
| 158 | + }, |
| 159 | + }) |
| 160 | + .then(event => event as BluetoothDiscoverGattServicesResponse) |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Read BLE GATT characteristic |
| 165 | + * @param bleDeviceAddress Address of the BLE device |
| 166 | + * @param gattServiceUuid UUID of the GATT service |
| 167 | + * @param gattCharacteristicUuid UUID of the GATT characteristic |
| 168 | + * @returns Promise that'll be fullfilled with `payload.value` on success, otherwise rejected with reason |
| 169 | + */ |
| 170 | +export const readBleGattCharacteristic = ({ |
| 171 | + bleDeviceAddress, |
| 172 | + gattServiceUuid, |
| 173 | + gattCharacteristicUuid, |
| 174 | +}: { |
| 175 | + bleDeviceAddress: string, |
| 176 | + gattServiceUuid: string, |
| 177 | + gattCharacteristicUuid: string, |
| 178 | +}): Promise<BluetoothDiscoverGattServicesResponse> => { |
| 179 | + if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE) |
| 180 | + |
| 181 | + return bridge |
| 182 | + .sendClientEvent({ |
| 183 | + method: METHODS.READ_BLE_GATT_CHARACTERISTIC, |
| 184 | + params: { |
| 185 | + bleDeviceAddress, |
| 186 | + gattServiceUuid, |
| 187 | + gattCharacteristicUuid, |
| 188 | + }, |
| 189 | + }) |
| 190 | + .then(event => event as BluetoothDiscoverGattServicesResponse) |
| 191 | +} |
| 192 | + |
| 193 | +// Init device event listener |
| 194 | +installBridgeEventListener() |
0 commit comments