|
| 1 | +import { BleManager } from 'react-native-ble-plx' |
| 2 | +import { noop } from '@mpxjs/utils' |
| 3 | +import { Platform, PermissionsAndroid } from 'react-native' |
| 4 | +import { base64ToArrayBuffer } from '../base/index' |
| 5 | +import { envError } from '../../../common/js' |
| 6 | + |
| 7 | +let manager |
| 8 | +let deviceFoundCallbacks = [] |
| 9 | +let discovering = false |
| 10 | + |
| 11 | +const requestBluetoothPermission = async () => { |
| 12 | + if (__mpx_mode__ === 'ios') { |
| 13 | + return true |
| 14 | + } |
| 15 | + if (__mpx_mode__ === 'android' && PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) { |
| 16 | + const apiLevel = parseInt(Platform.Version.toString(), 10) |
| 17 | + |
| 18 | + if (apiLevel < 31) { |
| 19 | + const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) |
| 20 | + return granted === PermissionsAndroid.RESULTS.GRANTED |
| 21 | + } |
| 22 | + if (PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN && PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT) { |
| 23 | + const result = await PermissionsAndroid.requestMultiple([ |
| 24 | + PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN, |
| 25 | + PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT, |
| 26 | + PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION |
| 27 | + ]) |
| 28 | + |
| 29 | + return ( |
| 30 | + result['android.permission.BLUETOOTH_CONNECT'] === PermissionsAndroid.RESULTS.GRANTED && |
| 31 | + result['android.permission.BLUETOOTH_SCAN'] === PermissionsAndroid.RESULTS.GRANTED && |
| 32 | + result['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED |
| 33 | + ) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return false |
| 38 | +} |
| 39 | + |
| 40 | +function openBluetoothAdapter (options = {}) { |
| 41 | + const { success = noop, fail = noop, complete = noop } = options |
| 42 | + if (manager) { |
| 43 | + fail({ |
| 44 | + errMsg: 'openBluetoothAdapter:fail already opened' |
| 45 | + }) |
| 46 | + complete({ |
| 47 | + errMsg: 'openBluetoothAdapter:fail already opened' |
| 48 | + }) |
| 49 | + return |
| 50 | + } |
| 51 | + manager = new BleManager() |
| 52 | + const checkState = function () { |
| 53 | + manager.state().then((newState) => { |
| 54 | + if (newState === 'PoweredOn') { |
| 55 | + const result = { |
| 56 | + errno: 0, |
| 57 | + errMsg: 'openBluetoothAdapter:ok' |
| 58 | + } |
| 59 | + success(result) |
| 60 | + complete(result) |
| 61 | + } else { |
| 62 | + manager = null |
| 63 | + if (__mpx_mode__ === 'ios') { |
| 64 | + let state = 0 |
| 65 | + switch (newState) { |
| 66 | + case 'Unknown': |
| 67 | + state = 0 |
| 68 | + break |
| 69 | + case 'Resetting': |
| 70 | + state = 1 |
| 71 | + break |
| 72 | + case 'Unsupported': |
| 73 | + state = 2 |
| 74 | + break |
| 75 | + case 'Unauthorized': |
| 76 | + state = 3 |
| 77 | + break |
| 78 | + case 'PoweredOff': |
| 79 | + state = 4 |
| 80 | + break |
| 81 | + } |
| 82 | + const result = { |
| 83 | + state, |
| 84 | + errMsg: 'openBluetoothAdapter:fail' |
| 85 | + } |
| 86 | + fail(result) |
| 87 | + complete(result) |
| 88 | + } else { |
| 89 | + fail && fail({ |
| 90 | + errMsg: 'openBluetoothAdapter:fail' |
| 91 | + }) |
| 92 | + complete && complete({ |
| 93 | + errMsg: 'openBluetoothAdapter:fail' |
| 94 | + }) |
| 95 | + } |
| 96 | + } |
| 97 | + }).catch((error) => { |
| 98 | + manager = null |
| 99 | + fail({ |
| 100 | + errMsg: 'openBluetoothAdapter:fail ' + error.message |
| 101 | + }) |
| 102 | + complete({ |
| 103 | + errMsg: 'openBluetoothAdapter:fail ' + error.message |
| 104 | + }) |
| 105 | + }) |
| 106 | + } |
| 107 | + // 先请求权限,再初始化蓝牙管理器 |
| 108 | + requestBluetoothPermission().then((hasPermissions) => { |
| 109 | + if (!hasPermissions) { |
| 110 | + const result = { |
| 111 | + errMsg: 'openBluetoothAdapter:fail no permission' |
| 112 | + } |
| 113 | + fail(result) |
| 114 | + complete(result) |
| 115 | + return |
| 116 | + } |
| 117 | + manager.connectedDevices().then((devices) => { |
| 118 | + if (devices.length > 0) { |
| 119 | + const result = { |
| 120 | + errno: -1, |
| 121 | + errMsg: 'openBluetoothAdapter:ok' |
| 122 | + } |
| 123 | + success(result) |
| 124 | + complete(result) |
| 125 | + return |
| 126 | + } |
| 127 | + checkState() |
| 128 | + }).catch(() => { |
| 129 | + checkState() |
| 130 | + }) |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +function closeBluetoothAdapter (options = {}) { |
| 135 | + const { success = noop, fail = noop, complete = noop } = options |
| 136 | + if (manager) { |
| 137 | + manager.destroy().then((res) => { |
| 138 | + deviceFoundCallbacks = [] |
| 139 | + manager = null |
| 140 | + const result = { |
| 141 | + errMsg: 'closeBluetoothAdapter:ok' |
| 142 | + } |
| 143 | + success(result) |
| 144 | + complete(result) |
| 145 | + }).catch((error) => { |
| 146 | + const result = { |
| 147 | + errMsg: 'closeBluetoothAdapter:fail ' + error.message |
| 148 | + } |
| 149 | + fail(result) |
| 150 | + complete(result) |
| 151 | + }) |
| 152 | + } else { |
| 153 | + const result = { |
| 154 | + errMsg: 'closeBluetoothAdapter:ok' |
| 155 | + } |
| 156 | + success(result) |
| 157 | + complete(result) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +function startBluetoothDevicesDiscovery (options = {}) { |
| 162 | + if (!manager) { |
| 163 | + return |
| 164 | + } |
| 165 | + const { |
| 166 | + services = [], |
| 167 | + allowDuplicatesKey = false, |
| 168 | + powerLevel = 'medium', |
| 169 | + success = noop, |
| 170 | + fail = noop, |
| 171 | + complete = noop |
| 172 | + } = options |
| 173 | + const scanMode = { |
| 174 | + low: 'LowPower', |
| 175 | + medium: 'Balanced', |
| 176 | + high: 'LowLatency' |
| 177 | + } |
| 178 | + manager.startDeviceScan(services, { |
| 179 | + scanMode: scanMode[powerLevel], |
| 180 | + allowDuplicates: allowDuplicatesKey |
| 181 | + }, (error, sannnedDevice) => { |
| 182 | + if (error) { |
| 183 | + return |
| 184 | + } |
| 185 | + discovering = true |
| 186 | + if (sannnedDevice) { |
| 187 | + deviceFoundCallbacks.forEach((callback) => { |
| 188 | + const result = { |
| 189 | + devices: [{ |
| 190 | + name: sannnedDevice.name || '', |
| 191 | + id: sannnedDevice.id, |
| 192 | + RSSI: sannnedDevice.rssi || 0, |
| 193 | + advertisData: base64ToArrayBuffer(sannnedDevice.manufacturerData || ''), |
| 194 | + advertisServiceUUIDs: sannnedDevice.serviceUUIDs || [], |
| 195 | + localName: sannnedDevice.localName || '', |
| 196 | + serviceData: sannnedDevice.serviceData, |
| 197 | + connectable: sannnedDevice.isConnectable |
| 198 | + }] |
| 199 | + } |
| 200 | + callback(result) |
| 201 | + }) |
| 202 | + } |
| 203 | + // if (sannnedDevice) { |
| 204 | + // const result = { |
| 205 | + // errMsg: 'startBluetoothDevicesDiscovery:ok', |
| 206 | + // isDiscovering: true |
| 207 | + // } |
| 208 | + // success(result) |
| 209 | + // complete(result) |
| 210 | + // } |
| 211 | + }).then(() => { |
| 212 | + const result = { |
| 213 | + errMsg: 'startBluetoothDevicesDiscovery:ok', |
| 214 | + isDiscovering: true |
| 215 | + } |
| 216 | + success(result) |
| 217 | + complete(result) |
| 218 | + }).catch((error) => { |
| 219 | + const result = { |
| 220 | + errMsg: 'startBluetoothDevicesDiscovery:fail ' + (error?.message || '') |
| 221 | + } |
| 222 | + fail(result) |
| 223 | + complete(result) |
| 224 | + }) |
| 225 | +} |
| 226 | + |
| 227 | +function stopBluetoothDevicesDiscovery (options = {}) { |
| 228 | + if (!manager) { |
| 229 | + return |
| 230 | + } |
| 231 | + const { success = noop, fail = noop, complete = noop } = options |
| 232 | + manager.stopDeviceScan().then(() => { |
| 233 | + discovering = false |
| 234 | + const result = { |
| 235 | + errMsg: 'stopBluetoothDevicesDiscovery:ok' |
| 236 | + } |
| 237 | + success(result) |
| 238 | + complete(result) |
| 239 | + }).catch((error) => { |
| 240 | + const result = { |
| 241 | + errMsg: 'stopBluetoothDevicesDiscovery:fail ' + (error?.message || '') |
| 242 | + } |
| 243 | + fail(result) |
| 244 | + complete(result) |
| 245 | + }) |
| 246 | +} |
| 247 | + |
| 248 | +function onBluetoothDeviceFound (callback) { |
| 249 | + deviceFoundCallbacks.push(callback) |
| 250 | +} |
| 251 | + |
| 252 | +function offBluetoothDeviceFound (callback) { |
| 253 | + const index = deviceFoundCallbacks.indexOf(callback) |
| 254 | + if (index > -1) { |
| 255 | + deviceFoundCallbacks.splice(index, 1) |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +function getConnectedBluetoothDevices (options = {}) { |
| 260 | + if (!manager) { |
| 261 | + return |
| 262 | + } |
| 263 | + const { services = [], success = noop, fail = noop, complete = noop } = options |
| 264 | + manager.connectedDevices(services).then((devices) => { |
| 265 | + const connectedDevices = devices.map(device => ({ |
| 266 | + deviceId: device.id, |
| 267 | + name: device.name |
| 268 | + })) |
| 269 | + const result = { |
| 270 | + errMsg: 'getConnectedBluetoothDevices:ok', |
| 271 | + devices: connectedDevices |
| 272 | + } |
| 273 | + success(result) |
| 274 | + complete(result) |
| 275 | + }).catch((error) => { |
| 276 | + const result = { |
| 277 | + errMsg: 'getConnectedBluetoothDevices:fail ' + (error?.message || '') |
| 278 | + } |
| 279 | + fail(result) |
| 280 | + complete(result) |
| 281 | + }) |
| 282 | +} |
| 283 | + |
| 284 | +function getBluetoothAdapterState (options = {}) { |
| 285 | + if (!manager) { |
| 286 | + return |
| 287 | + } |
| 288 | + const { success = noop, fail = noop, complete = noop } = options |
| 289 | + manager.state().then((state) => { |
| 290 | + const result = { |
| 291 | + errmsg: 'getBluetoothAdapterState:ok', |
| 292 | + discovering, |
| 293 | + available: state === 'PoweredOn' |
| 294 | + } |
| 295 | + success(result) |
| 296 | + complete(result) |
| 297 | + }).catch((error) => { |
| 298 | + const result = { |
| 299 | + errMsg: 'getBluetoothAdapterState:fail ' + (error?.message || '') |
| 300 | + } |
| 301 | + fail(result) |
| 302 | + complete(result) |
| 303 | + }) |
| 304 | +} |
| 305 | + |
| 306 | +const closeBLEConnection = envError('closeBLEConnection') |
| 307 | + |
| 308 | +const createBLEConnection = envError('createBLEConnection') |
| 309 | + |
| 310 | +const onBLEConnectionStateChange = envError('onBLEConnectionStateChange') |
| 311 | + |
| 312 | +export { |
| 313 | + closeBLEConnection, |
| 314 | + createBLEConnection, |
| 315 | + onBLEConnectionStateChange, |
| 316 | + openBluetoothAdapter, |
| 317 | + closeBluetoothAdapter, |
| 318 | + startBluetoothDevicesDiscovery, |
| 319 | + stopBluetoothDevicesDiscovery, |
| 320 | + onBluetoothDeviceFound, |
| 321 | + offBluetoothDeviceFound, |
| 322 | + getConnectedBluetoothDevices, |
| 323 | + getBluetoothAdapterState |
| 324 | +} |
0 commit comments