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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
package com.ihealth.ihealthlibrary;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.ihealth.communication.control.Bg1Control;
import com.ihealth.communication.control.Bg1Profile;
import com.ihealth.communication.manager.DiscoveryTypeEnum;
import com.ihealth.communication.manager.iHealthDevicesCallback;
import com.ihealth.communication.manager.iHealthDevicesManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Created by jing on 16/10/20.
*/
@ReactModule(name = "iHealthDeviceManagerModule")
public class iHealthDeviceManagerModule extends iHealthBaseModule implements LifecycleEventListener {
private static final String modelName = "iHealthDeviceManagerModule";
private static final String TAG = "iHealthModel";
private final static String AM3S = "AM3S";
private final static String AM4 = "AM4";
private final static String AM5 = "AM5";
private final static String PO3 = "PO3";
private final static String BP5 = "BP5";
private final static String BP5S = "BP5S";
private final static String BP3L = "BP3L";
private final static String BP7 = "BP7";
private final static String BP7S = "BP7S";
private final static String KN550 = "KN550";
private final static String HS2 = "HS2";
private final static String HS2S = "HS2S";
private final static String HS4 = "HS4";
private final static String HS4S = "HS4S";
// private final static String HS6 = "HS6";
private final static String BG1 = "BG1";
private final static String BG1S = "BG1S";
private final static String BG5 = "BG5";
private final static String BG5S = "BG5S";
private final static String BG5L = "BG5L";
private final static String BTM = "BTM";
private final static String ECG3 = "ECG3";
private final static String ECG3USB = "ECG3USB";
private final static String PT3SBT = "PT3SBT";
private final static String TS28B = "TS28B";
private final static String NT13B = "NT13B";
private final static String PO1 = "PO1";
private final static String Event_Scan_Device = "event_scan_device";
private final static String Event_Scan_Finish = "event_scan_finish";
private final static String Event_Device_Connected = "event_device_connected";
private final static String Event_Device_Connect_Failed = "event_device_connect_failed";
private final static String Event_Device_Disconnect = "event_device_disconnect";
private final static String Event_Authenticate_Result = "event_authenticate_result";
private static int callbackId;
private ReactApplicationContext mContext;
private String userName;
private String mac = "";//macAddressForBg1
private static final String DESCRIPTION = "description";
private static final String IDPS = "idps";
private String idps = "";
public iHealthDeviceManagerModule(ReactApplicationContext reactContext) {
super(TAG, reactContext);
mContext = reactContext;
reactContext.addLifecycleEventListener(this);
// SDK init deferred to ensureInitialized() — called lazily on first startDiscovery
}
private iHealthDevicesCallback miHealthDevicesCallback = new iHealthDevicesCallback() {
@Override
public void onScanDevice(String mac, String deviceType, int rssi, Map manufactorData) {
WritableMap params = Arguments.createMap();
params.putString("mac", mac);
if (deviceType.equals("ECGUSB")) {
params.putString("type", "ECG3USB");
} else if (deviceType.equals("KN-550BT")) {
params.putString("type", "KN550");
} else {
params.putString("type", deviceType);
}
params.putInt("rssi", rssi);
sendEvent(Event_Scan_Device, params);
}
@Override
public void onDeviceConnectionStateChange(String mac, String deviceType, int status, int errorID, Map manufactorData) {
String eventName = null;
if (status == iHealthDevicesManager.DEVICE_STATE_CONNECTED) {
eventName = Event_Device_Connected;
} else if (status == iHealthDevicesManager.DEVICE_STATE_CONNECTIONFAIL) {
eventName = Event_Device_Connect_Failed;
} else if (status == iHealthDevicesManager.DEVICE_STATE_DISCONNECTED) {
eventName = Event_Device_Disconnect;
}
if (eventName != null) {
WritableMap params = Arguments.createMap();
params.putString("mac", mac);
if (deviceType.equals("ECGUSB")) {
params.putString("type", "ECG3USB");
} else if (deviceType.equals("KN-550BT")) {
params.putString("type", "KN550");
} else {
params.putString("type", deviceType);
}
params.putInt("errorid", errorID);
sendEvent(eventName, params);
}
}
@Override
public void onUserStatus(String username, int userStatus) {
WritableMap params = Arguments.createMap();
params.putInt("authen", userStatus);
sendEvent(Event_Authenticate_Result, params);
}
@Override
public void onDeviceNotify(String mac, String deviceType, String action, String message) {
commandHandleDeviceNotify(mac, deviceType, action, message);
}
@Override
public void onScanFinish() {
sendEvent(Event_Scan_Finish, null);
}
};
private void commandHandleDeviceNotify(String mac, String deviceType, String action, String message) {
//为了与iOS返回值保持一致,需要进行二次加工
iHealthBaseModule module = null;
switch (deviceType) {
case iHealthDevicesManager.TYPE_BP5:
module = getReactApplicationContext().getNativeModule(BP5Module.class);
break;
case iHealthDevicesManager.TYPE_BP5S:
module = getReactApplicationContext().getNativeModule(BP5SModule.class);
break;
case iHealthDevicesManager.TYPE_BP3L:
module = getReactApplicationContext().getNativeModule(BP3LModule.class);
break;
case iHealthDevicesManager.TYPE_550BT:
module = getReactApplicationContext().getNativeModule(BP550BTModule.class);
break;
case iHealthDevicesManager.TYPE_BP7:
module = getReactApplicationContext().getNativeModule(BP7Module.class);
break;
case iHealthDevicesManager.TYPE_BP7S:
module = getReactApplicationContext().getNativeModule(BP7SModule.class);
break;
case iHealthDevicesManager.TYPE_AM3S:
module = getReactApplicationContext().getNativeModule(AM3SModule.class);
break;
case iHealthDevicesManager.TYPE_AM4:
module = getReactApplicationContext().getNativeModule(AM4Module.class);
break;
case iHealthDevicesManager.TYPE_AM5:
module = getReactApplicationContext().getNativeModule(AM5Module.class);
break;
case iHealthDevicesManager.TYPE_PO3:
module = getReactApplicationContext().getNativeModule(PO3Module.class);
break;
case iHealthDevicesManager.TYPE_HS2:
module = getReactApplicationContext().getNativeModule(HS2Module.class);
break;
case iHealthDevicesManager.TYPE_HS2S:
case iHealthDevicesManager.TYPE_HS2S_PRO:
module = getReactApplicationContext().getNativeModule(HS2SModule.class);
break;
case iHealthDevicesManager.TYPE_HS4:
case iHealthDevicesManager.TYPE_HS4S:
module = getReactApplicationContext().getNativeModule(HS4SModule.class);
break;
case iHealthDevicesManager.TYPE_HS6:
break;
case iHealthDevicesManager.TYPE_BG1:
break;
case iHealthDevicesManager.TYPE_BG1S:
module = getReactApplicationContext().getNativeModule(BG1SModule.class);
break;
case iHealthDevicesManager.TYPE_BG5:
module = getReactApplicationContext().getNativeModule(BG5Module.class);
break;
case iHealthDevicesManager.TYPE_BG5S:
module = getReactApplicationContext().getNativeModule(BG5SModule.class);
break;
case iHealthDevicesManager.TYPE_FDIR_V3:
module = getReactApplicationContext().getNativeModule(BTMModule.class);
break;
case iHealthDevicesManager.TYPE_ECG3:
module = getReactApplicationContext().getNativeModule(ECGModule.class);
break;
case iHealthDevicesManager.TYPE_ECG3_USB:
module = getReactApplicationContext().getNativeModule(ECGUSBModule.class);
break;
case iHealthDevicesManager.TYPE_PT3SBT:
module = getReactApplicationContext().getNativeModule(PT3SBTModule.class);
break;
case iHealthDevicesManager.TYPE_TS28B:
module = getReactApplicationContext().getNativeModule(TS28BModule.class);
break;
case iHealthDevicesManager.TYPE_NT13B:
module = getReactApplicationContext().getNativeModule(NT13BModule.class);
break;
case iHealthDevicesManager.TYPE_PO1:
module = getReactApplicationContext().getNativeModule(PO1Module.class);
break;
default:
module = null;
break;
}
if (module != null) {
module.handleNotify(mac, deviceType, action, message);
} else {
Log.e(TAG, "We do not support this type: " + deviceType);
}
}
@Nullable
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(AM3S, iHealthDevicesManager.DISCOVERY_AM3S);
constants.put(AM4, iHealthDevicesManager.DISCOVERY_AM4);
constants.put(AM5, iHealthDevicesManager.DISCOVERY_AM5);
constants.put(PO3, iHealthDevicesManager.DISCOVERY_PO3);
constants.put(BP5, iHealthDevicesManager.DISCOVERY_BP5);
constants.put(BP5S, iHealthDevicesManager.DISCOVERY_BP5S);
constants.put(BP3L, iHealthDevicesManager.DISCOVERY_BP3L);
constants.put(BP7, iHealthDevicesManager.DISCOVERY_BP7);
constants.put(BP7S, iHealthDevicesManager.DISCOVERY_BP7S);
constants.put(KN550, iHealthDevicesManager.DISCOVERY_BP550BT);
constants.put(HS2, iHealthDevicesManager.DISCOVERY_HS2);
constants.put(HS2S, iHealthDevicesManager.DISCOVERY_HS2S);
constants.put(HS4, iHealthDevicesManager.DISCOVERY_HS4);
constants.put(HS4S, iHealthDevicesManager.DISCOVERY_HS4S);
constants.put(BG1, (double) 110);
constants.put(BG1S, iHealthDevicesManager.DISCOVERY_BG1S);
constants.put(BG5, iHealthDevicesManager.DISCOVERY_BG5);
constants.put(BG5S, iHealthDevicesManager.DISCOVERY_BG5S);
constants.put(BTM, iHealthDevicesManager.DISCOVERY_FDIR_V3);
constants.put(ECG3, iHealthDevicesManager.DISCOVERY_ECG3);
constants.put(ECG3USB, iHealthDevicesManager.DISCOVERY_ECG3_USB);
constants.put(PT3SBT, iHealthDevicesManager.DISCOVERY_PT3SBT);
constants.put(TS28B, iHealthDevicesManager.DISCOVERY_TS28B);
constants.put(NT13B, iHealthDevicesManager.DISCOVERY_NT13B);
constants.put(PO1, iHealthDevicesManager.DISCOVERY_PO1);
constants.put(PO3, iHealthDevicesManager.DISCOVERY_PO3);
constants.put("Event_Scan_Device", Event_Scan_Device);
constants.put("Event_Scan_Finish", Event_Scan_Finish);
constants.put("Event_Device_Connected", Event_Device_Connected);
constants.put("Event_Device_Connect_Failed", Event_Device_Connect_Failed);
constants.put("Event_Device_Disconnect", Event_Device_Disconnect);
constants.put("Event_Authenticate_Result", Event_Authenticate_Result);
return constants;
}
@Override
public String getName() {
return modelName;
}
@Override
public void onHostResume() {
if (callbackId == 0) {
android.app.Activity activity = getCurrentActivity();
android.app.Application app = activity != null ? activity.getApplication() : (android.app.Application) mContext.getApplicationContext();
iHealthDevicesManager.getInstance().init(app, Log.VERBOSE, Log.VERBOSE);
Bg1Control.getInstance().init(mContext, "", 0, true);
callbackId = iHealthDevicesManager.getInstance().registerClientCallback(miHealthDevicesCallback);
}
}
@Override
public void onHostPause() {
Log.i(TAG, "onHostPause");
}
@Override
public void onHostDestroy() {
Log.e(TAG, "onHostDestroy");
if (mBroadcastReceiverRegistered) {
unRegisterReceiver();
}
callbackId = 0;
iHealthDevicesManager.getInstance().unRegisterClientCallback(callbackId);
iHealthDevicesManager.getInstance().destroy();
}
private DiscoveryTypeEnum getDiscoveryType(String type) {
switch (type) {
case "AM3S":
return DiscoveryTypeEnum.AM3S;
case "AM4":
return DiscoveryTypeEnum.AM4;
case "AM5":
return DiscoveryTypeEnum.AM5;
case "BG1S":
return DiscoveryTypeEnum.BG1S;
case "BG5":
return DiscoveryTypeEnum.BG5;
case "BG5S":
return DiscoveryTypeEnum.BG5S;
case "BP3L":
return DiscoveryTypeEnum.BP3L;
case "BP5":
return DiscoveryTypeEnum.BP5;
case "BP5S":
return DiscoveryTypeEnum.BP5S;
case "BP7":
return DiscoveryTypeEnum.BP7;
case "KN550":
return DiscoveryTypeEnum.BP550BT;
case "HS2":
return DiscoveryTypeEnum.HS2;
case "HS2S":
return DiscoveryTypeEnum.HS2S;
case "HS4":
return DiscoveryTypeEnum.HS4;
case "HS4S":
return DiscoveryTypeEnum.HS4S;
case "PO1":
return DiscoveryTypeEnum.PO1;
case "PO3":
return DiscoveryTypeEnum.PO3;
case "PT3SBT":
return DiscoveryTypeEnum.PT3SBT;
case "NT13B":
return DiscoveryTypeEnum.NT13B;
case "TS28B":
return DiscoveryTypeEnum.TS28B;
default:
return DiscoveryTypeEnum.All;
}
}
private void ensureInitialized() {
if (callbackId == 0) {
android.app.Activity activity = getCurrentActivity();
if (activity != null) {
iHealthDevicesManager.getInstance().init(activity.getApplication(), Log.VERBOSE, Log.VERBOSE);
} else {
iHealthDevicesManager.getInstance().init((android.app.Application) mContext.getApplicationContext(), Log.VERBOSE, Log.VERBOSE);
}
Bg1Control.getInstance().init(mContext, "", 0, true);
callbackId = iHealthDevicesManager.getInstance().registerClientCallback(miHealthDevicesCallback);
android.util.Log.e("iHealthScanner", "=== SDK initialized from ensureInitialized, callbackId=" + callbackId + " ===");
}
}
@ReactMethod
public void startDiscovery(String type) {
ensureInitialized();
if (type.equals("BG1")) {
registerReceiver();//scan BG1
} else {
iHealthDevicesManager.getInstance().startDiscovery(getDiscoveryType(type));
}
}
@ReactMethod
public void stopDiscovery() {
iHealthDevicesManager.getInstance().stopDiscovery();
}
@ReactMethod
public void connectDevice(String mac, String type) {
if (type.equals(BG1)) {
Bg1Control.getInstance().connect();
} else if (type.equals(iHealthDevicesManager.TYPE_FDIR_V3)) {
connectTherm(this.userName, mac, type, 1, 1, 2, 0, 1, 0);
} else {
if (type.equals("KN550")) {
iHealthDevicesManager.getInstance().connectDevice(this.userName, mac, iHealthDevicesManager.TYPE_550BT);
} else {
iHealthDevicesManager.getInstance().connectDevice(this.userName, mac, type);
}
}
}
@ReactMethod
public void connectTherm(String userName, String mac, String type, int unit, int measureTarget,
int functionTarget, int hour, int minute, int second) {
iHealthDevicesManager.getInstance().connectTherm(userName, mac, type,
unit, measureTarget, functionTarget, hour, minute, second);
}
@ReactMethod
public void authenConfigureInfo(String userName, String clientID, String clientSecret) {
this.userName = userName;
iHealthDevicesManager.getInstance().sdkUserInAuthor(mContext, userName, clientID, clientSecret, callbackId);
}
@ReactMethod
public void authenAppSecret(String appSecret, Callback callback) {
boolean authenResult = iHealthDevicesManager.getInstance().sdkAuthWithAppSecret(mContext, appSecret);
callback.invoke(authenResult);
}
@ReactMethod
public void getDevicesIDPS(String mac, Callback callback) {
String idpsInfo = iHealthDevicesManager.getInstance().getDevicesIDPS(mac);
WritableMap writableMap = Arguments.createMap();
Utils.jsonToMap(idpsInfo, writableMap);
callback.invoke(writableMap);
}
@Override
public void handleNotify(String mac, String deviceType, String action, String message) {
//Do nothing
}
private void registerReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
intentFilter.addAction(Bg1Profile.ACTION_BG1_CONNECT_RESULT);
intentFilter.addAction(Bg1Profile.ACTION_BG1_SENDCODE_RESULT);
intentFilter.addAction(Bg1Profile.ACTION_BG1_MEASURE_ERROR);
intentFilter.addAction(Bg1Profile.ACTION_BG1_MEASURE_STANDBY);
intentFilter.addAction(Bg1Profile.ACTION_BG1_MEASURE_RESULT);
intentFilter.addAction(Bg1Profile.ACTION_BG1_MEASURE_STRIP_IN);
intentFilter.addAction(Bg1Profile.ACTION_BG1_MEASURE_GET_BLOOD);
intentFilter.addAction(Bg1Profile.ACTION_BG1_MEASURE_STRIP_OUT);
intentFilter.addAction(Bg1Profile.ACTION_BG1_IDPS);
mContext.registerReceiver(broadcastReceiver, intentFilter);
mBroadcastReceiverRegistered = true;
}
synchronized private void unRegisterReceiver() {
mContext.unregisterReceiver(broadcastReceiver);
mBroadcastReceiverRegistered = false;
}
private boolean mBroadcastReceiverRegistered = false;
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!TextUtils.isEmpty(intent.getAction())) {
iHealthBaseModule module = getReactApplicationContext().getNativeModule(BG1Module.class);
WritableMap params = Arguments.createMap();
params.putString("action", intent.getAction());
params.putString("mac", mac);
params.putString("type", BG1);
try {
JSONObject jsonObject = new JSONObject();
switch (intent.getAction()) {
case Intent.ACTION_HEADSET_PLUG:
Log.e(TAG, "headset state -----> " + intent.getExtras().getInt("state"));
Log.e(TAG, "has microphone ----> " + intent.getExtras().getInt("microphone"));
Log.e(TAG, "headset name ------> " + intent.getExtras().getString("name"));
jsonObject.put("state", intent.getExtras().getInt("state"));
Utils.jsonToMap(jsonObject.toString(), params);
if (intent.getExtras().getInt("state") == 1) {
if (intent.getExtras().getInt("microphone") == 1) {//1 if headset has a microphone, 0 otherwise
sendEvent(Event_Scan_Device, params);
} else {
Log.e(TAG, "headSet has no microphone");
}
} else {
sendEvent(Event_Device_Disconnect, params);
Bg1Control.getInstance().disconnect();
module.handleNotify(mac, BG1, Event_Device_Disconnect, jsonObject.toString());
}
sendEvent(Event_Scan_Finish, null);
break;
case Bg1Profile.ACTION_BG1_CONNECT_RESULT:
int connectFlag = intent.getIntExtra(Bg1Profile.BG1_CONNECT_RESULT, -1);
jsonObject.put(Bg1Profile.BG1_CONNECT_RESULT, connectFlag);
jsonObject.put(DESCRIPTION, getErrorDescription(Bg1Profile.ACTION_BG1_CONNECT_RESULT, connectFlag));
if (formatIdps(idps) != null) {
jsonObject.put(IDPS, formatIdps(idps));
}
Utils.jsonToMap(jsonObject.toString(), params);
if (connectFlag == 0) {
sendEvent(Event_Device_Connected, params);
} else {
sendEvent(Event_Device_Connect_Failed, params);
}
break;
case Bg1Profile.ACTION_BG1_SENDCODE_RESULT:
int sendCodeFlag = intent.getIntExtra(Bg1Profile.BG1_SENDCODE_RESULT, -1);
jsonObject.put(Bg1Profile.BG1_SENDCODE_RESULT, sendCodeFlag);
jsonObject.put(DESCRIPTION, getErrorDescription(Bg1Profile.ACTION_BG1_SENDCODE_RESULT, sendCodeFlag));
//Utils.jsonToMap(jsonObject.toString(), params);
module.handleNotify(mac, BG1, Bg1Profile.ACTION_BG1_SENDCODE_RESULT, jsonObject.toString());
break;
case Bg1Profile.ACTION_BG1_MEASURE_ERROR:
int errorNumber = intent.getIntExtra(Bg1Profile.BG1_MEASURE_ERROR, -1);
jsonObject.put(Bg1Profile.BG1_MEASURE_ERROR, errorNumber);
jsonObject.put(DESCRIPTION, getErrorDescription(Bg1Profile.ACTION_BG1_MEASURE_ERROR, errorNumber));
module.handleNotify(mac, BG1, Bg1Profile.ACTION_BG1_MEASURE_ERROR, jsonObject.toString());
break;
case Bg1Profile.ACTION_BG1_MEASURE_RESULT:
int measureResult = intent.getIntExtra(Bg1Profile.BG1_MEASURE_RESULT, -1);
jsonObject.put(Bg1Profile.BG1_MEASURE_RESULT, measureResult);
module.handleNotify(mac, BG1, Bg1Profile.ACTION_BG1_MEASURE_RESULT, jsonObject.toString());
break;
case Bg1Profile.ACTION_BG1_IDPS:
idps = intent.getStringExtra(Bg1Profile.BG1_IDPS);
mac = getMacFromIdps(idps);
// module.handleNotify(mac, BG1, Bg1Profile.ACTION_BG1_IDPS, intent.getStringExtra(Bg1Profile.BG1_IDPS));
break;
default:
module.handleNotify(mac, BG1, intent.getAction(), jsonObject.toString());
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
String getMacFromIdps(String idps) {
//{"IDPS":[{"DeviceId":"9D300A5682","FirmWare":"13.5.0","HardWare":"13.6.0"}]}
try {
JSONObject sourceObject = new JSONObject(idps);
JSONArray array = sourceObject.getJSONArray("IDPS");
JSONObject object = (JSONObject) array.get(0);
return object.getString("DeviceId");
} catch (Exception e) {
}
return "";
}
JSONObject formatIdps(String idps) {
try {
JSONObject sourceObject = new JSONObject(idps);
JSONArray array = sourceObject.getJSONArray("IDPS");
return (JSONObject) array.get(0);
} catch (Exception e) {
}
return null;
}
String getErrorDescription(String action, int err_num) {
switch (action) {
case Bg1Profile.ACTION_BG1_CONNECT_RESULT:
case Bg1Profile.ACTION_BG1_SENDCODE_RESULT:
if ((err_num >= 6 && err_num <= 13) || (err_num >= 19 && err_num <= 28)) {
return "Send code failed.";
}
switch (err_num) {
case 0:
return "Success.";
case 1:
return "Hand shake timeout.";
case 2:
case 16://1307
return "Get idps failed.";
case 3:
return "Register clientId failed.";
case 5:
case 17://1307
return "Identify failed.";
case 32:
return "Connect timeout";
case 18:
return "Set bottleId failed.";
default:
return "Unknown.";
}
case Bg1Profile.ACTION_BG1_MEASURE_ERROR:
switch (err_num) {
case 0:
return "Battery is low.";
case 1:
return "Glucose test result is out of the measurement range.";
case 2:
return "Unknown interference detected, please repeat the test.";
case 3:
return "Strip is used or unknown moisture detected, discard the test strip and repeat the test with a new strip.";
case 4:
return "Communication error,resend the code to repeat the test.";
case 5:
case 6:
return "The environmental temperature is beyond normal range, place the meter at room temperature for at least 30 minutes, then repeat the test.";
case 7:
return "Authentication failed more than 10 times.";
case 8:
return "Communication error, please repeat the test.";
case 9:
case 10:
case 11:
return "Communication error, please repeat the test. If the problem persists, contact iHealth customer service for assistance.";
case 12:
return "Glucose test result is low.";
case 13:
return "Glucose test result is high.";
case 400:
return "Parameters out of range.";
case 401:
return "Dolby is on, please turn it off.";
default:
return "Unknown.";
}
default:
return "Unknown.";
}
}
};
@ReactMethod
public void disconnectDevice(String mac, String type) {
if (type.equals(BG1)) {
Bg1Control.getInstance().disconnect();
} else {
iHealthDevicesManager.getInstance().disconnectDevice(mac, type);
}
}
@ReactMethod
public void sdkAuthWithLicense(String license) {
boolean result = false;
try {
InputStream ins = mContext.getAssets().open(license);
byte[] licenseBuffer = new byte[ins.available()];
ins.read(licenseBuffer);
ins.close();
result = iHealthDevicesManager.getInstance().sdkAuthWithLicense(licenseBuffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
WritableMap writableMap = Arguments.createMap();
writableMap.putBoolean("access", result);
sendEvent(Event_Authenticate_Result, writableMap);
}
@ReactMethod
public void isSupportOTG(Callback callback) {
boolean isSupportOTG = iHealthDevicesManager.getInstance().checkSupportOTG();
callback.invoke(isSupportOTG);
}
}
|