@mpxjs/api-proxy 2.10.16-beta.7 → 2.10.16-beta.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.10.16-beta.7",
3
+ "version": "2.10.16-beta.9",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -1,4 +1,3 @@
1
- import BleManager from 'react-native-ble-manager'
2
1
  import { noop } from '@mpxjs/utils'
3
2
  import mpx from '@mpxjs/core'
4
3
  import { Platform, PermissionsAndroid } from 'react-native'
@@ -72,10 +71,11 @@ const commonFailHandler = function (errMsg, fail, complete) {
72
71
  }
73
72
 
74
73
  function openBluetoothAdapter (options = {}) {
74
+ const BleManager = require('react-native-ble-manager').default
75
75
  const { success = noop, fail = noop, complete = noop } = options
76
76
  let bluetoothPermission = requestBluetoothPermission
77
- if (__mpx_env__ === 'android' && mpx.rnConfig?.bluetoothPermission) { // 安卓需要验证权限,开放给用户可以自定义验证权限的方法
78
- bluetoothPermission = mpx.rnConfig.bluetoothPermission
77
+ if (mpx.config?.rnConfig?.bluetoothPermission) { // 安卓需要验证权限,开放给用户可以自定义验证权限的方法
78
+ bluetoothPermission = mpx.config.rnConfig.bluetoothPermission
79
79
  }
80
80
  // 先请求权限,再初始化蓝牙管理器
81
81
  bluetoothPermission().then((hasPermissions) => {
@@ -118,6 +118,7 @@ function openBluetoothAdapter (options = {}) {
118
118
  }
119
119
 
120
120
  function closeBluetoothAdapter (options = {}) {
121
+ const BleManager = require('react-native-ble-manager').default
121
122
  const { success = noop, fail = noop, complete = noop } = options
122
123
  if (!bleManagerInitialized) {
123
124
  const result = {
@@ -183,6 +184,7 @@ function closeBluetoothAdapter (options = {}) {
183
184
  }
184
185
 
185
186
  function startBluetoothDevicesDiscovery (options = {}) {
187
+ const BleManager = require('react-native-ble-manager').default
186
188
  const {
187
189
  services = [],
188
190
  allowDuplicatesKey = false,
@@ -243,6 +245,7 @@ function startBluetoothDevicesDiscovery (options = {}) {
243
245
  }
244
246
 
245
247
  function stopBluetoothDevicesDiscovery (options = {}) {
248
+ const BleManager = require('react-native-ble-manager').default
246
249
  const { success = noop, fail = noop, complete = noop } = options
247
250
 
248
251
  if (!bleManagerInitialized) {
@@ -282,6 +285,7 @@ function offBluetoothDeviceFound (callback) {
282
285
  }
283
286
 
284
287
  function getConnectedBluetoothDevices (options = {}) {
288
+ const BleManager = require('react-native-ble-manager').default
285
289
  const { services = [], success = noop, fail = noop, complete = noop } = options
286
290
 
287
291
  if (!bleManagerInitialized) {
@@ -306,6 +310,7 @@ function getConnectedBluetoothDevices (options = {}) {
306
310
  }
307
311
 
308
312
  function getBluetoothAdapterState (options = {}) {
313
+ const BleManager = require('react-native-ble-manager').default
309
314
  const { success = noop, fail = noop, complete = noop } = options
310
315
 
311
316
  if (!bleManagerInitialized) {
@@ -326,6 +331,7 @@ function getBluetoothAdapterState (options = {}) {
326
331
  })
327
332
  }
328
333
  function onDidUpdateState () {
334
+ const BleManager = require('react-native-ble-manager').default
329
335
  updateStateSubscription = BleManager.onDidUpdateState((state) => {
330
336
  onStateChangeCallbacks.forEach(cb => {
331
337
  cb({
@@ -384,6 +390,7 @@ function getBluetoothDevices (options = {}) { // 该能力只是获取应用级
384
390
  }
385
391
 
386
392
  function writeBLECharacteristicValue (options = {}) {
393
+ const BleManager = require('react-native-ble-manager').default
387
394
  const { deviceId, serviceId, characteristicId, value, success = noop, fail = noop, complete = noop } = options
388
395
  if (!deviceId || !serviceId || !characteristicId || !value) {
389
396
  const result = {
@@ -413,6 +420,7 @@ function writeBLECharacteristicValue (options = {}) {
413
420
  }
414
421
 
415
422
  function readBLECharacteristicValue (options = {}) {
423
+ const BleManager = require('react-native-ble-manager').default
416
424
  const { deviceId, serviceId, characteristicId, success = noop, fail = noop, complete = noop } = options
417
425
 
418
426
  if (!deviceId || !serviceId || !characteristicId) {
@@ -449,6 +457,7 @@ function readBLECharacteristicValue (options = {}) {
449
457
  }
450
458
 
451
459
  function notifyBLECharacteristicValueChange (options = {}) {
460
+ const BleManager = require('react-native-ble-manager').default
452
461
  const { deviceId, serviceId, characteristicId, state = true, success = noop, fail = noop, complete = noop } = options
453
462
 
454
463
  if (!deviceId || !serviceId || !characteristicId) {
@@ -502,6 +511,7 @@ function notifyBLECharacteristicValueChange (options = {}) {
502
511
 
503
512
  let valueForCharacteristicSubscriptions = null
504
513
  function onBLECharacteristicValueChange (callback) {
514
+ const BleManager = require('react-native-ble-manager').default
505
515
  if (characteristicCallbacks.length === 0) {
506
516
  valueForCharacteristicSubscriptions = BleManager.onDidUpdateValueForCharacteristic((data) => {
507
517
  // 将byte array转换为ArrayBuffer
@@ -538,6 +548,7 @@ function offBLECharacteristicValueChange (callback) {
538
548
  }
539
549
 
540
550
  function setBLEMTU (options = {}) {
551
+ const BleManager = require('react-native-ble-manager').default
541
552
  const { deviceId, mtu, success = noop, fail = noop, complete = noop } = options
542
553
  if (!mtu) {
543
554
  commonFailHandler('setBLEMTU:fail parameter error: parameter.mtu should be Number instead of Undefined;', fail, complete)
@@ -569,6 +580,7 @@ function setBLEMTU (options = {}) {
569
580
  }
570
581
 
571
582
  function getBLEDeviceRSSI (options = {}) {
583
+ const BleManager = require('react-native-ble-manager').default
572
584
  const { deviceId, success = noop, fail = noop, complete = noop } = options
573
585
 
574
586
  if (!deviceId) {
@@ -599,6 +611,7 @@ function getBLEDeviceRSSI (options = {}) {
599
611
  }
600
612
 
601
613
  function getBLEDeviceServices (options = {}) {
614
+ const BleManager = require('react-native-ble-manager').default
602
615
  const { deviceId, success = noop, fail = noop, complete = noop } = options
603
616
 
604
617
  if (!deviceId) {
@@ -671,7 +684,13 @@ function getBLEDeviceCharacteristics (options = {}) {
671
684
  }
672
685
  const characteristics = characteristicsList.map(char => ({
673
686
  uuid: char.characteristic,
674
- properties: char.properties
687
+ properties: {
688
+ read: !!char.properties.Read,
689
+ write: !!char.properties.Write,
690
+ notify: !!char.properties.Notify,
691
+ indicate: !!char.properties.Indicate,
692
+ writeNoResponse: !!char.properties.writeWithoutResponse
693
+ }
675
694
  }))
676
695
 
677
696
  const result = {
@@ -683,6 +702,7 @@ function getBLEDeviceCharacteristics (options = {}) {
683
702
  }
684
703
 
685
704
  function createBLEConnection (options = {}) {
705
+ const BleManager = require('react-native-ble-manager').default
686
706
  const { deviceId, timeout, success = noop, fail = noop, complete = noop } = options
687
707
 
688
708
  if (!deviceId) {
@@ -730,6 +750,7 @@ function createBLEConnection (options = {}) {
730
750
  }
731
751
 
732
752
  function closeBLEConnection (options = {}) {
753
+ const BleManager = require('react-native-ble-manager').default
733
754
  const { deviceId, success = noop, fail = noop, complete = noop } = options
734
755
 
735
756
  if (!deviceId) {
@@ -1,114 +1,44 @@
1
- import { noop } from '@mpxjs/utils'
1
+ import { noop, getFocusedNavigation } from '@mpxjs/utils'
2
2
 
3
- const qualityValue = {
4
- high: 90,
5
- normal: 75,
6
- low: 50,
7
- original: 100
8
- }
9
3
  export default class CreateCamera {
10
4
  constructor () {
11
- const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
12
- this.camera = navigation?.camera || {}
5
+ const navigation = getFocusedNavigation() || {}
6
+ this.camera = navigation.camera || {}
13
7
  }
14
8
 
15
9
  setZoom (options = {}) {
16
- const { zoom } = options
17
- if (this.camera.setZoom) {
18
- this.camera.setZoom(zoom)
19
- }
20
- }
21
-
22
- takePhoto (options = {}) {
23
- const { success = noop, fail = noop, complete = noop } = options
24
- const takePhoto = this.camera.getTakePhoto?.()
25
- if (takePhoto) {
26
- takePhoto({
27
- quality: qualityValue[options.quality || 'normal']
28
- }).then((res) => {
29
- const result = {
30
- errMsg: 'takePhoto:ok',
31
- tempImagePath: res.path
32
- }
10
+ const { zoom, success = noop, fail = noop, complete = noop } = options
11
+ try {
12
+ if (this.camera.setZoom) {
13
+ const result = { errMsg: 'setZoom:ok' }
33
14
  success(result)
34
15
  complete(result)
35
- }).catch(() => {
16
+ this.camera.setZoom(zoom)
17
+ } else {
36
18
  const result = {
37
- errMsg: 'takePhoto:fail'
19
+ errMsg: 'setZoom:fail camera instance not found'
38
20
  }
39
21
  fail(result)
40
22
  complete(result)
41
- })
42
- }
43
- }
44
-
45
- startRecord (options = {}) {
46
- let { timeout = 30, success = noop, fail = noop, complete = noop, timeoutCallback = noop } = options
47
- timeout = timeout > 300 ? 300 : timeout
48
- let recordTimer = null
49
- const isTimeout = false
50
- const startRecord = this.camera.getStartRecord?.()
51
- if (startRecord) {
52
- const result = {
53
- errMsg: 'startRecord:ok'
54
23
  }
55
- success(result)
56
- complete(result)
57
- startRecord({
58
- onRecordingError: (res) => {
59
- clearTimeout(recordTimer)
60
- timeoutCallback()
61
- },
62
- onRecordingFinished: (res) => {
63
- if (isTimeout) {
64
- console.log('record timeout, ignore', res)
65
- }
66
- clearTimeout(recordTimer)
67
- console.log('record finished', res)
68
- }
69
- })
70
- recordTimer = setTimeout(() => { // 超时自动停止
71
- if (this.camera.stopRecord) {
72
- this.camera.stopRecord().catch(() => {})
73
- }
74
- }, timeout * 1000)
75
- } else {
24
+ } catch (error) {
76
25
  const result = {
77
- errMsg: 'startRecord:fail to initialize the camera'
26
+ errMsg: 'setZoom:fail ' + (error?.message || '')
78
27
  }
79
28
  fail(result)
80
29
  complete(result)
81
30
  }
82
31
  }
83
32
 
84
- stopRecord (options = {}) {
85
- const { success = noop, fail = noop, complete = noop } = options
86
- const stopRecord = this.camera.getStopRecord?.()
87
- if (stopRecord) {
88
- stopRecord().then((res) => {
89
- console.log('stopRecord res', res)
90
- const result = {
91
- errMsg: 'stopRecord:ok',
92
- tempVideoPath: res.path,
93
- duration: res.duration * 1000, // 转成ms
94
- size: res.fileSize
95
- }
96
- success(result)
97
- complete(result)
98
- }).catch((e) => {
99
- console.log('stopRecord error', e)
100
- const result = {
101
- errMsg: 'stopRecord:fail'
102
- }
103
- fail(result)
104
- complete(result)
105
- })
106
- } else {
107
- const result = {
108
- errMsg: 'stopRecord:fail to initialize the camera'
109
- }
110
- fail(result)
111
- complete(result)
112
- }
33
+ takePhoto (options) {
34
+ this.camera?.takePhoto(options)
35
+ }
36
+
37
+ startRecord (options) {
38
+ this.camera?.startRecord(options)
39
+ }
40
+
41
+ stopRecord (options) {
42
+ this.camera?.stopRecord(options)
113
43
  }
114
44
  }
@@ -1,4 +1,3 @@
1
- import WifiManager from 'react-native-wifi-reborn'
2
1
  import { PermissionsAndroid } from 'react-native'
3
2
  import { noop } from '@mpxjs/utils'
4
3
  import mpx from '@mpxjs/core'
@@ -22,6 +21,7 @@ async function requestWifiPermission () {
22
21
  }
23
22
 
24
23
  function startWifi (options = {}) {
24
+ const WifiManager = require('react-native-wifi-reborn').default
25
25
  const { success = noop, fail = noop, complete = noop } = options
26
26
  if (__mpx_mode__ === 'ios') {
27
27
  const result = {
@@ -33,8 +33,8 @@ function startWifi (options = {}) {
33
33
  }
34
34
  startWifiReady = true
35
35
  let wifiPermission = requestWifiPermission
36
- if (mpx.rnConfig?.wifiPermission) {
37
- wifiPermission = mpx.rnConfig.wifiPermission
36
+ if (mpx.config?.rnConfig?.wifiPermission) {
37
+ wifiPermission = mpx.config.rnConfig.wifiPermission
38
38
  }
39
39
  wifiPermission().then(async () => {
40
40
  let enabled
@@ -65,6 +65,7 @@ function startWifi (options = {}) {
65
65
  }
66
66
 
67
67
  function stopWifi (options = {}) {
68
+ const WifiManager = require('react-native-wifi-reborn').default
68
69
  const { success = noop, fail = noop, complete = noop } = options
69
70
  if (__mpx_mode__ === 'ios') {
70
71
  const result = {
@@ -83,6 +84,7 @@ function stopWifi (options = {}) {
83
84
  }
84
85
 
85
86
  function getWifiList (options = {}) {
87
+ const WifiManager = require('react-native-wifi-reborn').default
86
88
  const { success = noop, fail = noop, complete = noop } = options
87
89
  if (__mpx_mode__ === 'ios') {
88
90
  const result = {
@@ -148,6 +150,7 @@ function offGetWifiList (callback) {
148
150
  }
149
151
 
150
152
  function getConnectedWifi (options = {}) {
153
+ const WifiManager = require('react-native-wifi-reborn').default
151
154
  const { partialInfo = false, success = noop, fail = noop, complete = noop } = options
152
155
 
153
156
  if (!startWifiReady) {