-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRNPrinterModule.java
More file actions
79 lines (58 loc) · 2.16 KB
/
RNPrinterModule.java
File metadata and controls
79 lines (58 loc) · 2.16 KB
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
package com.pinmi.react.printer;
import android.hardware.usb.UsbDevice;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.pinmi.react.printer.adapter.USBPrinterAdapter;
import java.util.List;
public class RNPrinterModule extends ReactContextBaseJavaModule {
private USBPrinterAdapter adapter;
public RNPrinterModule(ReactApplicationContext reactContext){
super(reactContext);
this.adapter = USBPrinterAdapter.getInstance();
this.adapter.init(reactContext);
}
@Override
public String getName() {
return "RNPrinter";
}
@ReactMethod
public void getUSBDeviceList(Promise promise) {
List<UsbDevice> usbDevices = adapter.getDeviceList();
WritableArray pairedDeviceList = Arguments.createArray();
for (UsbDevice usbDevice : usbDevices) {
WritableMap deviceMap = Arguments.createMap();
deviceMap.putString("device_name", usbDevice.getDeviceName());
deviceMap.putInt("device_id", usbDevice.getDeviceId());
deviceMap.putInt("vendor_id", usbDevice.getVendorId());
deviceMap.putInt("product_id", usbDevice.getProductId());
pairedDeviceList.pushMap(deviceMap);
}
promise.resolve(pairedDeviceList);
}
@ReactMethod
public void connectPrinter(Integer vendorId, Integer productId, Promise promise) {
if(!adapter.selectDevice(vendorId, productId)){
promise.resolve(false);
}else{
promise.resolve(true);
}
}
@ReactMethod
public void closeConn(Promise promise) {
adapter.closeConnectionIfExists();
promise.resolve(null);
}
@ReactMethod
public void printText(String text) {
adapter.printText(text);
}
@ReactMethod
public void printRawData(String base64Data) {
adapter.printRawData(base64Data);
}
}