react-native-biometric-verifier 0.0.46 → 0.0.48

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": "react-native-biometric-verifier",
3
- "version": "0.0.46",
3
+ "version": "0.0.48",
4
4
  "description": "A React Native module for biometric verification with face recognition and QR code scanning",
5
5
  "main": "src/index.js",
6
6
  "private": false,
@@ -0,0 +1,195 @@
1
+ import { useCallback, useState, useRef } from 'react';
2
+ import { Platform, PermissionsAndroid } from 'react-native';
3
+ import { BleManager } from 'react-native-ble-plx';
4
+
5
+ const manager = new BleManager();
6
+
7
+ /**
8
+ * Bluetooth Service Hook for device scanning and distance estimation
9
+ */
10
+ export const useBluetoothService = (notifyMessage) => {
11
+ const [nearbyDevices, setNearbyDevices] = useState([]);
12
+ const scanTimeoutRef = useRef(null);
13
+
14
+ /* -------------------------------------------------------------------------- */
15
+ /* BLUETOOTH PERMISSIONS */
16
+ /* -------------------------------------------------------------------------- */
17
+ const requestBluetoothPermissions = useCallback(async () => {
18
+ if (Platform.OS !== 'android') return true;
19
+
20
+ try {
21
+ let permissions = [];
22
+ if (Platform.Version >= 31) {
23
+ permissions = [
24
+ PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
25
+ PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
26
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
27
+ ];
28
+ } else {
29
+ permissions = [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION];
30
+ }
31
+
32
+ const granted = await PermissionsAndroid.requestMultiple(permissions);
33
+ const allGranted = Object.values(granted).every(
34
+ status => status === PermissionsAndroid.RESULTS.GRANTED
35
+ );
36
+
37
+ if (!allGranted) {
38
+ notifyMessage?.('Bluetooth permissions are required for device scanning', 'warning');
39
+ }
40
+
41
+ return allGranted;
42
+ } catch (error) {
43
+ console.error('[Permissions] Error:', error);
44
+ notifyMessage?.('Failed to request Bluetooth permissions', 'error');
45
+ return false;
46
+ }
47
+ }, [notifyMessage]);
48
+
49
+ /* -------------------------------------------------------------------------- */
50
+ /* DISTANCE ESTIMATION */
51
+ /* -------------------------------------------------------------------------- */
52
+ const estimateDistance = useCallback((rssi, txPower = -59) => {
53
+ if (!rssi || rssi >= 0) return -1;
54
+
55
+ const n = 2.0;
56
+ const distance1 = Math.pow(10, (txPower - rssi) / (10 * n));
57
+ const distance2 = 0.89976 * Math.pow(Math.abs(rssi), 0.80976) + 0.111;
58
+ const avgDistance = (distance1 + distance2) / 2;
59
+
60
+ return Math.max(0.1, avgDistance);
61
+ }, []);
62
+
63
+ /* -------------------------------------------------------------------------- */
64
+ /* FILTER STALE DEVICES */
65
+ /* -------------------------------------------------------------------------- */
66
+ const filterStaleDevices = useCallback(() => {
67
+ setNearbyDevices(prev =>
68
+ prev.filter(device => {
69
+ const isRecent = Date.now() - device.lastSeen < 10000; // 10 seconds
70
+ const hasMultipleReadings = device.count >= 3;
71
+ return isRecent && hasMultipleReadings;
72
+ })
73
+ );
74
+ }, []);
75
+
76
+ /* -------------------------------------------------------------------------- */
77
+ /* STOP SCAN */
78
+ /* -------------------------------------------------------------------------- */
79
+ const stopBluetoothScan = useCallback(() => {
80
+ manager.stopDeviceScan();
81
+ if (scanTimeoutRef.current) {
82
+ clearTimeout(scanTimeoutRef.current);
83
+ scanTimeoutRef.current = null;
84
+ }
85
+ }, []);
86
+
87
+ /* -------------------------------------------------------------------------- */
88
+ /* START BLE SCAN */
89
+ /* -------------------------------------------------------------------------- */
90
+ const startBluetoothScan = useCallback(async () => {
91
+ const permission = await requestBluetoothPermissions();
92
+ if (!permission) return;
93
+
94
+ setNearbyDevices([]);
95
+
96
+ const scanOptions = {
97
+ allowDuplicates: true,
98
+ scanMode: 2,
99
+ };
100
+
101
+ manager.startDeviceScan(null, scanOptions, (error, device) => {
102
+ if (error) {
103
+ console.error('[BLE] Scan error:', error);
104
+ stopBluetoothScan();
105
+
106
+ let errorMessage = 'Bluetooth scan failed';
107
+ if (error.errorCode === 102) errorMessage = 'Bluetooth is not enabled';
108
+ else if (error.errorCode === 103) errorMessage = 'Location services required for scanning';
109
+
110
+ notifyMessage?.(errorMessage, 'error');
111
+ return;
112
+ }
113
+
114
+ if (device && device.name && device.rssi) {
115
+ const distance = estimateDistance(device.rssi);
116
+
117
+ if (distance > 0 && distance <= 20) {
118
+ setNearbyDevices(prev => {
119
+ const existingIndex = prev.findIndex(d => d.id === device.id);
120
+ if (existingIndex >= 0) {
121
+ const existing = prev[existingIndex];
122
+ const avgDistance = (parseFloat(existing.distance) + distance) / 2;
123
+
124
+ const updated = [...prev];
125
+ updated[existingIndex] = {
126
+ ...existing,
127
+ rssi: device.rssi,
128
+ distance: avgDistance.toFixed(2),
129
+ lastSeen: Date.now(),
130
+ count: existing.count + 1,
131
+ txPowerLevel: device.txPowerLevel || existing.txPowerLevel,
132
+ };
133
+ return updated;
134
+ } else {
135
+ return [
136
+ ...prev,
137
+ {
138
+ id: device.id,
139
+ name: device.name || 'Unknown Device',
140
+ rssi: device.rssi,
141
+ distance: distance.toFixed(2),
142
+ lastSeen: Date.now(),
143
+ count: 1,
144
+ manufacturerData: device.manufacturerData,
145
+ serviceUUIDs: device.serviceUUIDs,
146
+ txPowerLevel: device.txPowerLevel,
147
+ isConnectable: device.isConnectable,
148
+ },
149
+ ];
150
+ }
151
+ });
152
+ }
153
+ }
154
+ });
155
+
156
+ scanTimeoutRef.current = setTimeout(() => {
157
+ stopBluetoothScan();
158
+ filterStaleDevices();
159
+ }, 10000);
160
+ }, [notifyMessage, requestBluetoothPermissions, stopBluetoothScan, filterStaleDevices, estimateDistance, nearbyDevices]);
161
+
162
+ /* -------------------------------------------------------------------------- */
163
+ /* DEVICE DETAILS */
164
+ /* -------------------------------------------------------------------------- */
165
+ const getDeviceDetails = useCallback(async (deviceId) => {
166
+ try {
167
+ const device = await manager.connectToDevice(deviceId);
168
+ await device.discoverAllServicesAndCharacteristics();
169
+ const services = await device.services();
170
+
171
+ return { ...device, services, isConnected: true };
172
+ } catch (error) {
173
+ console.error('[BLE] Error getting device details:', error);
174
+ return null;
175
+ }
176
+ }, []);
177
+
178
+ /* -------------------------------------------------------------------------- */
179
+ /* CLEAR DEVICES */
180
+ /* -------------------------------------------------------------------------- */
181
+ const clearDevices = useCallback(() => {
182
+ setNearbyDevices([]);
183
+ }, []);
184
+
185
+ return {
186
+ requestBluetoothPermission: requestBluetoothPermissions,
187
+ startBluetoothScan,
188
+ stopBluetoothScan,
189
+ nearbyDevices,
190
+ filterStaleDevices,
191
+ clearDevices,
192
+ getDeviceDetails,
193
+ estimateDistance,
194
+ };
195
+ };