-
Notifications
You must be signed in to change notification settings - Fork 21
/
BluetoothPrinter.java
376 lines (323 loc) · 11.4 KB
/
BluetoothPrinter.java
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
package cz.martinforejt.bluetooth_printer;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Created by Martin Forejt on 28.06.2017.
*/
public class BluetoothPrinter {
public static final int ALIGN_CENTER = 100;
public static final int ALIGN_RIGHT = 101;
public static final int ALIGN_LEFT = 102;
private static final byte[] NEW_LINE = {10};
private static final byte[] ESC_ALIGN_CENTER = new byte[]{0x1b, 'a', 0x01};
private static final byte[] ESC_ALIGN_RIGHT = new byte[]{0x1b, 'a', 0x02};
private static final byte[] ESC_ALIGN_LEFT = new byte[]{0x1b, 'a', 0x00};
private BluetoothDevice printer;
private BluetoothSocket btSocket = null;
private OutputStream btOutputStream = null;
public BluetoothPrinter(BluetoothDevice printer) {
this.printer = printer;
}
public void connectPrinter(final PrinterConnectListener listener) {
new ConnectTask(new ConnectTask.BtConnectListener() {
@Override
public void onConnected(BluetoothSocket socket) {
btSocket = socket;
try {
btOutputStream = socket.getOutputStream();
listener.onConnected();
} catch (IOException e) {
listener.onFailed();
}
}
@Override
public void onFailed() {
listener.onFailed();
}
}).execute(printer);
}
public boolean isConnected() {
return btSocket != null && btSocket.isConnected();
}
public void finish() {
if (btSocket != null) {
try {
btOutputStream.close();
btSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
btSocket = null;
}
}
public boolean printText(String text) {
try {
btOutputStream.write(encodeNonAscii(text).getBytes());
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean printUnicode(byte[] data) {
try {
btOutputStream.write(data);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean printLine() {
return printText("________________________________");
}
public boolean addNewLine() {
return printUnicode(NEW_LINE);
}
public int addNewLines(int count) {
int success = 0;
for (int i = 0; i < count; i++) {
if (addNewLine()) success++;
}
return success;
}
public boolean printImage(Bitmap bitmap) {
byte[] command = decodeBitmap(bitmap);
return printUnicode(command);
}
public void setAlign(int alignType) {
byte[] d;
switch (alignType) {
case ALIGN_CENTER:
d = ESC_ALIGN_CENTER;
break;
case ALIGN_LEFT:
d = ESC_ALIGN_LEFT;
break;
case ALIGN_RIGHT:
d = ESC_ALIGN_RIGHT;
break;
default:
d = ESC_ALIGN_LEFT;
break;
}
try {
btOutputStream.write(d);
} catch (IOException e) {
e.printStackTrace();
}
}
public void setLineSpacing(int lineSpacing) {
byte[] cmd = new byte[]{0x1B, 0x33, (byte) lineSpacing};
printUnicode(cmd);
}
public void setBold(boolean bold) {
byte[] cmd = new byte[]{0x1B, 0x45, bold ? (byte) 1 : 0};
printUnicode(cmd);
}
public void feedPaper() {
addNewLine();
addNewLine();
addNewLine();
addNewLine();
}
private static class ConnectTask extends AsyncTask<BluetoothDevice, Void, BluetoothSocket> {
private BtConnectListener listener;
private ConnectTask(BtConnectListener listener) {
this.listener = listener;
}
@Override
protected BluetoothSocket doInBackground(BluetoothDevice... bluetoothDevices) {
BluetoothDevice device = bluetoothDevices[0];
UUID uuid = device.getUuids()[0].getUuid();
BluetoothSocket socket = null;
boolean connected = true;
try {
socket = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
}
try {
socket.connect();
} catch (IOException e) {
try {
socket = (BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[]{int.class})
.invoke(device, 1);
socket.connect();
} catch (Exception e2) {
connected = false;
}
}
return connected ? socket : null;
}
@Override
protected void onPostExecute(BluetoothSocket bluetoothSocket) {
if (listener != null) {
if (bluetoothSocket != null) listener.onConnected(bluetoothSocket);
else listener.onFailed();
}
}
private interface BtConnectListener {
void onConnected(BluetoothSocket socket);
void onFailed();
}
}
public interface PrinterConnectListener {
void onConnected();
void onFailed();
}
private static String encodeNonAscii(String text) {
return text.replace('á', 'a')
.replace('č', 'c')
.replace('ď', 'd')
.replace('é', 'e')
.replace('ě', 'e')
.replace('í', 'i')
.replace('ň', 'n')
.replace('ó', 'o')
.replace('ř', 'r')
.replace('š', 's')
.replace('ť', 't')
.replace('ú', 'u')
.replace('ů', 'u')
.replace('ý', 'y')
.replace('ž', 'z')
.replace('Á', 'A')
.replace('Č', 'C')
.replace('Ď', 'D')
.replace('É', 'E')
.replace('Ě', 'E')
.replace('Í', 'I')
.replace('Ň', 'N')
.replace('Ó', 'O')
.replace('Ř', 'R')
.replace('Š', 'S')
.replace('Ť', 'T')
.replace('Ú', 'U')
.replace('Ů', 'U')
.replace('Ý', 'Y')
.replace('Ž', 'Z');
}
public static byte[] decodeBitmap(Bitmap bmp) {
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
List<String> list = new ArrayList<>();
StringBuffer sb;
int zeroCount = bmpWidth % 8;
String zeroStr = "";
if (zeroCount > 0) {
for (int i = 0; i < (8 - zeroCount); i++) zeroStr = zeroStr + "0";
}
for (int i = 0; i < bmpHeight; i++) {
sb = new StringBuffer();
for (int j = 0; j < bmpWidth; j++) {
int color = bmp.getPixel(j, i);
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = color & 0xff;
if (r > 160 && g > 160 && b > 160) sb.append("0");
else sb.append("1");
}
if (zeroCount > 0) sb.append(zeroStr);
list.add(sb.toString());
}
List<String> bmpHexList = binaryListToHexStringList(list);
String commandHexString = "1D763000";
String widthHexString = Integer
.toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8 : (bmpWidth / 8 + 1));
if (widthHexString.length() > 2) {
return null;
} else if (widthHexString.length() == 1) {
widthHexString = "0" + widthHexString;
}
widthHexString = widthHexString + "00";
String heightHexString = Integer.toHexString(bmpHeight);
if (heightHexString.length() > 2) {
return null;
} else if (heightHexString.length() == 1) {
heightHexString = "0" + heightHexString;
}
heightHexString = heightHexString + "00";
List<String> commandList = new ArrayList<>();
commandList.add(commandHexString + widthHexString + heightHexString);
commandList.addAll(bmpHexList);
return hexList2Byte(commandList);
}
private static List<String> binaryListToHexStringList(List<String> list) {
List<String> hexList = new ArrayList<>();
for (String binaryStr : list) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < binaryStr.length(); i += 8) {
String str = binaryStr.substring(i, i + 8);
String hexString = strToHexString(str);
sb.append(hexString);
}
hexList.add(sb.toString());
}
return hexList;
}
private static String hexStr = "0123456789ABCDEF";
private static String[] binaryArray = {"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"};
private static String strToHexString(String binaryStr) {
String hex = "";
String f4 = binaryStr.substring(0, 4);
String b4 = binaryStr.substring(4, 8);
for (int i = 0; i < binaryArray.length; i++) {
if (f4.equals(binaryArray[i]))
hex += hexStr.substring(i, i + 1);
}
for (int i = 0; i < binaryArray.length; i++) {
if (b4.equals(binaryArray[i]))
hex += hexStr.substring(i, i + 1);
}
return hex;
}
private static byte[] hexList2Byte(List<String> list) {
List<byte[]> commandList = new ArrayList<>();
for (String hexStr : list) commandList.add(hexStringToBytes(hexStr));
return sysCopy(commandList);
}
private static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) return null;
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte[] sysCopy(List<byte[]> srcArrays) {
int len = 0;
for (byte[] srcArray : srcArrays) {
len += srcArray.length;
}
byte[] destArray = new byte[len];
int destLen = 0;
for (byte[] srcArray : srcArrays) {
System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
destLen += srcArray.length;
}
return destArray;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public BluetoothSocket getSocket() {
return btSocket;
}
public BluetoothDevice getDevice() {
return printer;
}
}