@eohjsc/react-native-smart-city 0.3.75 → 0.3.76

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@eohjsc/react-native-smart-city",
3
3
  "title": "React Native Smart Home",
4
- "version": "0.3.75",
4
+ "version": "0.3.76",
5
5
  "description": "TODO",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -1,5 +1,7 @@
1
- import { useContext, useCallback, useRef, useEffect } from 'react';
1
+ import { useContext, useCallback, useRef, useEffect, useMemo } from 'react';
2
2
  import { AppState, Platform } from 'react-native';
3
+ import { getSystemVersion } from 'react-native-device-info';
4
+
3
5
  import { SCContext, useSCContextSelector } from '../../context';
4
6
  import { Action } from '../../context/actionType';
5
7
  import { scanBluetoothDevices } from '../../iot/RemoteControl/Bluetooth';
@@ -8,6 +10,7 @@ import { OpenSetting } from '../../utils/Permission/common';
8
10
  import { useTranslations } from '../Common/useTranslations';
9
11
  import { SCConfig } from '../../configs';
10
12
 
13
+ // These permissions are only for android 12
11
14
  const permissions = [
12
15
  'android.permission.BLUETOOTH_CONNECT',
13
16
  'android.permission.BLUETOOTH_SCAN',
@@ -24,11 +27,13 @@ const useBluetoothConnection = (fnCallback) => {
24
27
  const permissionsRequested = useSCContextSelector(
25
28
  (state) => state.bluetooth.permissionsRequested
26
29
  );
27
- const permissionsGranted =
28
- Platform.OS === 'ios'
30
+
31
+ const permissionsGranted = useMemo(() => {
32
+ return Platform.OS === 'ios' || parseInt(getSystemVersion(), 10) < 12
29
33
  ? true
30
34
  : // eslint-disable-next-line react-hooks/rules-of-hooks
31
35
  useSCContextSelector((state) => state.bluetooth.permissionsGranted);
36
+ }, []);
32
37
 
33
38
  const onDeviceFound = useCallback(async (name, device) => {
34
39
  fnCallback && fnCallback({ name, device });
@@ -38,9 +43,9 @@ const useBluetoothConnection = (fnCallback) => {
38
43
 
39
44
  const bluetoothScanDevices = useCallback(
40
45
  (addresses) => {
41
- permissionsGranted && scanBluetoothDevices(addresses, onDeviceFound);
46
+ scanBluetoothDevices(addresses, onDeviceFound);
42
47
  },
43
- [permissionsGranted, onDeviceFound]
48
+ [onDeviceFound]
44
49
  );
45
50
 
46
51
  useEffect(() => {
@@ -1,14 +1,17 @@
1
1
  /* eslint-disable promise/prefer-await-to-callbacks */
2
- import { BLE } from '../../configs';
3
- import t from '../../hooks/Common/useTranslations';
4
2
  import base64 from 'react-native-base64';
5
- import { BleManager } from 'react-native-ble-plx';
3
+ import { BleManager, ScanMode } from 'react-native-ble-plx';
6
4
  import { ToastBottomHelper } from '../../utils/Utils';
7
5
 
6
+ import { BLE } from '../../configs';
7
+ import t from '../../hooks/Common/useTranslations';
8
+
8
9
  const bluetoothDevices = {};
9
10
  const needToScanDevices = [];
10
11
  const bleManager = new BleManager();
11
12
 
13
+ let isScanning = false;
14
+
12
15
  export const SEND_COMMAND_OVER_BLUETOOTH_FAIL =
13
16
  'SEND_COMMAND_OVER_BLUETOOTH_FAIL';
14
17
 
@@ -36,44 +39,52 @@ export const realScanBluetoothDevices = async (onDeviceFound) => {
36
39
  if (!needToScanDevices.length) {
37
40
  return;
38
41
  }
42
+ if (!isScanning) {
43
+ isScanning = true;
44
+ bleManager.startDeviceScan(
45
+ null,
46
+ { scanMode: ScanMode?.Balanced },
47
+ (error, device) => {
48
+ if (error) {
49
+ return;
50
+ }
39
51
 
40
- bleManager.startDeviceScan(null, null, (error, device) => {
41
- if (error) {
42
- return;
43
- }
44
-
45
- let name = null;
46
- if (
47
- needToScanDevices.includes(device.name) &&
48
- !bluetoothDevices[device.name]
49
- ) {
50
- name = device.name;
51
- } else if (
52
- needToScanDevices.includes(device.localName) &&
53
- !bluetoothDevices[device.localName]
54
- ) {
55
- name = device.localName;
56
- } else {
57
- return;
58
- }
52
+ let name = null;
53
+ if (
54
+ needToScanDevices.includes(device.name) &&
55
+ !bluetoothDevices[device.name]
56
+ ) {
57
+ name = device.name;
58
+ } else if (
59
+ needToScanDevices.includes(device.localName) &&
60
+ !bluetoothDevices[device.localName]
61
+ ) {
62
+ name = device.localName;
63
+ } else {
64
+ return;
65
+ }
59
66
 
60
- const index = needToScanDevices.indexOf(name);
61
- needToScanDevices.splice(index, 1);
67
+ const index = needToScanDevices.indexOf(name);
68
+ needToScanDevices.splice(index, 1);
62
69
 
63
- bluetoothDevices[name] = device;
64
- onDeviceFound && onDeviceFound(name, device);
70
+ bluetoothDevices[name] = device;
71
+ onDeviceFound && onDeviceFound(name, device);
65
72
 
66
- if (!needToScanDevices.length) {
67
- try {
68
- bleManager.stopDeviceScan();
69
- // eslint-disable-next-line no-empty
70
- } catch {}
71
- }
72
- });
73
+ if (!needToScanDevices.length) {
74
+ try {
75
+ bleManager.stopDeviceScan();
76
+ isScanning = false;
77
+ // eslint-disable-next-line no-empty
78
+ } catch {}
79
+ }
80
+ }
81
+ );
82
+ }
73
83
 
74
84
  const to = setTimeout(() => {
75
85
  try {
76
86
  bleManager.stopDeviceScan();
87
+ isScanning = false;
77
88
  clearTimeout(to);
78
89
  // eslint-disable-next-line no-empty
79
90
  } catch {}
@@ -25,6 +25,36 @@ describe('Test IOT Bluetooth', () => {
25
25
  clearNeedToScanDevices();
26
26
  });
27
27
 
28
+ it('Send command over bluetooth via device success', async () => {
29
+ const device = {
30
+ name: '1234567',
31
+ cancelConnection: jest.fn(),
32
+ connect: async () => ({
33
+ discoverAllServicesAndCharacteristics: async () => ({
34
+ writeCharacteristicWithResponseForService: async () => ({}),
35
+ monitorCharacteristicForService: async () => ({}),
36
+ }),
37
+ }),
38
+ };
39
+ bleManager.startDeviceScan.mockImplementation(
40
+ (uuids, options, listener) => {
41
+ listener(null, device);
42
+ }
43
+ );
44
+ await scanBluetoothDevices([device.name], mockOnDeviceFound);
45
+
46
+ await sendCommandOverBluetooth(
47
+ {
48
+ remote_control_options: {
49
+ bluetooth: {
50
+ address: device.name,
51
+ },
52
+ },
53
+ },
54
+ {}
55
+ );
56
+ });
57
+
28
58
  test('Scan bluetooth device will init hardware scan', async () => {
29
59
  await scanBluetoothDevices(['123456'], mockOnDeviceFound);
30
60
  expect(bleManager.startDeviceScan).toBeCalled();
@@ -107,7 +137,7 @@ describe('Test IOT Bluetooth', () => {
107
137
  }
108
138
  );
109
139
  await scanBluetoothDevices([device.name], mockOnDeviceFound);
110
- expect(bleManager.stopDeviceScan).toBeCalled();
140
+ expect(bleManager.stopDeviceScan).not.toBeCalled();
111
141
  });
112
142
 
113
143
  it('Scan same device again will not trigger hardware scan', async () => {
@@ -120,7 +150,7 @@ describe('Test IOT Bluetooth', () => {
120
150
  }
121
151
  );
122
152
  await scanBluetoothDevices([device.name], mockOnDeviceFound);
123
- expect(bleManager.startDeviceScan).toBeCalled();
153
+ expect(bleManager.startDeviceScan).not.toBeCalled();
124
154
 
125
155
  bleManager.startDeviceScan.mockClear();
126
156
 
@@ -169,37 +199,7 @@ describe('Test IOT Bluetooth', () => {
169
199
  },
170
200
  },
171
201
  },
172
- "Cannot read properties of null (reading 'cancelConnection')"
173
- );
174
- });
175
-
176
- it('Send command over bluetooth via device success', async () => {
177
- const device = {
178
- name: '1234567',
179
- cancelConnection: jest.fn(),
180
- connect: async () => ({
181
- discoverAllServicesAndCharacteristics: async () => ({
182
- writeCharacteristicWithResponseForService: async () => ({}),
183
- monitorCharacteristicForService: async () => ({}),
184
- }),
185
- }),
186
- };
187
- bleManager.startDeviceScan.mockImplementation(
188
- (uuids, options, listener) => {
189
- listener(null, device);
190
- }
191
- );
192
- await scanBluetoothDevices([device.name], mockOnDeviceFound);
193
-
194
- await sendCommandOverBluetooth(
195
- {
196
- remote_control_options: {
197
- bluetooth: {
198
- address: device.name,
199
- },
200
- },
201
- },
202
- {}
202
+ undefined
203
203
  );
204
204
  });
205
205
  });
@@ -26,7 +26,7 @@ const SelectDeviceSubUnit = ({ route }) => {
26
26
  });
27
27
  break;
28
28
  case DEVICE_TYPE.ZIGBEE:
29
- navigation.navigate(Routes.ConnectRouterGuide, {
29
+ navigation.navigate(Routes.SelectZigbeeGateway, {
30
30
  unit,
31
31
  subUnit: chosenSubUnit,
32
32
  });