@mpxjs/api-proxy 2.10.15-prelease.1 → 2.10.16-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/@types/index.d.ts CHANGED
@@ -45,9 +45,36 @@ export const offError: WechatMiniprogram.Wx['offError']
45
45
  export const createInnerAudioContext: WechatMiniprogram.Wx['createInnerAudioContext']
46
46
  export const base64ToArrayBuffer: WechatMiniprogram.Wx['base64ToArrayBuffer']
47
47
  export const arrayBufferToBase64: WechatMiniprogram.Wx['arrayBufferToBase64']
48
+ export const openBluetoothAdapter: WechatMiniprogram.Wx['openBluetoothAdapter']
49
+ export const closeBluetoothAdapter: WechatMiniprogram.Wx['closeBluetoothAdapter']
50
+ export const startBluetoothDevicesDiscovery: WechatMiniprogram.Wx['startBluetoothDevicesDiscovery']
51
+ export const stopBluetoothDevicesDiscovery: WechatMiniprogram.Wx['stopBluetoothDevicesDiscovery']
52
+ export const onBluetoothDeviceFound: WechatMiniprogram.Wx['onBluetoothDeviceFound']
53
+ export const offBluetoothDeviceFound: WechatMiniprogram.Wx['offBluetoothDeviceFound']
54
+ export const getConnectedBluetoothDevices: WechatMiniprogram.Wx['getConnectedBluetoothDevices']
55
+ export const getBluetoothAdapterState: WechatMiniprogram.Wx['getBluetoothAdapterState']
56
+ export const onBluetoothAdapterStateChange: WechatMiniprogram.Wx['onBluetoothAdapterStateChange']
57
+ export const offBluetoothAdapterStateChange: WechatMiniprogram.Wx['offBluetoothAdapterStateChange']
58
+ export const getBluetoothDevices: WechatMiniprogram.Wx['getBluetoothDevices']
59
+ export const writeBLECharacteristicValue: WechatMiniprogram.Wx['writeBLECharacteristicValue']
60
+ export const readBLECharacteristicValue: WechatMiniprogram.Wx['readBLECharacteristicValue']
61
+ export const notifyBLECharacteristicValueChange: WechatMiniprogram.Wx['notifyBLECharacteristicValueChange']
62
+ export const onBLECharacteristicValueChange: WechatMiniprogram.Wx['onBLECharacteristicValueChange']
63
+ export const offBLECharacteristicValueChange: WechatMiniprogram.Wx['offBLECharacteristicValueChange']
64
+ export const setBLEMTU: WechatMiniprogram.Wx['setBLEMTU']
65
+ export const getBLEDeviceRSSI: WechatMiniprogram.Wx['getBLEDeviceRSSI']
66
+ export const getBLEDeviceServices: WechatMiniprogram.Wx['getBLEDeviceServices']
67
+ export const getBLEDeviceCharacteristics: WechatMiniprogram.Wx['getBLEDeviceCharacteristics']
48
68
  export const closeBLEConnection: WechatMiniprogram.Wx['closeBLEConnection']
49
69
  export const createBLEConnection: WechatMiniprogram.Wx['createBLEConnection']
50
70
  export const onBLEConnectionStateChange: WechatMiniprogram.Wx['onBLEConnectionStateChange']
71
+ export const offBLEConnectionStateChange: WechatMiniprogram.Wx['offBLEConnectionStateChange']
72
+ export const startWifi: WechatMiniprogram.Wx['startWifi']
73
+ export const stopWifi: WechatMiniprogram.Wx['stopWifi']
74
+ export const getWifiList: WechatMiniprogram.Wx['getWifiList']
75
+ export const onGetWifiList: WechatMiniprogram.Wx['onGetWifiList']
76
+ export const offGetWifiList: WechatMiniprogram.Wx['offGetWifiList']
77
+ export const getConnectedWifi: WechatMiniprogram.Wx['getConnectedWifi']
51
78
  export const createCanvasContext: WechatMiniprogram.Wx['createCanvasContext']
52
79
  export const canvasToTempFilePath: WechatMiniprogram.Wx['canvasToTempFilePath']
53
80
  export const canvasGetImageData: WechatMiniprogram.Wx['canvasGetImageData']
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.10.15-prelease.1",
3
+ "version": "2.10.16-beta.1",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "homepage": "https://github.com/didi/mpx#readme",
39
39
  "dependencies": {
40
- "@mpxjs/utils": "^2.10.13",
40
+ "@mpxjs/utils": "^2.10.16",
41
41
  "axios": "^1.7.3"
42
42
  },
43
43
  "peerDependencies": {
@@ -46,7 +46,9 @@
46
46
  "react-native-device-info": "*",
47
47
  "react-native-get-location": "*",
48
48
  "react-native-haptic-feedback": "*",
49
- "react-native-safe-area-context": "*"
49
+ "react-native-safe-area-context": "*",
50
+ "react-native-ble-manager": "*",
51
+ "react-native-wifi-reborn": "*"
50
52
  },
51
53
  "peerDependenciesMeta": {
52
54
  "@react-native-async-storage/async-storage": {
@@ -66,6 +68,12 @@
66
68
  },
67
69
  "react-native-haptic-feedback": {
68
70
  "optional": true
71
+ },
72
+ "react-native-ble-manager": {
73
+ "optional": true
74
+ },
75
+ "react-native-wifi-reborn": {
76
+ "optional": true
69
77
  }
70
78
  }
71
79
  }
@@ -3,7 +3,8 @@ import { noop } from '@mpxjs/utils'
3
3
 
