1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
import React, {useState, useEffect, useRef} from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
FlatList,
Platform,
PermissionsAndroid,
DeviceEventEmitter,
NativeModules,
} from 'react-native';
import {SafeAreaProvider, SafeAreaView} from 'react-native-safe-area-context';
// Device type string names accepted by startDiscovery on both platforms
const DEVICE_TYPE_KEYS = [
'AM3S', 'AM4', 'PO3', 'BP5', 'BP5S', 'BP3L', 'BP7', 'BP7S',
'KN550', 'HS2', 'HS2S', 'HS4S', 'BG1', 'BG1S', 'BG5', 'BG5S',
'ECG3', 'BTM', 'TS28B', 'NT13B',
] as const;
type DeviceTypeName = (typeof DEVICE_TYPE_KEYS)[number];
// The native module exports string constants on iOS, number constants on Android.
type DiscoveryConstant = string | number;
interface IHealthDeviceManager {
// Device type constants (string on iOS, number on Android)
AM3S: DiscoveryConstant;
AM4: DiscoveryConstant;
PO3: DiscoveryConstant;
BP5: DiscoveryConstant;
BP5S: DiscoveryConstant;
BP3L: DiscoveryConstant;
BP7: DiscoveryConstant;
BP7S: DiscoveryConstant;
KN550: DiscoveryConstant;
HS2: DiscoveryConstant;
HS2S: DiscoveryConstant;
HS4S: DiscoveryConstant;
BG1: DiscoveryConstant;
BG1S: DiscoveryConstant;
BG5: DiscoveryConstant;
BG5S: DiscoveryConstant;
ECG3: DiscoveryConstant;
BTM: DiscoveryConstant;
TS28B: DiscoveryConstant;
NT13B: DiscoveryConstant;
// Event name constants
Event_Scan_Device: string;
Event_Scan_Finish: string;
Event_Device_Connected: string;
Event_Device_Connect_Failed: string;
Event_Device_Disconnect: string;
Event_Authenticate_Result: string;
// Methods — startDiscovery takes a device type name string per official docs
startDiscovery(type: DeviceTypeName | string): void;
stopDiscovery(): void;
connectDevice(mac: string, type: string): void;
disconnectDevice(mac: string, type: string): void;
sdkAuthWithLicense(license: string): void;
authenConfigureInfo(userName: string, clientID: string, clientSecret: string): void;
getDevicesIDPS(mac: string, callback: (idps: Record<string, string>) => void): void;
}
const iHealthDeviceManagerModule =
NativeModules.iHealthDeviceManagerModule as IHealthDeviceManager;
type Device = {
mac: string;
type: string;
rssi?: number;
timestamp: number;
};
async function requestAndroidPermissions(): Promise<boolean> {
if (Platform.OS !== 'android') return true;
const apiLevel = Platform.Version;
const permissions: string[] = [];
if (apiLevel >= 31) {
permissions.push(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
);
}
permissions.push(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
const results = await PermissionsAndroid.requestMultiple(permissions as any);
return Object.values(results).every(
r => r === PermissionsAndroid.RESULTS.GRANTED,
);
}
function DeviceIcon({type}: {type: string}) {
const icons: Record<string, string> = {
BP: 'BP',
AM: 'AM',
PO: 'PO',
BG: 'BG',
HS: 'HS',
ECG: 'ECG',
BTM: 'BTM',
};
const prefix = Object.keys(icons).find(k => type.startsWith(k)) || '?';
return (
<View style={styles.deviceIcon}>
<Text style={styles.deviceIconText}>{icons[prefix] || '?'}</Text>
</View>
);
}
function App() {
const [devices, setDevices] = useState<Device[]>([]);
const [scanning, setScanning] = useState(false);
const [error, setError] = useState<string | null>(null);
const devicesRef = useRef<Device[]>([]);
useEffect(() => {
if (!iHealthDeviceManagerModule) {
setError('iHealth native module not found. Check linking.');
return;
}
// The iHealth SDK uses the global DeviceEventEmitter (not NativeEventEmitter)
// because the iOS native module doesn't subclass RCTEventEmitter.
const scanSub = DeviceEventEmitter.addListener(
iHealthDeviceManagerModule.Event_Scan_Device ?? 'event_scan_device',
(event: {mac: string; type: string; rssi?: number}) => {
const {mac = '', type = 'Unknown', rssi} = event;
// Filter out AM3 — likely false positives from non-iHealth heart rate devices
if (type === 'AM3') return;
const existing = devicesRef.current.findIndex(d => d.mac === mac);
let updated: Device[];
if (existing >= 0) {
updated = [...devicesRef.current];
updated[existing] = {mac, type, rssi, timestamp: Date.now()};
} else {
updated = [
...devicesRef.current,
{mac, type, rssi, timestamp: Date.now()},
];
}
devicesRef.current = updated;
setDevices(updated);
},
);
const finishSub = DeviceEventEmitter.addListener(
iHealthDeviceManagerModule.Event_Scan_Finish ?? 'event_scan_finish',
() => {
setScanning(false);
},
);
return () => {
scanSub.remove();
finishSub.remove();
};
}, []);
const startScan = async () => {
setError(null);
const granted = await requestAndroidPermissions();
if (!granted) {
setError('Bluetooth permissions denied');
return;
}
devicesRef.current = [];
setDevices([]);
setScanning(true);
// Discover all device types. 'ALL' hits the default case in getDiscoveryType()
// which maps to DiscoveryTypeEnum.All (MIX = BLE + BT Classic + WiFi).
// Requires ACCESS_NETWORK_STATE permission for WiFi scan.
try {
iHealthDeviceManagerModule.startDiscovery('ALL');
} catch (e) {
console.log('Failed to start discovery:', e);
}
};
const stopScan = () => {
try {
iHealthDeviceManagerModule.stopDiscovery();
} catch (e) {
console.log('Failed to stop discovery:', e);
}
setScanning(false);
};
const renderDevice = ({item}: {item: Device}) => (
<View style={styles.deviceRow}>
<DeviceIcon type={item.type} />
<View style={styles.deviceInfo}>
<Text style={styles.deviceType}>{item.type}</Text>
<Text style={styles.deviceMac}>{item.mac}</Text>
</View>
{item.rssi != null && (
<Text style={styles.deviceRssi}>{item.rssi} dBm</Text>
)}
</View>
);
return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Text style={styles.title}>iHealth Scanner</Text>
<Text style={styles.subtitle}>
{scanning
? `Scanning... (${devices.length} found)`
: `${devices.length} device(s) found`}
</Text>
{error && <Text style={styles.error}>{error}</Text>}
<TouchableOpacity
style={[styles.button, scanning && styles.buttonStop]}
onPress={scanning ? stopScan : startScan}>
<Text style={styles.buttonText}>
{scanning ? 'Stop Scan' : 'Start Scan'}
</Text>
</TouchableOpacity>
<FlatList
data={devices}
keyExtractor={item => item.mac}
renderItem={renderDevice}
style={styles.list}
contentContainerStyle={devices.length === 0 && styles.emptyList}
ListEmptyComponent={
<Text style={styles.emptyText}>
{scanning
? 'Looking for iHealth devices...'
: 'Tap "Start Scan" to find nearby iHealth devices'}
</Text>
}
/>
</SafeAreaView>
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
paddingHorizontal: 16,
},
title: {
fontSize: 28,
fontWeight: '700',
color: '#1a1a1a',
marginTop: 16,
},
subtitle: {
fontSize: 14,
color: '#666',
marginTop: 4,
marginBottom: 16,
},
error: {
color: '#d32f2f',
fontSize: 13,
marginBottom: 8,
backgroundColor: '#ffebee',
padding: 8,
borderRadius: 6,
},
button: {
backgroundColor: '#2196F3',
paddingVertical: 14,
borderRadius: 10,
alignItems: 'center',
marginBottom: 16,
},
buttonStop: {
backgroundColor: '#f44336',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
list: {
flex: 1,
},
emptyList: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
emptyText: {
color: '#999',
fontSize: 15,
textAlign: 'center',
},
deviceRow: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: '#fff',
padding: 14,
borderRadius: 10,
marginBottom: 8,
shadowColor: '#000',
shadowOffset: {width: 0, height: 1},
shadowOpacity: 0.05,
shadowRadius: 2,
elevation: 1,
},
deviceIcon: {
width: 44,
height: 44,
borderRadius: 22,
backgroundColor: '#e3f2fd',
justifyContent: 'center',
alignItems: 'center',
marginRight: 12,
},
deviceIconText: {
fontSize: 13,
fontWeight: '700',
color: '#1565c0',
},
deviceInfo: {
flex: 1,
},
deviceType: {
fontSize: 16,
fontWeight: '600',
color: '#1a1a1a',
},
deviceMac: {
fontSize: 12,
color: '#888',
fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace',
marginTop: 2,
},
deviceRssi: {
fontSize: 12,
color: '#999',
marginLeft: 8,
},
});
export default App;
|