Skip to content

Commit be6494f

Browse files
committed
change promise call to callback function
1 parent 0ffc04d commit be6494f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1878
-694
lines changed

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Example/
2+
.git
3+
.gitignore
4+
.idea

README.md

+242-35
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,260 @@
1+
# react-native-printer
2+
Fork of react-native-printer and add implement for auto connect printer with usb
3+
A React Native Library to support USB/BLE/Net printer for Android platform
14

2-
# react-native-thermal-receipt-printer
5+
## Installation
36

4-
## Getting started
7+
```
8+
npm install react-native-printer --save
59
6-
`$ npm install react-native-thermal-receipt-printer --save`
10+
```
711

8-
### Mostly automatic installation
12+
## Integrate module
913

10-
`$ react-native link react-native-thermal-receipt-printer`
14+
To integrate `react-native-printer` with the rest of your react app just execute:
1115

12-
### Manual installation
16+
```
17+
react-native link react-native-printer
1318
19+
```
1420

15-
#### iOS
21+
## Usage
1622

17-
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
18-
2. Go to `node_modules``react-native-thermal-receipt-printer` and add `RNThermalReceiptPrinter.xcodeproj`
19-
3. In XCode, in the project navigator, select your project. Add `libRNThermalReceiptPrinter.a` to your project's `Build Phases``Link Binary With Libraries`
20-
4. Run your project (`Cmd+R`)<
23+
```javascript
24+
import { USBPrinter, NetPrinter, BLEPrinter } from 'react-native-printer';
2125

22-
#### Android
26+
USBPrinter.printText('<C>这是一个测试打印</C>')
27+
USBPrinter.printBill("<C>这是一段打印测试文字</C>")
2328

24-
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
25-
- Add `import com.reactlibrary.RNThermalReceiptPrinterPackage;` to the imports at the top of the file
26-
- Add `new RNThermalReceiptPrinterPackage()` to the list returned by the `getPackages()` method
27-
2. Append the following lines to `android/settings.gradle`:
28-
```
29-
include ':react-native-thermal-receipt-printer'
30-
project(':react-native-thermal-receipt-printer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-thermal-receipt-printer/android')
31-
```
32-
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
33-
```
34-
compile project(':react-native-thermal-receipt-printer')
35-
```
29+
```
3630

