@@ -124,6 +124,8 @@ |
||
124 | 124 |
|
125 | 125 |
<service android:name=".service.OrderDealService"/> |
126 | 126 |
|
127 |
+ <service android:name=".printer.PrinterService"/> |
|
128 |
+ |
|
127 | 129 |
<service |
128 | 130 |
android:name="com.gprinter.service.GpPrintService" |
129 | 131 |
android:label="GpPrintService" |
@@ -0,0 +1,176 @@ |
||
1 |
+package ai.pai.lensman.printer; |
|
2 |
+ |
|
3 |
+import android.app.Service; |
|
4 |
+import android.bluetooth.BluetoothDevice; |
|
5 |
+import android.content.ComponentName; |
|
6 |
+import android.content.Context; |
|
7 |
+import android.content.Intent; |
|
8 |
+import android.content.ServiceConnection; |
|
9 |
+import android.os.Binder; |
|
10 |
+import android.os.IBinder; |
|
11 |
+import android.util.Base64; |
|
12 |
+import android.util.Log; |
|
13 |
+ |
|
14 |
+import com.android.common.utils.LogHelper; |
|
15 |
+import com.gprinter.aidl.GpService; |
|
16 |
+import com.gprinter.command.EscCommand; |
|
17 |
+import com.gprinter.command.GpCom; |
|
18 |
+import com.gprinter.io.PortParameters; |
|
19 |
+import com.gprinter.service.GpPrintService; |
|
20 |
+ |
|
21 |
+import org.apache.commons.lang.ArrayUtils; |
|
22 |
+ |
|
23 |
+import java.util.Vector; |
|
24 |
+ |
|
25 |
+import ai.pai.lensman.R; |
|
26 |
+ |
|
27 |
+ |
|
28 |
+public class PrinterService extends Service { |
|
29 |
+ |
|
30 |
+ private GpService mGpService; |
|
31 |
+ private GpServiceConnection conn = null; |
|
32 |
+ |
|
33 |
+ public static final int ERROR_CODE_PRINTER_SERVICE_OFF = 1000; |
|
34 |
+ public static final int ERROR_CODE_PRINTER_SERVICE_EXCEPTION = 2000; |
|
35 |
+ |
|
36 |
+ private static final String TAG = "PrinterService"; |
|
37 |
+ |
|
38 |
+ |
|
39 |
+ class GpServiceConnection implements ServiceConnection { |
|
40 |
+ @Override |
|
41 |
+ public void onServiceDisconnected(ComponentName name) { |
|
42 |
+ Log.i(TAG, "onServiceDisconnected() called"); |
|
43 |
+ mGpService = null; |
|
44 |
+ } |
|
45 |
+ |
|
46 |
+ @Override |
|
47 |
+ public void onServiceConnected(ComponentName name, IBinder service) { |
|
48 |
+ Log.i(TAG, "onServiceConnected() called"); |
|
49 |
+ mGpService = GpService.Stub.asInterface(service); |
|
50 |
+ } |
|
51 |
+ } |
|
52 |
+ |
|
53 |
+ @Override |
|
54 |
+ public void onCreate() { |
|
55 |
+ super.onCreate(); |
|
56 |
+ } |
|
57 |
+ |
|
58 |
+ @Override |
|
59 |
+ public IBinder onBind(Intent intent) { |
|
60 |
+ return new PrintServiceBinder(); |
|
61 |
+ } |
|
62 |
+ |
|
63 |
+ @Override |
|
64 |
+ public int onStartCommand(Intent intent, int flags, int startId) { |
|
65 |
+ if(mGpService == null){ |
|
66 |
+ startAndBindPrintService(); |
|
67 |
+ } |
|
68 |
+ return super.onStartCommand(intent, flags, startId); |
|
69 |
+ } |
|
70 |
+ |
|
71 |
+ @Override |
|
72 |
+ public void onDestroy() { |
|
73 |
+ super.onDestroy(); |
|
74 |
+ if (conn != null) { |
|
75 |
+ unbindService(conn); |
|
76 |
+ } |
|
77 |
+ } |
|
78 |
+ |
|
79 |
+ |
|
80 |
+ private void startAndBindPrintService() { |
|
81 |
+ Intent intent = new Intent(this, GpPrintService.class); |
|
82 |
+ startService(intent); |
|
83 |
+ conn = new GpServiceConnection(); |
|
84 |
+ bindService(intent, conn, Context.BIND_AUTO_CREATE); |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ public String queryPrinterStatus() { |
|
88 |
+ String str; |
|
89 |
+ try { |
|
90 |
+ int status = mGpService.queryPrinterStatus(0, 10000); |
|
91 |
+ if (status == GpCom.STATE_NO_ERR) { |
|
92 |
+ str = "打印机正常"; |
|
93 |
+ } else { |
|
94 |
+ str = "打印机 "; |
|
95 |
+ if ((byte) (status & GpCom.STATE_OFFLINE) > 0) { |
|
96 |
+ str += "脱机"; |
|
97 |
+ } |
|
98 |
+ if ((byte) (status & GpCom.STATE_PAPER_ERR) > 0) { |
|
99 |
+ str += "缺纸"; |
|
100 |
+ } |
|
101 |
+ if ((byte) (status & GpCom.STATE_COVER_OPEN) > 0) { |
|
102 |
+ str += "打印机开盖"; |
|
103 |
+ } |
|
104 |
+ if ((byte) (status & GpCom.STATE_ERR_OCCURS) > 0) { |
|
105 |
+ str += "打印机出错"; |
|
106 |
+ } |
|
107 |
+ if ((byte) (status & GpCom.STATE_TIMES_OUT) > 0) { |
|
108 |
+ str += "查询超时"; |
|
109 |
+ } |
|
110 |
+ } |
|
111 |
+ |
|
112 |
+ } catch (Exception e1) { |
|
113 |
+ str = getString(R.string.printer_status_query_fail); |
|
114 |
+ } |
|
115 |
+ return str; |
|
116 |
+ } |
|
117 |
+ |
|
118 |
+ public int printQR(String qrCodeStr) { |
|
119 |
+ if(mGpService==null){ |
|
120 |
+ return ERROR_CODE_PRINTER_SERVICE_OFF; |
|
121 |
+ } |
|
122 |
+ EscCommand esc = new EscCommand(); |
|
123 |
+ /*打印文字*/ |
|
124 |
+ esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, |
|
125 |
+ EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);//取消倍高倍宽 |
|
126 |
+ esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印左对齐 |
|
127 |
+ esc.addText("拍爱\n"); // 打印文字 |
|
128 |
+ esc.addPrintAndLineFeed(); |
|
129 |
+ /*QRCode命令打印 |
|
130 |
+ 此命令只在支持QRCode命令打印的机型才能使用。 |
|
131 |
+ 在不支持二维码指令打印的机型上,则需要发送二维条码图片 |
|
132 |
+ */ |
|
133 |
+ esc.addSelectErrorCorrectionLevelForQRCode((byte) 0x31); //设置纠错等级 |
|
134 |
+ esc.addSelectSizeOfModuleForQRCode((byte)8);//设置qrcode模块大小 |
|
135 |
+ esc.addStoreQRCodeData(qrCodeStr);//设置qrcode内容 |
|
136 |
+ esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印中心对齐 |
|
137 |
+ esc.addPrintQRCode();//打印QRCode |
|
138 |
+ esc.addPrintAndFeedLines((byte) 1); |
|
139 |
+ |
|
140 |
+ Vector<Byte> datas = esc.getCommand(); //发送数据 |
|
141 |
+ Byte[] Bytes = datas.toArray(new Byte[datas.size()]); |
|
142 |
+ byte[] bytes = ArrayUtils.toPrimitive(Bytes); |
|
143 |
+ String str = Base64.encodeToString(bytes, Base64.DEFAULT); |
|
144 |
+ int rel; |
|
145 |
+ try { |
|
146 |
+ rel = mGpService.sendEscCommand(0, str); |
|
147 |
+ } catch (Exception e) { |
|
148 |
+ rel = ERROR_CODE_PRINTER_SERVICE_EXCEPTION; |
|
149 |
+ } |
|
150 |
+ return rel; |
|
151 |
+ } |
|
152 |
+ |
|
153 |
+ public int connectPrinter(BluetoothDevice device) { |
|
154 |
+ if(mGpService==null){ |
|
155 |
+ return ERROR_CODE_PRINTER_SERVICE_OFF; |
|
156 |
+ } |
|
157 |
+ int code ; |
|
158 |
+ try { |
|
159 |
+ code = mGpService.openPort(0, PortParameters.BLUETOOTH, device.getAddress(), 0); |
|
160 |
+ LogHelper.d(TAG,"open port return code ="+code); |
|
161 |
+ } catch (Exception e) { |
|
162 |
+ LogHelper.e(TAG,"connectPrinter open port error "+e); |
|
163 |
+ code = ERROR_CODE_PRINTER_SERVICE_EXCEPTION; |
|
164 |
+ } |
|
165 |
+ return code; |
|
166 |
+ } |
|
167 |
+ |
|
168 |
+ public class PrintServiceBinder extends Binder { |
|
169 |
+ |
|
170 |
+ public PrinterService getService(){ |
|
171 |
+ return PrinterService.this; |
|
172 |
+ } |
|
173 |
+ |
|
174 |
+ } |
|
175 |
+ |
|
176 |
+} |
@@ -10,23 +10,15 @@ import android.content.IntentFilter; |
||
10 | 10 |
import android.content.ServiceConnection; |
11 | 11 |
import android.os.IBinder; |
12 | 12 |
import android.text.TextUtils; |
13 |
-import android.util.Base64; |
|
14 | 13 |
import android.util.Log; |
15 | 14 |
|
16 |
-import com.android.common.utils.LogHelper; |
|
17 |
-import com.gprinter.aidl.GpService; |
|
18 |
-import com.gprinter.command.EscCommand; |
|
19 | 15 |
import com.gprinter.command.GpCom; |
20 | 16 |
import com.gprinter.io.GpDevice; |
21 |
-import com.gprinter.io.PortParameters; |
|
22 | 17 |
import com.gprinter.service.GpPrintService; |
23 | 18 |
|
24 |
-import org.apache.commons.lang.ArrayUtils; |
|
25 |
- |
|
26 | 19 |
import java.util.ArrayList; |
27 | 20 |
import java.util.List; |
28 | 21 |
import java.util.Set; |
29 |
-import java.util.Vector; |
|
30 | 22 |
|
31 | 23 |
import ai.pai.lensman.App; |
32 | 24 |
import ai.pai.lensman.R; |
@@ -36,44 +28,42 @@ import ai.pai.lensman.db.Preferences; |
||
36 | 28 |
public class PrinterSettingPresenter implements PrinterSettingContract.Presenter { |
37 | 29 |
|
38 | 30 |
private Context context; |
31 |
+ private PrinterService printerService; |
|
39 | 32 |
private PrinterSettingContract.View view; |
33 |
+ private PrinterServiceConnection connection; |
|
40 | 34 |
private BluetoothAdapter bluetoothAdapter; |
41 | 35 |
|
42 |
- private GpService mGpService; |
|
43 |
- private PrinterServiceConnection conn = null; |
|
44 |
- |
|
45 | 36 |
private static final String TAG = "PrinterSettingPresenter"; |
46 | 37 |
|
47 |
- class PrinterServiceConnection implements ServiceConnection { |
|
38 |
+ private class PrinterServiceConnection implements ServiceConnection{ |
|
39 |
+ |
|
48 | 40 |
@Override |
49 |
- public void onServiceDisconnected(ComponentName name) { |
|
50 |
- Log.i(TAG, "onServiceDisconnected() called"); |
|
51 |
- mGpService = null; |
|
41 |
+ public void onServiceConnected(ComponentName name, IBinder service) { |
|
42 |
+ printerService = ((PrinterService.PrintServiceBinder)service).getService(); |
|
52 | 43 |
} |
53 | 44 |
|
54 | 45 |
@Override |
55 |
- public void onServiceConnected(ComponentName name, IBinder service) { |
|
56 |
- Log.i(TAG, "onServiceConnected() called"); |
|
57 |
- mGpService = GpService.Stub.asInterface(service); |
|
46 |
+ public void onServiceDisconnected(ComponentName name) { |
|
47 |
+ printerService = null; |
|
58 | 48 |
} |
59 | 49 |
} |
60 | 50 |
|
61 |
- |
|
62 | 51 |
public PrinterSettingPresenter(Context context, PrinterSettingContract.View view){ |
63 | 52 |
this.view = view; |
64 | 53 |
this.context = context; |
54 |
+ connection = new PrinterServiceConnection(); |
|
65 | 55 |
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
66 | 56 |
} |
67 | 57 |
|
68 | 58 |
@Override |
69 | 59 |
public void start() { |
70 | 60 |
registerPrinterBroadcast(); |
71 |
- startAndBindPrintService(); |
|
61 |
+ context.startService(new Intent(context,PrinterService.class)); |
|
62 |
+ context.bindService(new Intent(context,PrinterService.class),connection,Context.BIND_AUTO_CREATE); |
|
72 | 63 |
if(queryBluetoothStatus()){ |
73 | 64 |
view.onBluetoothEnabled(); |
74 | 65 |
view.onPairedDeviceDiscovered(queryPairedDevices()); |
75 | 66 |
discoverNewDevices(); |
76 |
- |
|
77 | 67 |
}else{ |
78 | 68 |
view.onBluetoothDisabled(); |
79 | 69 |
view.showToast(context.getString(R.string.bt_is_disabled)); |
@@ -83,85 +73,39 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
83 | 73 |
@Override |
84 | 74 |
public void stop() { |
85 | 75 |
cancelDiscovery(); |
86 |
- if (conn != null) { |
|
87 |
- App.getAppContext().unbindService(conn); |
|
88 |
- } |
|
89 | 76 |
context.unregisterReceiver(PrinterStatusBroadcastReceiver); |
77 |
+ if(connection!=null){ |
|
78 |
+ App.getAppContext().unbindService(connection); |
|
79 |
+ } |
|
90 | 80 |
} |
91 | 81 |
|
92 | 82 |
@Override |
93 | 83 |
public void queryPrinterStatus() { |
94 |
- try { |
|
95 |
- int status = mGpService.queryPrinterStatus(0, 10000); |
|
96 |
- String str = new String(); |
|
97 |
- if (status == GpCom.STATE_NO_ERR) { |
|
98 |
- str = "打印机正常"; |
|
99 |
- } else { |
|
100 |
- str = "打印机 "; |
|
101 |
- if ((byte) (status & GpCom.STATE_OFFLINE) > 0) { |
|
102 |
- str += "脱机"; |
|
103 |
- } |
|
104 |
- if ((byte) (status & GpCom.STATE_PAPER_ERR) > 0) { |
|
105 |
- str += "缺纸"; |
|
106 |
- } |
|
107 |
- if ((byte) (status & GpCom.STATE_COVER_OPEN) > 0) { |
|
108 |
- str += "打印机开盖"; |
|
109 |
- } |
|
110 |
- if ((byte) (status & GpCom.STATE_ERR_OCCURS) > 0) { |
|
111 |
- str += "打印机出错"; |
|
112 |
- } |
|
113 |
- if ((byte) (status & GpCom.STATE_TIMES_OUT) > 0) { |
|
114 |
- str += "查询超时"; |
|
115 |
- } |
|
116 |
- } |
|
117 |
- view.onPrinterStatusFetched(str); |
|
118 |
- |
|
119 |
- } catch (Exception e1) { |
|
84 |
+ if(printerService==null){ |
|
120 | 85 |
view.showToast(context.getString(R.string.printer_status_query_fail)); |
86 |
+ return; |
|
121 | 87 |
} |
88 |
+ view.onPrinterStatusFetched(printerService.queryPrinterStatus()); |
|
122 | 89 |
} |
123 | 90 |
|
124 | 91 |
|
125 | 92 |
@Override |
126 | 93 |
public void printQR(String qrCodeStr) { |
94 |
+ if(printerService==null){ |
|
95 |
+ view.showToast(context.getString(R.string.printer_status_query_fail)); |
|
96 |
+ return; |
|
97 |
+ } |
|
127 | 98 |
if(TextUtils.isEmpty(Preferences.getInstance().getPrinterMac())){ |
128 | 99 |
view.showToast(App.getAppContext().getString(R.string.not_set_printer_yet)); |
129 | 100 |
return; |
130 | 101 |
} |
131 |
- EscCommand esc = new EscCommand(); |
|
132 |
- /*打印文字*/ |
|
133 |
- esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF, |
|
134 |
- EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);//取消倍高倍宽 |
|
135 |
- esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印左对齐 |
|
136 |
- esc.addText("拍爱\n"); // 打印文字 |
|
137 |
- esc.addPrintAndLineFeed(); |
|
138 |
- /*QRCode命令打印 |
|
139 |
- 此命令只在支持QRCode命令打印的机型才能使用。 |
|
140 |
- 在不支持二维码指令打印的机型上,则需要发送二维条码图片 |
|
141 |
- */ |
|
142 |
- esc.addSelectErrorCorrectionLevelForQRCode((byte) 0x31); //设置纠错等级 |
|
143 |
- esc.addSelectSizeOfModuleForQRCode((byte)8);//设置qrcode模块大小 |
|
144 |
- esc.addStoreQRCodeData(qrCodeStr);//设置qrcode内容 |
|
145 |
- esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印中心对齐 |
|
146 |
- esc.addPrintQRCode();//打印QRCode |
|
147 |
- esc.addPrintAndFeedLines((byte) 1); |
|
148 |
-// esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);//设置打印左对齐 |
|
149 |
-// esc.addText("拍摄日期 "+new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date())); |
|
150 |
-// esc.addPrintAndFeedLines((byte) 2); |
|
151 |
- |
|
152 |
- Vector<Byte> datas = esc.getCommand(); //发送数据 |
|
153 |
- Byte[] Bytes = datas.toArray(new Byte[datas.size()]); |
|
154 |
- byte[] bytes = ArrayUtils.toPrimitive(Bytes); |
|
155 |
- String str = Base64.encodeToString(bytes, Base64.DEFAULT); |
|
156 |
- int rel; |
|
157 |
- try { |
|
158 |
- rel = mGpService.sendEscCommand(0, str); |
|
159 |
- GpCom.ERROR_CODE r = GpCom.ERROR_CODE.values()[rel]; |
|
160 |
- if (r != GpCom.ERROR_CODE.SUCCESS) { |
|
161 |
- view.showToast( GpCom.getErrorText(r)); |
|
162 |
- } |
|
163 |
- } catch (Exception e) { |
|
164 |
- view.showToast(App.getAppContext().getString(R.string.go_check_printer)); |
|
102 |
+ int code = printerService.printQR(qrCodeStr); |
|
103 |
+ if(code == PrinterService.ERROR_CODE_PRINTER_SERVICE_EXCEPTION){ |
|
104 |
+ view.showToast(context.getString(R.string.go_check_printer)); |
|
105 |
+ }else if(code == PrinterService.ERROR_CODE_PRINTER_SERVICE_OFF){ |
|
106 |
+ view.showToast(context.getString(R.string.go_check_printer)); |
|
107 |
+ }else if( GpCom.ERROR_CODE.values()[code]!= GpCom.ERROR_CODE.SUCCESS){ |
|
108 |
+ view.showToast( GpCom.getErrorText(GpCom.ERROR_CODE.values()[code])); |
|
165 | 109 |
} |
166 | 110 |
} |
167 | 111 |
|
@@ -197,17 +141,14 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
197 | 141 |
|
198 | 142 |
@Override |
199 | 143 |
public void connectPrinter(BluetoothDevice device) { |
200 |
- if(mGpService==null){ |
|
144 |
+ if(printerService==null){ |
|
201 | 145 |
view.showToast(context.getString(R.string.printer_service_boot_fail)); |
202 | 146 |
return; |
203 | 147 |
} |
204 |
- try { |
|
205 |
- int code = mGpService.openPort(0, PortParameters.BLUETOOTH, device.getAddress(), 0); |
|
206 |
- if(code==0){ |
|
207 |
- Preferences.getInstance().setPrinterMac(device.getAddress()); |
|
208 |
- } |
|
209 |
- LogHelper.d(TAG,"open port return code ="+code); |
|
210 |
- } catch (Exception e) { |
|
148 |
+ int code = printerService.connectPrinter(device); |
|
149 |
+ if(code==0){ |
|
150 |
+ Preferences.getInstance().setPrinterMac(device.getAddress()); |
|
151 |
+ }else{ |
|
211 | 152 |
view.showToast(context.getString(R.string.printer_port_open_fail)); |
212 | 153 |
} |
213 | 154 |
|
@@ -223,12 +164,6 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
223 | 164 |
} |
224 | 165 |
} |
225 | 166 |
|
226 |
- private void startAndBindPrintService() { |
|
227 |
- Intent intent = new Intent(App.getAppContext(), GpPrintService.class); |
|
228 |
- App.getAppContext().startService(intent); |
|
229 |
- conn = new PrinterServiceConnection(); |
|
230 |
- App.getAppContext().bindService(intent, conn, Context.BIND_AUTO_CREATE); |
|
231 |
- } |
|
232 | 167 |
|
233 | 168 |
// changes the title when discovery is finished |
234 | 169 |
private final BroadcastReceiver mFindBlueToothReceiver = new BroadcastReceiver() { |
@@ -264,12 +199,11 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
264 | 199 |
public void onReceive(Context context, Intent intent) { |
265 | 200 |
if (GpCom.ACTION_CONNECT_STATUS.equals(intent.getAction())) { |
266 | 201 |
int type = intent.getIntExtra(GpPrintService.CONNECT_STATUS, 0); |
267 |
- int id = intent.getIntExtra(GpPrintService.PRINTER_ID, 0); |
|
268 | 202 |
Log.d(TAG, "connect status " + type); |
269 | 203 |
if (type == GpDevice.STATE_CONNECTING) { |
270 | 204 |
view.onPrinterStatusFetched(context.getString(R.string.connecting)); |
271 | 205 |
} else if (type == GpDevice.STATE_NONE) { |
272 |
- |
|
206 |
+ view.onPrinterStatusFetched(context.getString(R.string.connecting)); |
|
273 | 207 |
} else if (type == GpDevice.STATE_VALID_PRINTER) { |
274 | 208 |
view.onPrinterStatusFetched(context.getString(R.string.printer_is_connected)); |
275 | 209 |
} else if (type == GpDevice.STATE_INVALID_PRINTER) { |