4
4
  function showActionSheet (options = {}) {
5
5
  const opts = changeOpts(options, {
6
- itemList: 'items'
6
+ itemList: 'items',
7
+ alertText: 'title'
7
8
  })
8
9
 
9
10
  const cacheSuc = opts.success || noop
@@ -0,0 +1,13 @@
1
+ import { ENV_OBJ, changeOpts } from '../../../common/js'
2
+
3
+ function createAnimation (options = {}) {
4
+ const opts = changeOpts(options, {
5
+ timingFunction: 'timeFunction'
6
+ })
7
+
8
+ return ENV_OBJ.createAnimation(opts)
9
+ }
10
+
11
+ export {
12
+ createAnimation
13
+ }
@@ -1,4 +1,4 @@
1
- import { ENV_OBJ } from '../../../common/js'
1
+ import { ENV_OBJ, envError } from '../../../common/js'
2
2
 
3
3
  function closeBLEConnection (options = {}) {
4
4
  return ENV_OBJ.disconnectBLEDevice(options)
@@ -12,8 +12,73 @@ function onBLEConnectionStateChange (callback) {
12
12
  return ENV_OBJ.onBLEConnectionStateChanged(callback)
13
13
  }
14
14
 
15
+ function offBLEConnectionStateChange (callback) {
16
+ return ENV_OBJ.offBLEConnectionStateChanged(callback)
17
+ }
18
+
19
+ const openBluetoothAdapter = ENV_OBJ.openBluetoothAdapter || envError('openBluetoothAdapter')
20
+
21
+ const closeBluetoothAdapter = ENV_OBJ.closeBluetoothAdapter || envError('closeBluetoothAdapter')
22
+
23
+ const startBluetoothDevicesDiscovery = ENV_OBJ.startBluetoothDevicesDiscovery || envError('startBluetoothDevicesDiscovery')
24
+
25
+ const stopBluetoothDevicesDiscovery = ENV_OBJ.stopBluetoothDevicesDiscovery || envError('stopBluetoothDevicesDiscovery')
26
+
27
+ const onBluetoothDeviceFound = ENV_OBJ.onBluetoothDeviceFound || envError('onBluetoothDeviceFound')
28
+
29
+ const offBluetoothDeviceFound = ENV_OBJ.offBluetoothDeviceFound || envError('offBluetoothDeviceFound')
30
+
31
+ const getConnectedBluetoothDevices = ENV_OBJ.getConnectedBluetoothDevices || envError('getConnectedBluetoothDevices')
32
+
33
+ const getBluetoothAdapterState = ENV_OBJ.getBluetoothAdapterState || envError('getBluetoothAdapterState')
34
+
35
+ const onBluetoothAdapterStateChange = ENV_OBJ.onBluetoothAdapterStateChange || envError('onBluetoothAdapterStateChange')
36
+
37
+ const offBluetoothAdapterStateChange = ENV_OBJ.offBluetoothAdapterStateChange || envError('offBluetoothAdapterStateChange')
38
+
39
+ const getBluetoothDevices = ENV_OBJ.getBluetoothDevices || envError('getBluetoothDevices')
40
+
41
+ const writeBLECharacteristicValue = ENV_OBJ.writeBLECharacteristicValue || envError('writeBLECharacteristicValue')
42
+
43
+ const readBLECharacteristicValue = ENV_OBJ.readBLECharacteristicValue || envError('readBLECharacteristicValue')
44
+
45
+ const notifyBLECharacteristicValueChange = ENV_OBJ.notifyBLECharacteristicValueChange || envError('notifyBLECharacteristicValueChange')
46
+
47
+ const onBLECharacteristicValueChange = ENV_OBJ.onBLECharacteristicValueChange || envError('onBLECharacteristicValueChange')
48
+
49
+ const offBLECharacteristicValueChange = ENV_OBJ.offBLECharacteristicValueChange || envError('offBLECharacteristicValueChange')
50
+
51
+ const setBLEMTU = ENV_OBJ.setBLEMTU || envError('setBLEMTU')
52
+
53
+ const getBLEDeviceRSSI = ENV_OBJ.getBLEDeviceRSSI || envError('getBLEDeviceRSSI')
54
+
55
+ const getBLEDeviceServices = ENV_OBJ.getBLEDeviceServices || envError('getBLEDeviceServices')
56
+
57
+ const getBLEDeviceCharacteristics = ENV_OBJ.getBLEDeviceCharacteristics || envError('getBLEDeviceCharacteristics')
58
+
15
59
  export {
16
60
  closeBLEConnection,
17
61
  createBLEConnection,
18
- onBLEConnectionStateChange
62
+ onBLEConnectionStateChange,
63
+ offBLEConnectionStateChange,
64
+ openBluetoothAdapter,
65
+ closeBluetoothAdapter,
66
+ startBluetoothDevicesDiscovery,
67
+ stopBluetoothDevicesDiscovery,
68
+ onBluetoothDeviceFound,
69
+ offBluetoothDeviceFound,
70
+ getConnectedBluetoothDevices,
71
+ getBluetoothAdapterState,
72
+ onBluetoothAdapterStateChange,
73
+ offBluetoothAdapterStateChange,
74
+ getBluetoothDevices,
75
+ writeBLECharacteristicValue,
76
+ readBLECharacteristicValue,
77
+ notifyBLECharacteristicValueChange,
78
+ onBLECharacteristicValueChange,
79
+ offBLECharacteristicValueChange,
80
+ setBLEMTU,
81
+ getBLEDeviceRSSI,
82
+ getBLEDeviceServices,
83
+ getBLEDeviceCharacteristics
19
84
  }
@@ -0,0 +1,815 @@
1
+ import BleManager from 'react-native-ble-manager'
2
+ import { noop } from '@mpxjs/utils'
3
+ import mpx from '@mpxjs/core'
4
+ import { Platform, PermissionsAndroid } from 'react-native'
5
+ import { base64ToArrayBuffer } from '../base/index'
6
+
7
+ // BleManager 相关
8
+ // 全局状态管理
9
+ let bleManagerInitialized = false
10
+ let DiscoverPeripheralSubscription = null
11
+ let updateStateSubscription = null
12
+ let discovering = false
13
+ let getDevices = [] // 记录已扫描的设备列表
14
+ const deviceFoundCallbacks = []
15
+ const onStateChangeCallbacks = []
16
+ const characteristicCallbacks = []
17
+ const onBLEConnectionStateCallbacks = []
18
+ let characteristicSubscriptions = {}
19
+ const connectedDevices = new Set()
20
+ let createBLEConnectionTimeout = null
21
+ const BLEDeviceCharacteristics = {} // 记录已连接设备的特征值
22
+ const connectedDeviceId = []
23
+
24
+ // 请求蓝牙权限
25
+ const requestBluetoothPermission = async () => {
26
+ if (__mpx_mode__ === 'android') {
27
+ const permissions = []
28
+ if (Platform.Version >= 23 && Platform.Version < 31) {
29
+ permissions.push(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
30
+ } else if (Platform.Version >= 31) {
31
+ permissions.push(
32
+ PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
33
+ PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT
34
+ )
35
+ }
36
+
37
+ if (permissions.length === 0) {
38
+ return true
39
+ }
40
+ const granted = await PermissionsAndroid.requestMultiple(permissions)
41
+ return Object.values(granted).every(
42
+ result => result === PermissionsAndroid.RESULTS.GRANTED
43
+ )
44
+ }
45
+ return true
46
+ }
47
+
48
+ const removeBluetoothDevicesDiscovery = function () {
49
+ if (DiscoverPeripheralSubscription) {
50
+ DiscoverPeripheralSubscription.remove()
51
+ DiscoverPeripheralSubscription = null
52
+ }
53
+ }
54
+ const removeUpdateStateSubscription = function () {
55
+ if (updateStateSubscription && onStateChangeCallbacks.length === 0 && onBLEConnectionStateCallbacks.length === 0) {
56
+ updateStateSubscription.remove()
57
+ updateStateSubscription = null
58
+ }
59
+ }
60
+ const commonFailHandler = function (errMsg, fail, complete) {
61
+ const result = {
62
+ errMsg
63
+ }
64
+ if (!bleManagerInitialized) {
65
+ Object.assign(result, {
66
+ errCode: 10000,
67
+ errno: 1500101
68
+ })
69
+ }
70
+ fail(result)
71
+ complete(result)
72
+ }
73
+
74
+ function openBluetoothAdapter (options = {}) {
75
+ const { success = noop, fail = noop, complete = noop } = options
76
+ let bluetoothPermission = requestBluetoothPermission
77
+ if (__mpx_env__ === 'android' && mpx.rnConfig?.bluetoothPermission) { // 安卓需要验证权限,开放给用户可以自定义验证权限的方法
78
+ bluetoothPermission = mpx.rnConfig.bluetoothPermission
79
+ }
80
+ // 先请求权限,再初始化蓝牙管理器
81
+ bluetoothPermission().then((hasPermissions) => {
82
+ if (!hasPermissions) {
83
+ commonFailHandler('openBluetoothAdapter:fail no permission', fail, complete)
84
+ return
85
+ }
86
+
87
+ if (bleManagerInitialized) {
88
+ commonFailHandler('openBluetoothAdapter:fail already opened', fail, complete)
89
+ return
90
+ }
91
+
92
+ BleManager.start({ showAlert: false }).then(() => {
93
+ bleManagerInitialized = true
94
+
95
+ // 检查蓝牙状态
96
+ setTimeout(() => {
97
+ BleManager.checkState().then((state) => {
98
+ if (state === 'on') {
99
+ const result = {
100
+ errno: 0,
101
+ errMsg: 'openBluetoothAdapter:ok'
102
+ }
103
+ success(result)
104
+ complete(result)
105
+ } else {
106
+ commonFailHandler('openBluetoothAdapter:fail bluetooth not enabled', fail, complete)
107
+ }
108
+ }).catch((error) => {
109
+ commonFailHandler('openBluetoothAdapter:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
110
+ })
111
+ }, 1000)
112
+ }).catch((error) => {
113
+ commonFailHandler('openBluetoothAdapter:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
114
+ })
115
+ }).catch(() => {
116
+ commonFailHandler('openBluetoothAdapter:fail no permission', fail, complete)
117
+ })
118
+ }
119
+
120
+ function closeBluetoothAdapter (options = {}) {
121
+ const { success = noop, fail = noop, complete = noop } = options
122
+ if (!bleManagerInitialized) {
123
+ const result = {
124
+ errMsg: 'closeBluetoothAdapter:fail 请先调用 wx.openBluetoothAdapter 接口进行初始化操作'
125
+ }
126
+ fail(result)
127
+ complete(result)
128
+ return
129
+ }
130
+ try {
131
+ // 停止扫描
132
+ if (discovering) {
133
+ BleManager.stopScan()
134
+ discovering = false
135
+ }
136
+
137
+ if (createBLEConnectionTimeout) { // 清除掉正在连接的蓝牙设备
138
+ clearTimeout(createBLEConnectionTimeout)
139
+ createBLEConnectionTimeout = null
140
+ }
141
+
142
+ removeUpdateStateSubscription()
143
+ // 清理状态
144
+ bleManagerInitialized = false
145
+ discovering = false
146
+ getDevices = []
147
+ connectedDeviceId.length = 0
148
+ connectedDevices.forEach((id) => {
149
+ BleManager.disconnect(id).catch(() => {})
150
+ })
151
+ connectedDevices.clear()
152
+ deviceFoundCallbacks.length = 0
153
+ onStateChangeCallbacks.length = 0
154
+ characteristicCallbacks.length = 0
155
+ onBLEConnectionStateCallbacks.length = 0
156
+ if (valueForCharacteristicSubscriptions) {
157
+ valueForCharacteristicSubscriptions.remove()
158
+ valueForCharacteristicSubscriptions = null
159
+ }
160
+
161
+ removeBluetoothDevicesDiscovery()
162
+
163
+ // 清理订阅
164
+ Object.keys(characteristicSubscriptions).forEach(key => {
165
+ if (characteristicSubscriptions[key]) {
166
+ characteristicSubscriptions[key].remove()
167
+ }
168
+ })
169
+ characteristicSubscriptions = {}
170
+
171
+ const result = {
172
+ errMsg: 'closeBluetoothAdapter:ok'
173
+ }
174
+ success(result)
175
+ complete(result)
176
+ } catch (error) {
177
+ const result = {
178
+ errMsg: 'closeBluetoothAdapter:fail ' + error.message
179
+ }
180
+ fail(result)
181
+ complete(result)
182
+ }
183
+ }
184
+
185
+ function startBluetoothDevicesDiscovery (options = {}) {
186
+ const {
187
+ services = [],
188
+ allowDuplicatesKey = false,
189
+ success = noop,
190
+ fail = noop,
191
+ complete = noop
192
+ } = options
193
+
194
+ if (!bleManagerInitialized) {
195
+ commonFailHandler('startBluetoothDevicesDiscovery:fail ble adapter hans\'t been opened or ble is unavailable.', fail, complete)
196
+ return
197
+ }
198
+ DiscoverPeripheralSubscription = BleManager.onDiscoverPeripheral((device) => {
199
+ const advertising = device.advertising || {}
200
+ const advertisData = advertising.manufacturerData?.data || null
201
+ const deviceInfo = {
202
+ deviceId: device.id,
203
+ name: device.name || advertising.localName || '未知设备',
204
+ RSSI: device.rssi || 0,
205
+ advertisData: advertisData ? base64ToArrayBuffer(advertisData) : advertisData, // todo需要转换
206
+ advertisServiceUUIDs: advertising.serviceUUIDs || [],
207
+ localName: advertising.localName || '',
208
+ serviceData: advertising.serviceData || {},
209
+ connectable: advertising.isConnectable || false
210
+ }
211
+ if (allowDuplicatesKey === false) {
212
+ const existingDeviceIndex = getDevices.findIndex(existingDevice => existingDevice.deviceId === deviceInfo.deviceId)
213
+ if (existingDeviceIndex > -1) {
214
+ return
215
+ }
216
+ }
217
+ deviceFoundCallbacks.forEach(cb => {
218
+ cb({
219
+ devices: [deviceInfo]
220
+ })
221
+ })
222
+ getDevices.push(deviceInfo)
223
+ // 处理设备发现逻辑
224
+ })
225
+ BleManager.scan(services, 0, allowDuplicatesKey).then((res) => { // 必须,没有开启扫描,onDiscoverPeripheral回调不会触发
226
+ onStateChangeCallbacks.forEach(cb => {
227
+ cb({
228
+ available: true,
229
+ discovering: true
230
+ })
231
+ })
232
+ discovering = true
233
+ getDevices = [] // 清空之前的发现设备列表
234
+ const result = {
235
+ errMsg: 'startBluetoothDevicesDiscovery:ok',
236
+ isDiscovering: true
237
+ }
238
+ success(result)
239
+ complete(result)
240
+ }).catch((error) => {
241
+ commonFailHandler('startBluetoothDevicesDiscovery:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
242
+ })
243
+ }
244
+
245
+ function stopBluetoothDevicesDiscovery (options = {}) {
246
+ const { success = noop, fail = noop, complete = noop } = options
247
+
248
+ if (!bleManagerInitialized) {
249
+ commonFailHandler('stopBluetoothDevicesDiscovery:fail ble adapter hans\'t been opened or ble is unavailable.', fail, complete)
250
+ return
251
+ }
252
+ removeBluetoothDevicesDiscovery()
253
+ BleManager.stopScan().then(() => {
254
+ discovering = false
255
+ onStateChangeCallbacks.forEach(cb => {
256
+ cb({
257
+ available: true,
258
+ discovering: false
259
+ })
260
+ })
261
+ const result = {
262
+ errMsg: 'stopBluetoothDevicesDiscovery:ok'
263
+ }
264
+ success(result)
265
+ complete(result)
266
+ }).catch((error) => {
267
+ commonFailHandler('stopBluetoothDevicesDiscovery:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
268
+ })
269
+ }
270
+
271
+ function onBluetoothDeviceFound (callback) {
272
+ if (deviceFoundCallbacks.indexOf(callback) === -1) {
273
+ deviceFoundCallbacks.push(callback)
274
+ }
275
+ }
276
+
277
+ function offBluetoothDeviceFound (callback) {
278
+ const index = deviceFoundCallbacks.indexOf(callback)
279
+ if (index > -1) {
280
+ deviceFoundCallbacks.splice(index, 1)
281
+ }
282
+ }
283
+
284
+ function getConnectedBluetoothDevices (options = {}) {
285
+ const { services = [], success = noop, fail = noop, complete = noop } = options
286
+
287
+ if (!bleManagerInitialized) {
288
+ commonFailHandler('getConnectedBluetoothDevices:fail 请先调用 wx.openBluetoothAdapter 接口进行初始化操作', fail, complete)
289
+ return
290
+ }
291
+
292
+ BleManager.getConnectedPeripherals(services).then((peripherals) => {
293
+ const devices = peripherals.map(peripheral => ({
294
+ deviceId: peripheral.id,
295
+ name: peripheral.name || '未知设备'
296
+ }))
297
+ const result = {
298
+ errMsg: 'getConnectedBluetoothDevices:ok',
299
+ devices: devices
300
+ }
301
+ success(result)
302
+ complete(result)
303
+ }).catch((error) => {
304
+ commonFailHandler('getConnectedBluetoothDevices:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
305
+ })
306
+ }
307
+
308
+ function getBluetoothAdapterState (options = {}) {
309
+ const { success = noop, fail = noop, complete = noop } = options
310
+
311
+ if (!bleManagerInitialized) {
312
+ commonFailHandler('getBluetoothAdapterState:fail ble adapter need open first.', fail, complete)
313
+ return
314
+ }
315
+
316
+ BleManager.checkState().then((state) => {
317
+ const result = {
318
+ errMsg: 'getBluetoothAdapterState:ok',
319
+ discovering,
320
+ available: state === 'on'
321
+ }
322
+ success(result)
323
+ complete(result)
324
+ }).catch((error) => {
325
+ commonFailHandler('getBluetoothAdapterState:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
326
+ })
327
+ }
328
+ function onDidUpdateState () {
329
+ updateStateSubscription = BleManager.onDidUpdateState((state) => {
330
+ onStateChangeCallbacks.forEach(cb => {
331
+ cb({
332
+ available: state.state === 'on',
333
+ discovering: state.state === 'on' ? discovering : false
334
+ })
335
+ })
336
+ if (onBLEConnectionStateCallbacks.length && connectedDeviceId.length && state.state !== 'on') {
337
+ connectedDeviceId.forEach((id) => {
338
+ onBLEConnectionStateCallbacks.forEach(cb => {
339
+ cb({
340
+ deviceId: id,
341
+ connected: false
342
+ })
343
+ })
344
+ })
345
+ }
346
+ })
347
+ }
348
+
349
+ function onBluetoothAdapterStateChange (callback) {
350
+ if (!updateStateSubscription) {
351
+ onDidUpdateState()
352
+ }
353
+ if (onStateChangeCallbacks.indexOf(callback) === -1) {
354
+ onStateChangeCallbacks.push(callback)
355
+ }
356
+ }
357
+
358
+ function offBluetoothAdapterStateChange (callback) {
359
+ const index = onStateChangeCallbacks.indexOf(callback)
360
+ if (index > -1) {
361
+ onStateChangeCallbacks.splice(index, 1)
362
+ }
363
+ if (deviceFoundCallbacks.length === 0) {
364
+ removeUpdateStateSubscription()
365
+ }
366
+ }
367
+
368
+ function getBluetoothDevices (options = {}) { // 该能力只是获取应用级别已连接设备列表,非手机级别的已连接设备列表
369
+ const { success = noop, fail = noop, complete = noop } = options
370
+ if (!bleManagerInitialized) {
371
+ const result = {
372
+ errMsg: 'getBluetoothDevices:fail ble adapter hans\'t been opened or ble is unavailable.'
373
+ }
374
+ fail(result)
375
+ complete(result)
376
+ return
377
+ }
378
+ const result = {
379
+ errMsg: 'getBluetoothDevices:ok',
380
+ devices: getDevices // 返回已扫描的设备列表
381
+ }
382
+ success(result)
383
+ complete(result)
384
+ }
385
+
386
+ function writeBLECharacteristicValue (options = {}) {
387
+ const { deviceId, serviceId, characteristicId, value, success = noop, fail = noop, complete = noop } = options
388
+ if (!deviceId || !serviceId || !characteristicId || !value) {
389
+ const result = {
390
+ errMsg: 'writeBLECharacteristicValue:ok',
391
+ errno: 1509000
392
+ }
393
+ success(result)
394
+ complete(result)
395
+ return
396
+ }
397
+
398
+ // 将ArrayBuffer转换为byte array
399
+ const bytes = Array.from(new Uint8Array(value))
400
+ BleManager.write(deviceId, serviceId, characteristicId, bytes).then(() => {
401
+ const result = {
402
+ errMsg: 'writeBLECharacteristicValue:ok'
403
+ }
404
+ success(result)
405
+ complete(result)
406
+ }).catch((error) => {
407
+ const result = {
408
+ errMsg: 'writeBLECharacteristicValue:fail ' + (typeof error === 'string' ? error : '')
409
+ }
410
+ fail(result)
411
+ complete(result)
412
+ })
413
+ }
414
+
415
+ function readBLECharacteristicValue (options = {}) {
416
+ const { deviceId, serviceId, characteristicId, success = noop, fail = noop, complete = noop } = options
417
+
418
+ if (!deviceId || !serviceId || !characteristicId) {
419
+ const result = {
420
+ errMsg: 'readBLECharacteristicValue:ok',
421
+ errno: 1509000
422
+ }
423
+ success(result)
424
+ complete(result)
425
+ return
426
+ }
427
+
428
+ BleManager.read(deviceId, serviceId, characteristicId).then((data) => {
429
+ // 将byte array转换为ArrayBuffer
430
+ const buffer = new ArrayBuffer(data.length)
431
+ const view = new Uint8Array(buffer)
432
+ data.forEach((byte, index) => {
433
+ view[index] = byte
434
+ })
435
+
436
+ const result = {
437
+ errMsg: 'readBLECharacteristicValue:ok',
438
+ value: buffer
439
+ }
440
+ success(result)
441
+ complete(result)
442
+ }).catch((error) => {
443
+ const result = {
444
+ errMsg: 'readBLECharacteristicValue:fail ' + (typeof error === 'string' ? error : '')
445
+ }
446
+ fail(result)
447
+ complete(result)
448
+ })
449
+ }
450
+
451
+ function notifyBLECharacteristicValueChange (options = {}) {
452
+ const { deviceId, serviceId, characteristicId, state = true, success = noop, fail = noop, complete = noop } = options
453
+
454
+ if (!deviceId || !serviceId || !characteristicId) {
455
+ const result = {
456
+ errMsg: 'notifyBLECharacteristicValueChange:ok',
457
+ errno: 1509000
458
+ }
459
+ success(result)
460
+ complete(result)
461
+ return
462
+ }
463
+
464
+ const subscriptionKey = `${deviceId}_${serviceId}_${characteristicId}`
465
+
466
+ if (state) {
467
+ // 启用监听
468
+ BleManager.startNotification(deviceId, serviceId, characteristicId).then(() => {
469
+ characteristicSubscriptions[subscriptionKey] = true
470
+
471
+ const result = {
472
+ errMsg: 'notifyBLECharacteristicValueChange:ok'
473
+ }
474
+ success(result)
475
+ complete(result)
476
+ }).catch((error) => {
477
+ const result = {
478
+ errMsg: 'notifyBLECharacteristicValueChange:fail ' + (typeof error === 'string' ? error : '')
479
+ }
480
+ fail(result)
481
+ complete(result)
482
+ })
483
+ } else {
484
+ // 停止监听
485
+ BleManager.stopNotification(deviceId, serviceId, characteristicId).then(() => {
486
+ delete characteristicSubscriptions[subscriptionKey]
487
+
488
+ const result = {
489
+ errMsg: 'notifyBLECharacteristicValueChange:ok'
490
+ }
491
+ success(result)
492
+ complete(result)
493
+ }).catch((error) => {
494
+ const result = {
495
+ errMsg: 'notifyBLECharacteristicValueChange:fail ' + (typeof error === 'string' ? error : '')
496
+ }
497
+ fail(result)
498
+ complete(result)
499
+ })
500
+ }
501
+ }
502
+
503
+ let valueForCharacteristicSubscriptions = null
504
+ function onBLECharacteristicValueChange (callback) {
505
+ if (characteristicCallbacks.length === 0) {
506
+ valueForCharacteristicSubscriptions = BleManager.onDidUpdateValueForCharacteristic((data) => {
507
+ // 将byte array转换为ArrayBuffer
508
+ const buffer = new ArrayBuffer(data.value.length)
509
+ const view = new Uint8Array(buffer)
510
+ data.value.forEach((byte, index) => {
511
+ view[index] = byte
512
+ })
513
+ const result = {
514
+ deviceId: data.peripheral,
515
+ serviceId: data.service,
516
+ characteristicId: data.characteristic,
517
+ value: buffer
518
+ }
519
+ characteristicCallbacks.forEach(cb => {
520
+ cb(result)
521
+ })
522
+ })
523
+ }
524
+ if (characteristicCallbacks.indexOf(callback) === -1) {
525
+ characteristicCallbacks.push(callback)
526
+ }
527
+ }
528
+
529
+ function offBLECharacteristicValueChange (callback) {
530
+ const index = characteristicCallbacks.indexOf(callback)
531
+ if (index > -1) {
532
+ characteristicCallbacks.splice(index, 1)
533
+ }
534
+ if (characteristicCallbacks.length === 0 && valueForCharacteristicSubscriptions) {
535
+ valueForCharacteristicSubscriptions.remove()
536
+ valueForCharacteristicSubscriptions = null
537
+ }
538
+ }
539
+
540
+ function setBLEMTU (options = {}) {
541
+ const { deviceId, mtu, success = noop, fail = noop, complete = noop } = options
542
+ if (!mtu) {
543
+ commonFailHandler('setBLEMTU:fail parameter error: parameter.mtu should be Number instead of Undefined;', fail, complete)
544
+ return
545
+ }
546
+ if (!deviceId) {
547
+ commonFailHandler('setBLEMTU:fail parameter error: parameter.deviceId should be String instead of Undefined;', fail, complete)
548
+ return
549
+ }
550
+ if (!deviceId && !mtu) {
551
+ commonFailHandler('setBLEMTU:fail parameter error: parameter.deviceId should be String instead of Undefined;parameter.mtu should be Number instead of Undefined;', fail, complete)
552
+ return
553
+ }
554
+
555
+ BleManager.requestMTU(deviceId, mtu).then((actualMtu) => {
556
+ const result = {
557
+ errMsg: 'setBLEMTU:ok',
558
+ mtu: actualMtu
559
+ }
560
+ success(result)
561
+ complete(result)
562
+ }).catch((error) => {
563
+ const result = {
564
+ errMsg: 'setBLEMTU:fail ' + (typeof error === 'string' ? error : '')
565
+ }
566
+ fail(result)
567
+ complete(result)
568
+ })
569
+ }
570
+
571
+ function getBLEDeviceRSSI (options = {}) {
572
+ const { deviceId, success = noop, fail = noop, complete = noop } = options
573
+
574
+ if (!deviceId) {
575
+ const result = {
576
+ errMsg: 'getBLEDeviceRSSI:ok',
577
+ errno: 1509000
578
+ }
579
+ success(result)
580
+ complete(result)
581
+ return
582
+ }
583
+
584
+ BleManager.readRSSI(deviceId).then((rssi) => {
585
+ const result = {
586
+ errMsg: 'getBLEDeviceRSSI:ok',
587
+ RSSI: rssi
588
+ }
589
+ success(result)
590
+ complete(result)
591
+ }).catch((error) => {
592
+ const errmsg = typeof error === 'string' ? error : (typeof error === 'string' ? error : '')
593
+ const result = {
594
+ errMsg: 'getBLEDeviceRSSI:fail ' + errmsg
595
+ }
596
+ fail(result)
597
+ complete(result)
598
+ })
599
+ }
600
+
601
+ function getBLEDeviceServices (options = {}) {
602
+ const { deviceId, success = noop, fail = noop, complete = noop } = options
603
+
604
+ if (!deviceId) {
605
+ const result = {
606
+ errMsg: 'getBLEDeviceServices:ok',
607
+ errno: 1509000,
608
+ services: []
609
+ }
610
+ fail(result)
611
+ complete(result)
612
+ return
613
+ }
614
+ BleManager.retrieveServices(deviceId).then((peripheralInfo) => {
615
+ const services = peripheralInfo.services.map(service => ({
616
+ uuid: service.uuid,
617
+ isPrimary: true
618
+ }))
619
+
620
+ // 存储服务信息
621
+ BLEDeviceCharacteristics[deviceId] = peripheralInfo
622
+
623
+ const result = {
624
+ errMsg: 'getBLEDeviceServices:ok',
625
+ services: services
626
+ }
627
+ success(result)
628
+ complete(result)
629
+ }).catch((error) => {
630
+ const errmsg = typeof error === 'string' ? error : (typeof error === 'string' ? error : '')
631
+ const result = {
632
+ errMsg: 'getBLEDeviceServices:fail ' + errmsg
633
+ }
634
+ fail(result)
635
+ complete(result)
636
+ })
637
+ }
638
+
639
+ function getBLEDeviceCharacteristics (options = {}) {
640
+ const { deviceId, serviceId, success = noop, fail = noop, complete = noop } = options
641
+
642
+ if (!deviceId || !serviceId) {
643
+ const result = {
644
+ errMsg: 'getBLEDeviceCharacteristics:ok',
645
+ errno: 1509000,
646
+ characteristics: []
647
+ }
648
+ success(result)
649
+ complete(result)
650
+ return
651
+ }
652
+
653
+ const peripheralInfo = BLEDeviceCharacteristics[deviceId]
654
+ if (!peripheralInfo) {
655
+ const result = {
656
+ errMsg: 'getBLEDeviceCharacteristics:fail device services not retrieved'
657
+ }
658
+ fail(result)
659
+ complete(result)
660
+ return
661
+ }
662
+ const characteristicsList = peripheralInfo.characteristics || []
663
+ const service = characteristicsList.find(c => c.service.toLowerCase() === serviceId.toLowerCase())
664
+ if (!service && !characteristicsList.length) {
665
+ const result = {
666
+ errMsg: 'getBLEDeviceCharacteristics:fail service not found'
667
+ }
668
+ fail(result)
669
+ complete(result)
670
+ return
671
+ }
672
+ const characteristics = characteristicsList.map(char => ({
673
+ uuid: char.characteristic,
674
+ properties: char.properties
675
+ }))
676
+
677
+ const result = {
678
+ errMsg: 'getBLEDeviceCharacteristics:ok',
679
+ characteristics
680
+ }
681
+ success(result)
682
+ complete(result)
683
+ }
684
+
685
+ function createBLEConnection (options = {}) {
686
+ const { deviceId, timeout, success = noop, fail = noop, complete = noop } = options
687
+
688
+ if (!deviceId) {
689
+ const result = {
690
+ errMsg: 'createBLEConnection:ok',
691
+ errno: 1509000
692
+ }
693
+ fail(result)
694
+ complete(result)
695
+ return
696
+ }
697
+
698
+ BleManager.connect(deviceId, {
699
+ autoconnect: true
700
+ }).then(() => {
701
+ if (connectedDeviceId.indexOf(deviceId) === -1) {
702
+ connectedDeviceId.push(deviceId) // 记录一下已连接的设备id
703
+ }
704
+ clearTimeout(createBLEConnectionTimeout)
705
+ onBLEConnectionStateCallbacks.forEach(cb => {
706
+ cb({
707
+ deviceId,
708
+ connected: true
709
+ })
710
+ })
711
+ connectedDevices.add(deviceId)
712
+ const result = {
713
+ errMsg: 'createBLEConnection:ok'
714
+ }
715
+ success(result)
716
+ complete(result)
717
+ }).catch((error) => {
718
+ clearTimeout(createBLEConnectionTimeout)
719
+ const result = {
720
+ errMsg: 'createBLEConnection:fail ' + (typeof error === 'string' ? error : '')
721
+ }
722
+ fail(result)
723
+ complete(result)
724
+ })
725
+ if (timeout) {
726
+ createBLEConnectionTimeout = setTimeout(() => { // 超时处理,仅ios会一直连接,android不会
727
+ BleManager.disconnect(deviceId).catch(() => {})
728
+ }, timeout)
729
+ }
730
+ }
731
+
732
+ function closeBLEConnection (options = {}) {
733
+ const { deviceId, success = noop, fail = noop, complete = noop } = options
734
+
735
+ if (!deviceId) {
736
+ const result = {
737
+ errMsg: 'closeBLEConnection:ok',
738
+ errno: 1509000
739
+ }
740
+ success(result)
741
+ complete(result)
742
+ return
743
+ }
744
+
745
+ BleManager.disconnect(deviceId).then(() => {
746
+ const index = connectedDeviceId.indexOf(deviceId)
747
+ if (index !== -1) {
748
+ connectedDeviceId.splice(index, 1) // 记录一下已连接的设备id
749
+ }
750
+ onBLEConnectionStateCallbacks.forEach(cb => {
751
+ cb({
752
+ deviceId,
753
+ connected: false
754
+ })
755
+ })
756
+ connectedDevices.delete(deviceId)
757
+ const result = {
758
+ errMsg: 'closeBLEConnection:ok'
759
+ }
760
+ success(result)
761
+ complete(result)
762
+ }).catch((error) => {
763
+ const result = {
764
+ errMsg: 'closeBLEConnection:fail ' + (typeof error === 'string' ? error : '')
765
+ }
766
+ fail(result)
767
+ complete(result)
768
+ })
769
+ }
770
+
771
+ function onBLEConnectionStateChange (callback) {
772
+ if (!updateStateSubscription) {
773
+ onDidUpdateState()
774
+ }
775
+ if (onBLEConnectionStateCallbacks.indexOf(callback) === -1) {
776
+ onBLEConnectionStateCallbacks.push(callback)
777
+ }
778
+ }
779
+
780
+ function offBLEConnectionStateChange (callback) {
781
+ const index = onBLEConnectionStateCallbacks.indexOf(callback)
782
+ if (index !== -1) {
783
+ onBLEConnectionStateCallbacks.splice(index, 1)
784
+ }
785
+ if (onBLEConnectionStateCallbacks.length === 0) {
786
+ removeUpdateStateSubscription()
787
+ }
788
+ }
789
+
790
+ export {
791
+ openBluetoothAdapter,
792
+ closeBluetoothAdapter,
793
+ startBluetoothDevicesDiscovery,
794
+ stopBluetoothDevicesDiscovery,
795
+ onBluetoothDeviceFound,
796
+ offBluetoothDeviceFound,
797
+ getConnectedBluetoothDevices,
798
+ getBluetoothAdapterState,
799
+ onBluetoothAdapterStateChange,
800
+ offBluetoothAdapterStateChange,
801
+ getBluetoothDevices,
802
+ writeBLECharacteristicValue,
803
+ readBLECharacteristicValue,
804
+ notifyBLECharacteristicValueChange,
805
+ onBLECharacteristicValueChange,
806
+ offBLECharacteristicValueChange,
807
+ setBLEMTU,
808
+ getBLEDeviceRSSI,
809
+ getBLEDeviceServices,
810
+ getBLEDeviceCharacteristics,
811
+ createBLEConnection,
812
+ closeBLEConnection,
813
+ onBLEConnectionStateChange,
814
+ offBLEConnectionStateChange
815
+ }
@@ -6,8 +6,71 @@ const createBLEConnection = ENV_OBJ.createBLEConnection || envError('createBLECo
6
6
 
7
7
  const onBLEConnectionStateChange = ENV_OBJ.onBLEConnectionStateChange || envError('onBLEConnectionStateChange')
8
8
 
9
+ const offBLEConnectionStateChange = ENV_OBJ.offBLEConnectionStateChange || envError('offBLEConnectionStateChange')
10
+
11
+ const openBluetoothAdapter = ENV_OBJ.openBluetoothAdapter || envError('openBluetoothAdapter')
12
+
13
+ const closeBluetoothAdapter = ENV_OBJ.closeBluetoothAdapter || envError('closeBluetoothAdapter')
14
+
15
+ const startBluetoothDevicesDiscovery = ENV_OBJ.startBluetoothDevicesDiscovery || envError('startBluetoothDevicesDiscovery')
16
+
17
+ const stopBluetoothDevicesDiscovery = ENV_OBJ.stopBluetoothDevicesDiscovery || envError('stopBluetoothDevicesDiscovery')
18
+
19
+ const onBluetoothDeviceFound = ENV_OBJ.onBluetoothDeviceFound || envError('onBluetoothDeviceFound')
20
+
21
+ const offBluetoothDeviceFound = ENV_OBJ.offBluetoothDeviceFound || envError('offBluetoothDeviceFound')
22
+
23
+ const getConnectedBluetoothDevices = ENV_OBJ.getConnectedBluetoothDevices || envError('getConnectedBluetoothDevices')
24
+
25
+ const getBluetoothAdapterState = ENV_OBJ.getBluetoothAdapterState || envError('getBluetoothAdapterState')
26
+
27
+ const onBluetoothAdapterStateChange = ENV_OBJ.onBluetoothAdapterStateChange || envError('onBluetoothAdapterStateChange')
28
+
29
+ const offBluetoothAdapterStateChange = ENV_OBJ.offBluetoothAdapterStateChange || envError('offBluetoothAdapterStateChange')
30
+
31
+ const getBluetoothDevices = ENV_OBJ.getBluetoothDevices || envError('getBluetoothDevices')
32
+
33
+ const writeBLECharacteristicValue = ENV_OBJ.writeBLECharacteristicValue || envError('writeBLECharacteristicValue')
34
+
35
+ const readBLECharacteristicValue = ENV_OBJ.readBLECharacteristicValue || envError('readBLECharacteristicValue')
36
+
37
+ const notifyBLECharacteristicValueChange = ENV_OBJ.notifyBLECharacteristicValueChange || envError('notifyBLECharacteristicValueChange')
38
+
39
+ const onBLECharacteristicValueChange = ENV_OBJ.onBLECharacteristicValueChange || envError('onBLECharacteristicValueChange')
40
+
41
+ const offBLECharacteristicValueChange = ENV_OBJ.offBLECharacteristicValueChange || envError('offBLECharacteristicValueChange')
42
+
43
+ const setBLEMTU = ENV_OBJ.setBLEMTU || envError('setBLEMTU')
44
+
45
+ const getBLEDeviceRSSI = ENV_OBJ.getBLEDeviceRSSI || envError('getBLEDeviceRSSI')
46
+
47
+ const getBLEDeviceServices = ENV_OBJ.getBLEDeviceServices || envError('getBLEDeviceServices')
48
+
49
+ const getBLEDeviceCharacteristics = ENV_OBJ.getBLEDeviceCharacteristics || envError('getBLEDeviceCharacteristics')
50
+
9
51
  export {
10
- closeBLEConnection,
52
+ onBLEConnectionStateChange,
53
+ offBLEConnectionStateChange,
54
+ openBluetoothAdapter,
55
+ closeBluetoothAdapter,
56
+ startBluetoothDevicesDiscovery,
57
+ stopBluetoothDevicesDiscovery,
58
+ onBluetoothDeviceFound,
59
+ offBluetoothDeviceFound,
60
+ getConnectedBluetoothDevices,
61
+ getBluetoothAdapterState,
62
+ onBluetoothAdapterStateChange,
63
+ offBluetoothAdapterStateChange,
64
+ getBluetoothDevices,
65
+ writeBLECharacteristicValue,
66
+ readBLECharacteristicValue,
67
+ notifyBLECharacteristicValueChange,
68
+ onBLECharacteristicValueChange,
69
+ offBLECharacteristicValueChange,
70
+ setBLEMTU,
71
+ getBLEDeviceRSSI,
72
+ getBLEDeviceServices,
73
+ getBLEDeviceCharacteristics,
11
74
  createBLEConnection,
12
- onBLEConnectionStateChange
75
+ closeBLEConnection
13
76
  }
@@ -5,10 +5,10 @@ const oldObserveList = new Set()
5
5
 
6
6
  if (isBrowser) {
7
7
  window.addEventListener('offline', () => {
8
- oldObserveList.forEach(fn => fn({ isConnected: false, type: 'none' }))
8
+ oldObserveList.forEach(fn => fn({ isConnected: false, networkType: 'none' }))
9
9
  })
10
10
  window.addEventListener('online', () => {
11
- oldObserveList.forEach(fn => fn({ isConnected: true, type: 'unknow' }))
11
+ oldObserveList.forEach(fn => fn({ isConnected: true, networkType: 'unknow' }))
12
12
  })
13
13
  }
14
14
 
@@ -22,7 +22,7 @@ export function onNetworkStatusChange (callbackFn) {
22
22
  const isConnected = navigator.onLine
23
23
  callbackFn({
24
24
  isConnected,
25
- type: isConnected ? evt.currentTarget.effectiveType : 'none'
25
+ networkType: isConnected ? evt.currentTarget.effectiveType : 'none'
26
26
  })
27
27
  }
28
28
  fnMap.set(callbackFn, proxyCallback)
@@ -0,0 +1,231 @@
1
+ import WifiManager from 'react-native-wifi-reborn'
2
+ import { PermissionsAndroid } from 'react-native'
3
+ import { noop } from '@mpxjs/utils'
4
+ import mpx from '@mpxjs/core'
5
+ let startWifiReady = false
6
+ const wifiListListeners = []
7
+
8
+ async function requestWifiPermission () {
9
+ const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
10
+ title: 'Location permission is required for WiFi connections',
11
+ message:
12
+ 'This app needs location permission as this is required ' +
13
+ 'to scan for wifi networks.',
14
+ buttonNegative: 'DENY',
15
+ buttonPositive: 'ALLOW'
16
+ })
17
+ if (granted === PermissionsAndroid.RESULTS.GRANTED) {
18
+ return true
19
+ } else {
20
+ return false
21
+ }
22
+ }
23
+
24
+ function startWifi (options = {}) {
25
+ const { success = noop, fail = noop, complete = noop } = options
26
+ if (__mpx_mode__ === 'ios') {
27
+ const result = {
28
+ errMsg: 'startWifi:fail ios system not support, you need to manually go to the Settings to enable wifi'
29
+ }
30
+ fail(result)
31
+ complete(result)
32
+ return
33
+ }
34
+ startWifiReady = true
35
+ let wifiPermission = requestWifiPermission
36
+ if (mpx.rnConfig?.wifiPermission) {
37
+ wifiPermission = mpx.rnConfig.wifiPermission
38
+ }
39
+ wifiPermission().then(async () => {
40
+ let enabled
41
+ try {
42
+ enabled = await WifiManager.isEnabled()
43
+ } catch (e) {
44
+ enabled = false
45
+ }
46
+ const result = {
47
+ errMsg: 'startWifi:success'
48
+ }
49
+ if (!enabled) {
50
+ WifiManager.setEnabled(true)
51
+ success(result)
52
+ complete(result)
53
+ } else {
54
+ success(result)
55
+ complete(result)
56
+ }
57
+ }).catch((err) => {
58
+ const result = {
59
+ errMsg: 'startWifi:fail ' + (typeof err === 'string' ? err : ''),
60
+ errCode: 12001
61
+ }
62
+ fail(result)
63
+ complete(result)
64
+ })
65
+ }
66
+
67
+ function stopWifi (options = {}) {
68
+ const { success = noop, fail = noop, complete = noop } = options
69
+ if (__mpx_mode__ === 'ios') {
70
+ const result = {
71
+ errMsg: 'startWifi:fail ios system not support, you need to manually go to the Settings to enable wifi'
72
+ }
73
+ fail(result)
74
+ complete(result)
75
+ return
76
+ }
77
+ WifiManager.setEnabled(false)
78
+ const result = {
79
+ errMsg: 'stopWifi:success'
80
+ }
81
+ success(result)
82
+ complete(result)
83
+ }
84
+
85
+ function getWifiList (options = {}) {
86
+ const { success = noop, fail = noop, complete = noop } = options
87
+ if (__mpx_mode__ === 'ios') {
88
+ const result = {
89
+ errMsg: 'startWifi:fail ios system not support'
90
+ }
91
+ fail(result)
92
+ complete(result)
93
+ return
94
+ }
95
+ if (!startWifiReady) {
96
+ const result = {
97
+ errMsg: 'startWifi:fail not init startWifi',
98
+ errCode: 12000
99
+ }
100
+ fail(result)
101
+ complete(result)
102
+ return
103
+ }
104
+ WifiManager.loadWifiList().then((res) => {
105
+ if (wifiListListeners.length) {
106
+ const result = res.map(item => {
107
+ return {
108
+ SSID: item.SSID,
109
+ BSSID: item.BSSID,
110
+ frequency: item.frequency
111
+ }
112
+ })
113
+ wifiListListeners.forEach(callback => {
114
+ callback({ wifiList: result })
115
+ })
116
+ }
117
+ const result = {
118
+ errMsg: 'getWifiList:success',
119
+ errno: 0,
120
+ errCode: 0
121
+ }
122
+ success(result)
123
+ complete(result)
124
+ }).catch(() => {
125
+ const result = {
126
+ errMsg: 'getWifiList:fail'
127
+ }
128
+ fail(result)
129
+ complete(result)
130
+ })
131
+ }
132
+
133
+ function onGetWifiList (callback) {
134
+ if (!startWifiReady && wifiListListeners.indexOf(callback) > -1) {
135
+ return
136
+ }
137
+ wifiListListeners.push(callback)
138
+ }
139
+
140
+ function offGetWifiList (callback) {
141
+ if (!startWifiReady) {
142
+ return
143
+ }
144
+ const index = wifiListListeners.indexOf(callback)
145
+ if (index > -1) {
146
+ wifiListListeners.splice(index, 1)
147
+ }
148
+ }
149
+
150
+ function getConnectedWifi (options = {}) {
151
+ const { partialInfo = false, success = noop, fail = noop, complete = noop } = options
152
+
153
+ if (!startWifiReady) {
154
+ const result = {
155
+ errMsg: 'startWifi:fail not init startWifi',
156
+ errCode: 12000
157
+ }
158
+ fail(result)
159
+ complete(result)
160
+ return
161
+ }
162
+
163
+ if (partialInfo) {
164
+ WifiManager.getCurrentWifiSSID().then((res) => {
165
+ const wifi = {
166
+ SSID: res,
167
+ BSSID: '', // iOS无法获取BSSID
168
+ signalStrength: 0,
169
+ frequency: 0
170
+ }
171
+ const result = {
172
+ wifi: wifi,
173
+ errMsg: 'getConnectedWifi:ok'
174
+ }
175
+ success(result)
176
+ complete(result)
177
+ }).catch((error) => {
178
+ console.log(error)
179
+ const result = {
180
+ errMsg: 'getConnectedWifi:fail'
181
+ }
182
+ fail(result)
183
+ complete(result)
184
+ })
185
+ } else {
186
+ Promise.all([
187
+ WifiManager.getCurrentWifiSSID().catch(() => null),
188
+ WifiManager.getBSSID().catch(() => ''),
189
+ WifiManager.getCurrentSignalStrength().catch(() => 0),
190
+ WifiManager.getFrequency().catch(() => 0)
191
+ ]).then(([ssid, bssid, signalStrength, frequency]) => {
192
+ if (!ssid) {
193
+ const result = {
194
+ errMsg: 'getConnectedWifi:fail'
195
+ }
196
+ fail(result)
197
+ complete(result)
198
+ return
199
+ }
200
+
201
+ const wifi = {
202
+ SSID: ssid,
203
+ BSSID: bssid,
204
+ signalStrength: signalStrength,
205
+ frequency: frequency
206
+ }
207
+
208
+ const result = {
209
+ wifi: wifi,
210
+ errMsg: 'getConnectedWifi:ok'
211
+ }
212
+ success(result)
213
+ complete(result)
214
+ }).catch(() => {
215
+ const result = {
216
+ errMsg: 'getConnectedWifi:fail'
217
+ }
218
+ fail(result)
219
+ complete(result)
220
+ })
221
+ }
222
+ }
223
+
224
+ export {
225
+ startWifi,
226
+ stopWifi,
227
+ getWifiList,
228
+ onGetWifiList,
229
+ offGetWifiList,
230
+ getConnectedWifi
231
+ }
@@ -0,0 +1,22 @@
1
+ import { ENV_OBJ, envError } from '../../../../common/js'
2
+
3
+ const startWifi = ENV_OBJ.startWifi || envError('startWifi')
4
+
5
+ const stopWifi = ENV_OBJ.stopWifi || envError('stopWifi')
6
+
7
+ const getWifiList = ENV_OBJ.getWifiList || envError('getWifiList')
8
+
9
+ const getConnectedWifi = ENV_OBJ.getConnectedWifi || envError('getConnectedWifi')
10
+
11
+ const onGetWifiList = ENV_OBJ.onGetWifiList || envError('onGetWifiList')
12
+
13
+ const offGetWifiList = ENV_OBJ.offGetWifiList || envError('offGetWifiList')
14
+
15
+ export {
16
+ startWifi,
17
+ stopWifi,
18
+ getWifiList,
19
+ onGetWifiList,
20
+ offGetWifiList,
21
+ getConnectedWifi
22
+ }
@@ -11,7 +11,7 @@ const getSystemInfoSync = function () {
11
11
  brand: DeviceInfo.getBrand(),
12
12
  model: DeviceInfo.getModel(),
13
13
  system: `${DeviceInfo.getSystemName()} ${DeviceInfo.getSystemVersion()}`,
14
- platform: DeviceInfo.isEmulatorSync() ? 'emulator' : DeviceInfo.getSystemName(),
14
+ platform: DeviceInfo.isEmulatorSync() ? 'emulator' : DeviceInfo.getSystemName().toLowerCase(),
15
15
  deviceOrientation: screenWidth > screenHeight ? 'portrait' : 'landscape',
16
16
  fontSizeSetting: PixelRatio.getFontScale()
17
17
  }
@@ -68,7 +68,7 @@ const getDeviceInfo = function () {
68
68
  brand: DeviceInfo.getBrand(),
69
69
  model: DeviceInfo.getModel(),
70
70
  system: `${DeviceInfo.getSystemName()} ${DeviceInfo.getSystemVersion()}`,
71
- platform: DeviceInfo.isEmulatorSync() ? 'emulator' : DeviceInfo.getSystemName(),
71
+ platform: DeviceInfo.isEmulatorSync() ? 'emulator' : DeviceInfo.getSystemName().toLowerCase(),
72
72
  memorySize: DeviceInfo.getTotalMemorySync() / (1024 * 1024)
73
73
  })
74
74
  return deviceInfo
@@ -11,7 +11,7 @@ const getWindowInfo = function () {
11
11
  let safeArea = {}
12
12
  const { top = 0, left = 0, bottom = 0, right = 0 } = insets
13
13
  const layout = navigation.layout || {}
14
- const screenHeight = __mpx_mode__ === 'ios' ? dimensionsScreen.height : dimensionsScreen.height - layout.bottomVirtualHeight // 解决安卓开启屏幕内三建导航安卓把安全区计算进去后产生的影响
14
+ const screenHeight = __mpx_mode__ === 'ios' ? dimensionsScreen.height : dimensionsScreen.height - (layout.bottomVirtualHeight || 0) // 解决安卓开启屏幕内导航安卓把安全区计算进去后产生的影响
15
15
  const screenWidth = __mpx_mode__ === 'ios' ? dimensionsScreen.width : dimensionsScreen.width - right
16
16
  const layoutHeight = layout.height || 0
17
17
  const layoutWidth = layout.width || 0
@@ -37,6 +37,9 @@ export * from './api/create-selector-query'
37
37
  // getNetworkType, onNetworkStatusChange, offNetworkStatusChange
38
38
  export * from './api/device/network'
39
39
 
40
+ // startWifi, stopWifi, getWifiList, onGetWifiList, offGetWifiList, getConnectedWifi
41
+ export * from './api/device/wifi'
42
+
40
43
  // downloadFile, uploadFile
41
44
  export * from './api/file'
42
45