37-
#### Windows
38-
[Read it! :D](https://github.com/ReactWindows/react-native)
31+
## Example
3932

40-
1. In Visual Studio add the `RNThermalReceiptPrinter.sln` in `node_modules/react-native-thermal-receipt-printer/windows/RNThermalReceiptPrinter.sln` folder to their solution, reference from their app.
41-
2. Open up your `MainPage.cs` app
42-
- Add `using Thermal.Receipt.Printer.RNThermalReceiptPrinter;` to the usings at the top of the file
43-
- Add `new RNThermalReceiptPrinterPackage()` to the `List<IReactPackage>` returned by the `Packages` method
33+
### USBPrinter
34+
35+
Printer structure:
36+
```
37+
{
38+
device_name: '/usb/lp1',
39+
vendor_id: 1155,
40+
product_id: 22304,
41+
}
42+
```
43+
44+
45+
```javascript
46+
47+
componentDidMount = () => {
48+
if(Platform.OS == 'android'){
49+
USBPrinter.init().then(()=> {
50+
//list printers
51+
USBPrinter.getDeviceList()
52+
.then(printers => {
53+
this.setState(Object.assign({}, this.state, {printers: printers}))
54+
});
55+
56+
//connect printer
57+
vendorID = 1155
58+
productId = 22304
59+
USBPrinter.connectPrinter(vendorID, productId).then(
60+
(printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})),
61+
error => console.warn(error))
62+
})
63+
}
64+
}
65+
66+
printTextTest = () => {
67+
if(this.state.currentPrinter) {
68+
USBPrinter.printText("<C>这是一段打印测试文字</C>\n");
69+
}else{
70+
console.log("没有设置打印机")
71+
}
72+
73+
}
74+
75+
printBillTest = () => {
76+
if(this.state.currentPrinter) {
77+
USBPrinter.printBill("<C>这是一段打印测试文字</C>");
78+
}else{
79+
console.log("没有设置打印机")
80+
}
81+
}
82+
83+
...
84+
85+
render() {
86+
return (
87+
<View style={styles.container}>
88+
{
89+
this.state.printers.map(printer => (
90+
<TouchableOpacity key={printer.device_id} onPress={(printer) => this._connectPrinter(printer.vendor_id, printer.product_id)}>
91+
{`device_name: ${printer.device_name}, device_id: ${printer.device_id}, vendor_id: ${printer.vendor_id}, product_id: ${printer.product_id}`}
92+
</TouchableOpacity>
93+
))
94+
}
95+
<TouchableOpacity onPress={() => this.printTextTest()}>
96+
<Text> Print Text </Text>
97+
</TouchableOpacity>
98+
<TouchableOpacity onPress={() => this.printBillTest()}>
99+
<Text> Print Bill Text </Text>
100+
</TouchableOpacity>
101+
</View>
102+
)
103+
}
104+
105+
...
106+
107+
```
108+
109+
### BLEPrinter
110+
111+
Printer structure:
112+
```
113+
{
114+
device_name: '内部打印机',
115+
inner_mac_address: 'XXXXX-XXXXXXXX',
116+
}
117+
```
44118

45119

46-
## Usage
47120
```javascript
48-
import RNThermalReceiptPrinter from 'react-native-thermal-receipt-printer';
49121

50-
// TODO: What to do with the module?
51-
RNThermalReceiptPrinter;
122+
componentDidMount = () => {
123+
if(Platform.OS == 'android'){
124+
USBPrinter.init().then(()=> {
125+
//list printers
126+
USBPrinter.getDeviceList()
127+
.then(printers => {
128+
this.setState(Object.assign({}, this.state, {printers: printers}))
129+
});
130+
131+
132+
})
133+
}
134+
}
135+
136+
_connectPrinter => (inner_mac_address) => {
137+
if(Platform.OS == 'android'){
138+
//connect printer
139+
USBPrinter.connectPrinter(inner_mac_address).then(
140+
(printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})),
141+
error => console.warn(error))
142+
}
143+
}
144+
145+
printTextTest = () => {
146+
if(this.state.currentPrinter) {
147+
USBPrinter.printText("<C>这是一段打印测试文字</C>\n");
148+
}else{
149+
console.log("没有设置打印机")
150+
}
151+
152+
}
153+
154+
printBillTest = () => {
155+
if(this.state.currentPrinter) {
156+
USBPrinter.printBill("<C>这是一段打印测试文字</C>");
157+
}else{
158+
console.log("没有设置打印机")
159+
}
160+
}
161+
162+
...
163+
164+
render() {
165+
return (
166+
<View style={styles.container}>
167+
{
168+
this.state.printers.map(printer => (
169+
<TouchableOpacity key={printer.inner_mac_address} onPress={(printer) => this._connectPrinter(printer.inner_mac_address)}>
170+
{`device_name: ${printer.device_name}, inner_mac_address: ${printer.inner_mac_address}`}
171+
</TouchableOpacity>
172+
))
173+
}
174+
<TouchableOpacity onPress={() => this.printTextTest()}>
175+
<Text> Print Text </Text>
176+
</TouchableOpacity>
177+
<TouchableOpacity onPress={() => this.printBillTest()}>
178+
<Text> Print Bill Text </Text>
179+
</TouchableOpacity>
180+
</View>
181+
)
182+
}
183+
184+
...
185+
186+
```
187+
188+
### NetPrinter
189+
190+
Printer structure:
52191
```
53-
192+
{
193+
device_name: "192.168.10.241:9100",
194+
host: '192.168.10.241',
195+
port: 9100
196+
}
197+
```
198+
199+
200+
```javascript
201+
202+
componentDidMount = () => {
203+
if(Platform.OS == 'android'){
204+
NetPrinter.init().then(() => {
205+
this.setState(Object.assign({}, this.state, {printers: [{host: '192.168.10.241', port: 9100}]}))
206+
})
207+
}
208+
}
209+
210+
_connectPrinter => (host, port) => {
211+
if(Platform.OS == 'android'){
212+
//connect printer
213+
NetPrinter.connectPrinter(host, port).then(
214+
(printer) => this.setState(Object.assign({}, this.state, {currentPrinter: printer})),
215+
error => console.warn(error))
216+
}
217+
}
218+
219+
printTextTest = () => {
220+
if(this.state.currentPrinter) {
221+
NetPrinter.printText("<C>这是一段打印测试文字</C>\n");
222+
}else{
223+
console.log("没有设置打印机")
224+
}
225+
226+
}
227+
228+
printBillTest = () => {
229+
if(this.state.currentPrinter) {
230+
NetPrinter.printBill("<C>这是一段打印测试文字</C>");
231+
}else{
232+
console.log("没有设置打印机")
233+
}
234+
}
235+
236+
...
237+
238+
render() {
239+
return (
240+
<View style={styles.container}>
241+
{
242+
this.state.printers.map(printer => (
243+
<TouchableOpacity key={printer.device_id} onPress={(printer) => this._connectPrinter(printer.host, printer.port)}>
244+
{`device_name: ${printer.device_name}, host: ${printer.host}, port: ${printer.port}`}
245+
</TouchableOpacity>
246+
))
247+
}
248+
<TouchableOpacity onPress={() => this.printTextTest()}>
249+
<Text> Print Text </Text>
250+
</TouchableOpacity>
251+
<TouchableOpacity onPress={() => this.printBillTest()}>
252+
<Text> Print Bill Text </Text>
253+
</TouchableOpacity>
254+
</View>
255+
)
256+
}
257+
258+
...
259+
260+
```

android/build.gradle

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
2-
buildscript {
3-
repositories {
4-
jcenter()
5-
}
6-
7-
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.3.1'
9-
}
10-
}
11-
121
apply plugin: 'com.android.library'
132

143
android {
154
compileSdkVersion 23
165
buildToolsVersion "23.0.1"
176

187
defaultConfig {
19-
minSdkVersion 16
8+
minSdkVersion 15
209
targetSdkVersion 22
2110
versionCode 1
2211
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
2315
}
24-
lintOptions {
25-
abortOnError false
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
2621
}
2722
}
2823

29-
repositories {
30-
mavenCentral()
31-
}
32-
3324
dependencies {
34-
compile 'com.facebook.react:react-native:+'
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:23.0.1"
30+
compile "com.facebook.react:react-native:+"
31+
testCompile 'junit:junit:4.12'
3532
}
36-
52.4 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Fri Sep 22 20:18:20 CST 2017
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 commit comments

Comments
 (0)