Skip to content

Commit 9b2a207

Browse files
committed
Release 0.0.4
0 parents  commit 9b2a207

File tree

12 files changed

+591
-0
lines changed

12 files changed

+591
-0
lines changed

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Example/
2+
.git
3+
.gitignore
4+
.idea

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# react-native-usb-printer
2+
3+
A React Native Library to support USB printer for Android platform
4+
5+
## Installation
6+
7+
```
8+
npm install react-native-usb-printer --save
9+
```
10+
11+
## Integrate module
12+
13+
To integrate `react-native-usb-printer` with the rest of your react app just execute:
14+
```
15+
react-native link react-native-usb-printer
16+
```
17+
18+
## Usage
19+
20+
```javascript
21+
import { RNUSBPrinter } from 'react-native-usb-printer';
22+
23+
RNUSBPrinter.printText('This is test print.')
24+
RNUSBPrinter.printBillTextWithCut("<C>This is test print.</C>")
25+
RNUSBPrinter.printBillTextWithCut("<M>This is test print.</M>")
26+
RNUSBPrinter.printBillTextWithCut("<CM>This is test print.</CM>")
27+
28+
```
29+
30+
## Example
31+
32+
```javascript
33+
34+
componentDidMount = async () => {
35+
var devices = await RNUSBPrinter.getUSBDeviceList();
36+
vendorID = 1155
37+
productId = 22304
38+
let printedSelected = await RNUSBPrinter.connectPrinter(vendorId, productId);
39+
this.setState(Object.assign({}, this.state, {
40+
printedSelected: printedSelected,
41+
devices: devices,
42+
}));
43+
}
44+
45+
printTest = () => {
46+
if(this.state.printedSelected) {
47+
RNUSBPrinter.printText("<C>This is test print.</C>\n");
48+
}else{
49+
console.log("No printer connected")
50+
}
51+
52+
}
53+
54+
printRawDataTest = () => {
55+
if(this.state.printedSelected) {
56+
RNUSBPrinter.printBillTextWithCut("<C>This is test print.</C>");
57+
}else{
58+
console.log("No printer connected")
59+
}
60+
}
61+
62+
...
63+
64+
render() {
65+
return (
66+
<View style={styles.container}>
67+
{
68+
this.state.deviceList.map(device => (
69+
<Text key={device.device_id}>
70+
{`device_name: ${device.device_name}, device_id: ${device.device_id}, vendor_id: ${device.vendor_id}, product_id: ${device.product_id}`}
71+
</Text>
72+
))
73+
}
74+
<TouchableOpacity onPress={() => this.printTest()}>
75+
<Text> Print Text </Text>
76+
</TouchableOpacity>
77+
<TouchableOpacity onPress={() => this.printRawDataTest()}>
78+
<Text> Print Bill Text </Text>
79+
</TouchableOpacity>
80+
</View>
81+
)
82+
}
83+
...
84+
85+
```

android/build.gradle

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.1"
6+
7+
defaultConfig {
8+
minSdkVersion 15
9+
targetSdkVersion 26
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
15+
}
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
}
23+
24+
dependencies {
25+
compile fileTree(dir: 'libs', include: ['*.jar'])
26+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27+
exclude group: 'com.android.support', module: 'support-annotations'
28+
})
29+
compile 'com.android.support:appcompat-v7:21.2.1'
30+
compile "com.facebook.react:react-native:+"
31+
testCompile 'junit:junit:4.12'
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.pinmi.react.printer;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.pinmi.react.printer.test", appContext.getPackageName());
25+
}
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
3+
package="com.pinmi.react.printer">
4+
5+
<application android:allowBackup="true" android:label="@string/app_name"
6+
android:supportsRtl="true">
7+
8+
</application>
9+
10+
</manifest>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.pinmi.react.printer;
2+
3+
4+
import android.hardware.usb.UsbDevice;
5+
6+
import com.facebook.react.bridge.Arguments;
7+
import com.facebook.react.bridge.Promise;
8+
import com.facebook.react.bridge.ReactApplicationContext;
9+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
10+
import com.facebook.react.bridge.ReactMethod;
11+
import com.facebook.react.bridge.WritableArray;
12+
import com.facebook.react.bridge.WritableMap;
13+
import com.pinmi.react.printer.adapter.USBPrinterAdapter;
14+
15+
import java.util.List;
16+
17+
/**
18+
* Created by bondwp322 on 2017/10/9.
19+
*/
20+
21+
public class RNPrinterModule extends ReactContextBaseJavaModule {
22+
23+
24+
private USBPrinterAdapter adapter;
25+
public RNPrinterModule(ReactApplicationContext reactContext){
26+
super(reactContext);
27+
this.adapter = USBPrinterAdapter.getInstance();
28+
this.adapter.init(reactContext);
29+
}
30+
31+
@Override
32+
public String getName() {
33+
return "RNPrinter";
34+
}
35+
36+
37+
@ReactMethod
38+
public void getUSBDeviceList(Promise promise) {
39+
List<UsbDevice> usbDevices = adapter.getDeviceList();
40+
WritableArray pairedDeviceList = Arguments.createArray();
41+
for (UsbDevice usbDevice : usbDevices) {
42+
WritableMap deviceMap = Arguments.createMap();
43+
deviceMap.putString("device_name", usbDevice.getDeviceName());
44+
deviceMap.putInt("device_id", usbDevice.getDeviceId());
45+
deviceMap.putInt("vendor_id", usbDevice.getVendorId());
46+
deviceMap.putInt("product_id", usbDevice.getProductId());
47+
pairedDeviceList.pushMap(deviceMap);
48+
}
49+
promise.resolve(pairedDeviceList);
50+
}
51+
52+
53+
@ReactMethod
54+
public void connectPrinter(Integer vendorId, Integer productId, Promise promise) {
55+
if(!adapter.selectDevice(vendorId, productId)){
56+
promise.resolve(false);
57+
}else{
58+
promise.resolve(true);
59+
}
60+
}
61+
62+
63+
@ReactMethod
64+
public void closeConn(Promise promise) {
65+
adapter.closeConnectionIfExists();
66+
promise.resolve(null);
67+
}
68+
69+
70+
71+
72+
@ReactMethod
73+
public void printText(String text) {
74+
adapter.printText(text);
75+
}
76+
77+
@ReactMethod
78+
public void printRawData(String base64Data) {
79+
adapter.printRawData(base64Data);
80+
}
81+
82+
}
83+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pinmi.react.printer;
2+
3+
import com.facebook.react.ReactPackage;
4+
import com.facebook.react.bridge.NativeModule;
5+
import com.facebook.react.bridge.ReactApplicationContext;
6+
import com.facebook.react.uimanager.ViewManager;
7+
8+
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.List;
11+
12+
/**
13+
* Created by bondwp322 on 2017/10/9.
14+
*/
15+
16+
public class RNPrinterPackage implements ReactPackage {
17+
@Override
18+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
19+
return Arrays.asList(new NativeModule[]{
20+
new RNPrinterModule(reactContext)
21+
});
22+
}
23+
24+
@Override
25+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
26+
return Collections.emptyList();
27+
}
28+
}

0 commit comments

Comments
 (0)