react-native-biometric-verifier 0.0.47 → 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 +1 -1
- package/src/hooks/useBluetoothService.js +79 -119
- package/src/hooks/useGeolocation.js +192 -174
- package/src/hooks/useWifiService.js +175 -0
- package/src/index.js +260 -55
package/package.json
CHANGED
|
@@ -11,74 +11,60 @@ export const useBluetoothService = (notifyMessage) => {
|
|
|
11
11
|
const [nearbyDevices, setNearbyDevices] = useState([]);
|
|
12
12
|
const scanTimeoutRef = useRef(null);
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/* -------------------------------------------------------------------------- */
|
|
15
|
+
/* BLUETOOTH PERMISSIONS */
|
|
16
|
+
/* -------------------------------------------------------------------------- */
|
|
17
17
|
const requestBluetoothPermissions = useCallback(async () => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} else {
|
|
32
|
-
// Older Android versions
|
|
33
|
-
permissions = [
|
|
34
|
-
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
|
|
35
|
-
];
|
|
36
|
-
}
|
|
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
|
+
}
|
|
37
31
|
|
|
38
|
-
|
|
32
|
+
const granted = await PermissionsAndroid.requestMultiple(permissions);
|
|
33
|
+
const allGranted = Object.values(granted).every(
|
|
34
|
+
status => status === PermissionsAndroid.RESULTS.GRANTED
|
|
35
|
+
);
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
if (!allGranted) {
|
|
45
|
-
notifyMessage?.('Bluetooth permissions are required for device scanning', 'warning');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return allGranted;
|
|
49
|
-
} catch (error) {
|
|
50
|
-
console.error('[Permissions] Error:', error);
|
|
51
|
-
notifyMessage?.('Failed to request Bluetooth permissions', 'error');
|
|
52
|
-
return false;
|
|
37
|
+
if (!allGranted) {
|
|
38
|
+
notifyMessage?.('Bluetooth permissions are required for device scanning', 'warning');
|
|
53
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;
|
|
54
46
|
}
|
|
55
|
-
return true; // iOS handles permissions differently
|
|
56
47
|
}, [notifyMessage]);
|
|
57
48
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
/* -------------------------------------------------------------------------- */
|
|
50
|
+
/* DISTANCE ESTIMATION */
|
|
51
|
+
/* -------------------------------------------------------------------------- */
|
|
61
52
|
const estimateDistance = useCallback((rssi, txPower = -59) => {
|
|
62
53
|
if (!rssi || rssi >= 0) return -1;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const n = 2.0; // Path loss exponent (2 for free space, 2.7-3.5 for indoor)
|
|
54
|
+
|
|
55
|
+
const n = 2.0;
|
|
66
56
|
const distance1 = Math.pow(10, (txPower - rssi) / (10 * n));
|
|
67
|
-
|
|
68
|
-
// Method 2: Quadratic approximation for short distances
|
|
69
57
|
const distance2 = 0.89976 * Math.pow(Math.abs(rssi), 0.80976) + 0.111;
|
|
70
|
-
|
|
71
|
-
// Average the methods for better accuracy
|
|
72
58
|
const avgDistance = (distance1 + distance2) / 2;
|
|
73
|
-
|
|
74
|
-
return Math.max(0.1, avgDistance);
|
|
59
|
+
|
|
60
|
+
return Math.max(0.1, avgDistance);
|
|
75
61
|
}, []);
|
|
76
62
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
63
|
+
/* -------------------------------------------------------------------------- */
|
|
64
|
+
/* FILTER STALE DEVICES */
|
|
65
|
+
/* -------------------------------------------------------------------------- */
|
|
80
66
|
const filterStaleDevices = useCallback(() => {
|
|
81
|
-
setNearbyDevices(prev =>
|
|
67
|
+
setNearbyDevices(prev =>
|
|
82
68
|
prev.filter(device => {
|
|
83
69
|
const isRecent = Date.now() - device.lastSeen < 10000; // 10 seconds
|
|
84
70
|
const hasMultipleReadings = device.count >= 3;
|
|
@@ -87,11 +73,10 @@ export const useBluetoothService = (notifyMessage) => {
|
|
|
87
73
|
);
|
|
88
74
|
}, []);
|
|
89
75
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
76
|
+
/* -------------------------------------------------------------------------- */
|
|
77
|
+
/* STOP SCAN */
|
|
78
|
+
/* -------------------------------------------------------------------------- */
|
|
93
79
|
const stopBluetoothScan = useCallback(() => {
|
|
94
|
-
console.log('[BLE] Stopping Bluetooth scan...');
|
|
95
80
|
manager.stopDeviceScan();
|
|
96
81
|
if (scanTimeoutRef.current) {
|
|
97
82
|
clearTimeout(scanTimeoutRef.current);
|
|
@@ -99,56 +84,43 @@ export const useBluetoothService = (notifyMessage) => {
|
|
|
99
84
|
}
|
|
100
85
|
}, []);
|
|
101
86
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
87
|
+
/* -------------------------------------------------------------------------- */
|
|
88
|
+
/* START BLE SCAN */
|
|
89
|
+
/* -------------------------------------------------------------------------- */
|
|
105
90
|
const startBluetoothScan = useCallback(async () => {
|
|
106
|
-
console.log('[BLE] Starting enhanced Bluetooth scan...');
|
|
107
|
-
|
|
108
91
|
const permission = await requestBluetoothPermissions();
|
|
109
|
-
if (!permission)
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
92
|
+
if (!permission) return;
|
|
112
93
|
|
|
113
|
-
// Clear previous devices
|
|
114
94
|
setNearbyDevices([]);
|
|
115
|
-
|
|
116
|
-
// Configure scan for better accuracy
|
|
95
|
+
|
|
117
96
|
const scanOptions = {
|
|
118
97
|
allowDuplicates: true,
|
|
119
|
-
scanMode: 2,
|
|
98
|
+
scanMode: 2,
|
|
120
99
|
};
|
|
121
100
|
|
|
122
101
|
manager.startDeviceScan(null, scanOptions, (error, device) => {
|
|
123
102
|
if (error) {
|
|
124
103
|
console.error('[BLE] Scan error:', error);
|
|
125
104
|
stopBluetoothScan();
|
|
126
|
-
|
|
127
|
-
// Provide user-friendly error messages
|
|
105
|
+
|
|
128
106
|
let errorMessage = 'Bluetooth scan failed';
|
|
129
|
-
if (error.errorCode === 102)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
errorMessage = 'Location services required for scanning';
|
|
133
|
-
}
|
|
134
|
-
|
|
107
|
+
if (error.errorCode === 102) errorMessage = 'Bluetooth is not enabled';
|
|
108
|
+
else if (error.errorCode === 103) errorMessage = 'Location services required for scanning';
|
|
109
|
+
|
|
135
110
|
notifyMessage?.(errorMessage, 'error');
|
|
136
111
|
return;
|
|
137
112
|
}
|
|
138
113
|
|
|
139
114
|
if (device && device.name && device.rssi) {
|
|
140
115
|
const distance = estimateDistance(device.rssi);
|
|
141
|
-
|
|
142
|
-
// Filter out weak signals and calculate moving average
|
|
116
|
+
|
|
143
117
|
if (distance > 0 && distance <= 20) {
|
|
144
118
|
setNearbyDevices(prev => {
|
|
145
119
|
const existingIndex = prev.findIndex(d => d.id === device.id);
|
|
146
|
-
|
|
147
120
|
if (existingIndex >= 0) {
|
|
148
|
-
// Update existing device with moving average
|
|
149
121
|
const existing = prev[existingIndex];
|
|
150
122
|
const avgDistance = (parseFloat(existing.distance) + distance) / 2;
|
|
151
|
-
|
|
123
|
+
|
|
152
124
|
const updated = [...prev];
|
|
153
125
|
updated[existingIndex] = {
|
|
154
126
|
...existing,
|
|
@@ -160,64 +132,52 @@ export const useBluetoothService = (notifyMessage) => {
|
|
|
160
132
|
};
|
|
161
133
|
return updated;
|
|
162
134
|
} else {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
+
];
|
|
177
150
|
}
|
|
178
151
|
});
|
|
179
152
|
}
|
|
180
153
|
}
|
|
181
154
|
});
|
|
182
155
|
|
|
183
|
-
// Stop after optimized duration
|
|
184
156
|
scanTimeoutRef.current = setTimeout(() => {
|
|
185
157
|
stopBluetoothScan();
|
|
186
158
|
filterStaleDevices();
|
|
187
|
-
|
|
188
|
-
if (nearbyDevices.length === 0) {
|
|
189
|
-
notifyMessage?.('No nearby Bluetooth devices found', 'info');
|
|
190
|
-
} else {
|
|
191
|
-
console.log(`[BLE] Found ${nearbyDevices.length} nearby devices`);
|
|
192
|
-
}
|
|
193
159
|
}, 10000);
|
|
160
|
+
}, [notifyMessage, requestBluetoothPermissions, stopBluetoothScan, filterStaleDevices, estimateDistance, nearbyDevices]);
|
|
194
161
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Get device details by ID
|
|
200
|
-
*/
|
|
162
|
+
/* -------------------------------------------------------------------------- */
|
|
163
|
+
/* DEVICE DETAILS */
|
|
164
|
+
/* -------------------------------------------------------------------------- */
|
|
201
165
|
const getDeviceDetails = useCallback(async (deviceId) => {
|
|
202
166
|
try {
|
|
203
167
|
const device = await manager.connectToDevice(deviceId);
|
|
204
168
|
await device.discoverAllServicesAndCharacteristics();
|
|
205
169
|
const services = await device.services();
|
|
206
|
-
|
|
207
|
-
return {
|
|
208
|
-
...device,
|
|
209
|
-
services,
|
|
210
|
-
isConnected: true,
|
|
211
|
-
};
|
|
170
|
+
|
|
171
|
+
return { ...device, services, isConnected: true };
|
|
212
172
|
} catch (error) {
|
|
213
173
|
console.error('[BLE] Error getting device details:', error);
|
|
214
174
|
return null;
|
|
215
175
|
}
|
|
216
176
|
}, []);
|
|
217
177
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
178
|
+
/* -------------------------------------------------------------------------- */
|
|
179
|
+
/* CLEAR DEVICES */
|
|
180
|
+
/* -------------------------------------------------------------------------- */
|
|
221
181
|
const clearDevices = useCallback(() => {
|
|
222
182
|
setNearbyDevices([]);
|
|
223
183
|
}, []);
|
|
@@ -232,4 +192,4 @@ export const useBluetoothService = (notifyMessage) => {
|
|
|
232
192
|
getDeviceDetails,
|
|
233
193
|
estimateDistance,
|
|
234
194
|
};
|
|
235
|
-
};
|
|
195
|
+
};
|