forked from SocketMobile/react-native-capture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (91 loc) · 4.18 KB
/
index.js
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
import { Platform, NativeEventEmitter, NativeModules } from 'react-native';
import { Capture, SktErrors, CapturePropertyIds, CapturePropertyTypes, CaptureProperty, CaptureEventIds, CaptureEventTypes, CaptureEvent, CaptureDeviceType, CaptureDataSourceID, CaptureDataSourceFlags, CaptureDataSourceStatus, DataConfirmationMode, DeviceDataAcknowledgment, SecurityMode, Trigger, DeletePairing, SoundActionType, SoundFrequency, RumbleActionType, LocalDecodeAction, DataConfirmationLed, DataConfirmationBeep, DataConfirmationRumble, Flash, SoftScan, PowerState, MonitorDbg, Counter, Disconnect, ProfileSelect, ProfileConfig, Notifications, Timer, DataFormat, TriggerMode, ConnectReason, StartUpRoleSpp, ConnectBeepConfig, StandConfig } from 'socketmobile-capturejs';
const { JsonRpcTransport } = NativeModules;
const eventEmitter = new NativeEventEmitter(JsonRpcTransport);
const onCaptureEvent = e => {
console.log('index onCaptureEvent: ', e);
if(JsonRpcTransport.logger){
JsonRpcTransport.logger.log('<= ', e);
}
const event = JSON.parse(e);
if(JsonRpcTransport.onNotification) {
JsonRpcTransport.onNotification(event);
}
};
JsonRpcTransport.open = (host, notification) => {
console.log('JsonRpcTransport.open callback: ', notification);
console.log('JsonRpcTransport.open this: ', this);
JsonRpcTransport.onNotification = notification;
eventEmitter.subscription = eventEmitter.addListener('onCaptureEvent', onCaptureEvent);
return JsonRpcTransport.openTransport(host).then(result => result.transport);
}
JsonRpcTransport.send = (handle, jsonRpc) => {
JsonRpcTransport.logger.log('=> ',jsonRpc);
return JsonRpcTransport.sendTransport(handle, JSON.stringify(jsonRpc))
.then(response => {
JsonRpcTransport.logger.log('<= ',response);
return JSON.parse(response);
});
}
JsonRpcTransport.close = (handle) => {
eventEmitter.subscription.remove();
return JsonRpcTransport.closeTransport(handle);
}
const getOptions = (platform, options, logger) => {
const final = options || {};
const noLogger = {log:()=>{}};
if (platform.OS === 'ios') {
JsonRpcTransport.logger = logger || noLogger;
final.transport = JsonRpcTransport;
}
else {
JsonRpcTransport.startCaptureService();
}
return final;
};
class CaptureRn extends Capture {
constructor(logger) {
super(logger);
}
openForAndroid(tries, appInfo, callback, options) {
const promise = new Promise((resolve, reject) => {
let interval;
const openRetry = () => {
clearInterval(interval);
super.open(appInfo, callback, options)
.then(result => resolve(result))
.catch(err => {
console.log('android retry error: ', err);
if(tries > 0){
tries -= 1;
interval = setInterval(openRetry, 250);
}
else {
reject(err);
}
})
};
interval = setInterval(openRetry, 250);
});
return promise;
}
open(appInfo, callback, options) {
const finalOptions = getOptions(Platform, options, this.logger);
if(Platform.OS === 'android'){
var newAppInfo = genAppInfo(appInfo, true)
return this.openForAndroid(10, newAppInfo, callback, options);
}
var newAppInfo = genAppInfo(appInfo)
return super.open(newAppInfo, callback, finalOptions);
}
}
const genAppInfo = (appInfo, isAndroid) =>{
var id = isAndroid ? 'appIdAndroid' : 'appIdIos'
var key = isAndroid ? 'appKeyAndroid' : 'appKeyIos'
return {
...appInfo,
appId: appInfo[id] || appInfo.appId,
appKey: appInfo[key] || appInfo.appKey
}
}
export { CaptureRn, SktErrors, CapturePropertyIds, CapturePropertyTypes, CaptureProperty, CaptureEventIds, CaptureEventTypes, CaptureEvent, CaptureDeviceType, CaptureDataSourceID, CaptureDataSourceFlags, CaptureDataSourceStatus, DataConfirmationMode, DeviceDataAcknowledgment, SecurityMode, Trigger, DeletePairing, SoundActionType, SoundFrequency, RumbleActionType, LocalDecodeAction, DataConfirmationLed, DataConfirmationBeep, DataConfirmationRumble, Flash, SoftScan, PowerState, MonitorDbg, Counter, Disconnect, ProfileSelect, ProfileConfig, Notifications, Timer, DataFormat, TriggerMode, ConnectReason, StartUpRoleSpp, ConnectBeepConfig, StandConfig };