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