@@ -117,11 +117,21 @@ public class PrinterSettingActivity extends BaseActivity implements PrinterSetti |
||
| 117 | 117 |
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); |
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 |
+ @OnClick(R.id.tv_printer_status) |
|
| 121 |
+ void checkStatus(){
|
|
| 122 |
+ presenter.queryPrinterStatus(); |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 120 | 125 |
@OnClick(R.id.tv_print_test) |
| 121 | 126 |
void testPrint(){
|
| 122 | 127 |
presenter.printTestPage(); |
| 123 | 128 |
} |
| 124 | 129 |
|
| 130 |
+ @OnClick(R.id.title_bar_back_layout) |
|
| 131 |
+ void back(){
|
|
| 132 |
+ finish(); |
|
| 133 |
+ } |
|
| 134 |
+ |
|
| 125 | 135 |
@OnCheckedChanged(R.id.tb_bluetooth_switch) |
| 126 | 136 |
void switchBluetooth(){
|
| 127 | 137 |
if(!presenter.queryBluetoothStatus()){
|
@@ -152,6 +162,6 @@ public class PrinterSettingActivity extends BaseActivity implements PrinterSetti |
||
| 152 | 162 |
|
| 153 | 163 |
@Override |
| 154 | 164 |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
| 155 |
- presenter.connectBluetooth(deviceAdapter.getDeviceList().get(i)); |
|
| 165 |
+ presenter.connectPrinter(deviceAdapter.getDeviceList().get(i)); |
|
| 156 | 166 |
} |
| 157 | 167 |
} |
@@ -18,9 +18,8 @@ public class PrinterSettingContract {
|
||
| 18 | 18 |
boolean queryBluetoothStatus(); |
| 19 | 19 |
List<BluetoothDevice> queryPairedDevices(); |
| 20 | 20 |
void discoverNewDevices(); |
| 21 |
- void connectPrinter(); |
|
| 21 |
+ void connectPrinter(BluetoothDevice device); |
|
| 22 | 22 |
void cancelDiscovery(); |
| 23 |
- void connectBluetooth(BluetoothDevice device); |
|
| 24 | 23 |
} |
| 25 | 24 |
|
| 26 | 25 |
public interface View{
|
@@ -3,14 +3,27 @@ package ai.pai.lensman.printer; |
||
| 3 | 3 |
import android.bluetooth.BluetoothAdapter; |
| 4 | 4 |
import android.bluetooth.BluetoothDevice; |
| 5 | 5 |
import android.content.BroadcastReceiver; |
| 6 |
+import android.content.ComponentName; |
|
| 6 | 7 |
import android.content.Context; |
| 7 | 8 |
import android.content.Intent; |
| 8 | 9 |
import android.content.IntentFilter; |
| 10 |
+import android.content.ServiceConnection; |
|
| 11 |
+import android.os.IBinder; |
|
| 12 |
+import android.os.RemoteException; |
|
| 13 |
+import android.util.Log; |
|
| 14 |
+ |
|
| 15 |
+import com.android.common.utils.LogHelper; |
|
| 16 |
+import com.gprinter.aidl.GpService; |
|
| 17 |
+import com.gprinter.command.GpCom; |
|
| 18 |
+import com.gprinter.io.GpDevice; |
|
| 19 |
+import com.gprinter.io.PortParameters; |
|
| 20 |
+import com.gprinter.service.GpPrintService; |
|
| 9 | 21 |
|
| 10 | 22 |
import java.util.ArrayList; |
| 11 | 23 |
import java.util.List; |
| 12 | 24 |
import java.util.Set; |
| 13 | 25 |
|
| 26 |
+import ai.pai.lensman.App; |
|
| 14 | 27 |
import ai.pai.lensman.R; |
| 15 | 28 |
|
| 16 | 29 |
/** |
@@ -23,6 +36,24 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
| 23 | 36 |
private PrinterSettingContract.View view; |
| 24 | 37 |
private BluetoothAdapter bluetoothAdapter; |
| 25 | 38 |
|
| 39 |
+ private GpService mGpService; |
|
| 40 |
+ private PrinterServiceConnection conn = null; |
|
| 41 |
+ |
|
| 42 |
+ class PrinterServiceConnection implements ServiceConnection {
|
|
| 43 |
+ @Override |
|
| 44 |
+ public void onServiceDisconnected(ComponentName name) {
|
|
| 45 |
+ Log.i("czy", "onServiceDisconnected() called");
|
|
| 46 |
+ mGpService = null; |
|
| 47 |
+ } |
|
| 48 |
+ |
|
| 49 |
+ @Override |
|
| 50 |
+ public void onServiceConnected(ComponentName name, IBinder service) {
|
|
| 51 |
+ Log.i("czy", "onServiceConnected() called");
|
|
| 52 |
+ mGpService = GpService.Stub.asInterface(service); |
|
| 53 |
+ } |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ |
|
| 26 | 57 |
public PrinterSettingPresenter(Context context, PrinterSettingContract.View view){
|
| 27 | 58 |
this.view = view; |
| 28 | 59 |
this.context = context; |
@@ -31,12 +62,14 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
| 31 | 62 |
|
| 32 | 63 |
@Override |
| 33 | 64 |
public void start() {
|
| 65 |
+ registerPrinterBroadcast(); |
|
| 66 |
+ startAndBindPrintService(); |
|
| 34 | 67 |
if(queryBluetoothStatus()){
|
| 35 | 68 |
view.onBluetoothEnabled(); |
| 36 | 69 |
view.onPairedDeviceDiscovered(queryPairedDevices()); |
| 37 | 70 |
view.showToast(context.getString(R.string.query_processing)); |
| 38 |
- queryPrinterStatus(); |
|
| 39 | 71 |
discoverNewDevices(); |
| 72 |
+ |
|
| 40 | 73 |
}else{
|
| 41 | 74 |
view.onBluetoothDisabled(); |
| 42 | 75 |
view.showToast(context.getString(R.string.bt_is_disabled)); |
@@ -46,16 +79,59 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
| 46 | 79 |
@Override |
| 47 | 80 |
public void stop() {
|
| 48 | 81 |
cancelDiscovery(); |
| 82 |
+ if (conn != null) {
|
|
| 83 |
+ App.getAppContext().unbindService(conn); |
|
| 84 |
+ } |
|
| 85 |
+ context.unregisterReceiver(PrinterStatusBroadcastReceiver); |
|
| 49 | 86 |
} |
| 50 | 87 |
|
| 51 | 88 |
@Override |
| 52 | 89 |
public void queryPrinterStatus() {
|
| 90 |
+ try {
|
|
| 91 |
+ |
|
| 92 |
+ int status = mGpService.queryPrinterStatus(0, 5000); |
|
| 93 |
+ String str = new String(); |
|
| 94 |
+ if (status == GpCom.STATE_NO_ERR) {
|
|
| 95 |
+ str = "打印机正常"; |
|
| 96 |
+ } else {
|
|
| 97 |
+ str = "打印机 "; |
|
| 98 |
+ if ((byte) (status & GpCom.STATE_OFFLINE) > 0) {
|
|
| 99 |
+ str += "脱机"; |
|
| 100 |
+ } |
|
| 101 |
+ if ((byte) (status & GpCom.STATE_PAPER_ERR) > 0) {
|
|
| 102 |
+ str += "缺纸"; |
|
| 103 |
+ } |
|
| 104 |
+ if ((byte) (status & GpCom.STATE_COVER_OPEN) > 0) {
|
|
| 105 |
+ str += "打印机开盖"; |
|
| 106 |
+ } |
|
| 107 |
+ if ((byte) (status & GpCom.STATE_ERR_OCCURS) > 0) {
|
|
| 108 |
+ str += "打印机出错"; |
|
| 109 |
+ } |
|
| 110 |
+ if ((byte) (status & GpCom.STATE_TIMES_OUT) > 0) {
|
|
| 111 |
+ str += "查询超时"; |
|
| 112 |
+ } |
|
| 113 |
+ } |
|
| 114 |
+ view.showToast( "打印机:" + " 状态:" + str); |
|
| 115 |
+ view.onPrinterError(str); |
|
| 53 | 116 |
|
| 117 |
+ } catch (RemoteException e1) {
|
|
| 118 |
+ e1.printStackTrace(); |
|
| 119 |
+ } |
|
| 54 | 120 |
} |
| 55 | 121 |
|
| 56 | 122 |
@Override |
| 57 | 123 |
public void printTestPage() {
|
| 58 |
- |
|
| 124 |
+ try {
|
|
| 125 |
+ int rel = mGpService.printeTestPage(0); // |
|
| 126 |
+ Log.i("ServiceConnection", "rel " + rel);
|
|
| 127 |
+ GpCom.ERROR_CODE r = GpCom.ERROR_CODE.values()[rel]; |
|
| 128 |
+ if (r != GpCom.ERROR_CODE.SUCCESS) {
|
|
| 129 |
+ view.showToast( GpCom.getErrorText(r)); |
|
| 130 |
+ } |
|
| 131 |
+ } catch (RemoteException e1) {
|
|
| 132 |
+ // TODO Auto-generated catch block |
|
| 133 |
+ e1.printStackTrace(); |
|
| 134 |
+ } |
|
| 59 | 135 |
} |
| 60 | 136 |
|
| 61 | 137 |
@Override |
@@ -89,8 +165,13 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
| 89 | 165 |
} |
| 90 | 166 |
|
| 91 | 167 |
@Override |
| 92 |
- public void connectPrinter() {
|
|
| 93 |
- |
|
| 168 |
+ public void connectPrinter(BluetoothDevice device) {
|
|
| 169 |
+ try {
|
|
| 170 |
+ int code = mGpService.openPort(0, PortParameters.BLUETOOTH, device.getAddress(), 0); |
|
| 171 |
+ LogHelper.d("czy","open port return code ="+code);
|
|
| 172 |
+ } catch (RemoteException e) {
|
|
| 173 |
+ e.printStackTrace(); |
|
| 174 |
+ } |
|
| 94 | 175 |
} |
| 95 | 176 |
|
| 96 | 177 |
@Override |
@@ -103,9 +184,11 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
| 103 | 184 |
} |
| 104 | 185 |
} |
| 105 | 186 |
|
| 106 |
- @Override |
|
| 107 |
- public void connectBluetooth(BluetoothDevice device) {
|
|
| 108 |
- |
|
| 187 |
+ private void startAndBindPrintService() {
|
|
| 188 |
+ Intent intent = new Intent(App.getAppContext(), GpPrintService.class); |
|
| 189 |
+ App.getAppContext().startService(intent); |
|
| 190 |
+ conn = new PrinterServiceConnection(); |
|
| 191 |
+ App.getAppContext().bindService(intent, conn, Context.BIND_AUTO_CREATE); |
|
| 109 | 192 |
} |
| 110 | 193 |
|
| 111 | 194 |
// changes the title when discovery is finished |
@@ -131,6 +214,30 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
| 131 | 214 |
} |
| 132 | 215 |
}; |
| 133 | 216 |
|
| 217 |
+ private void registerPrinterBroadcast() {
|
|
| 218 |
+ IntentFilter filter = new IntentFilter(); |
|
| 219 |
+ filter.addAction(GpCom.ACTION_CONNECT_STATUS); |
|
| 220 |
+ context.registerReceiver(PrinterStatusBroadcastReceiver, filter); |
|
| 221 |
+ } |
|
| 222 |
+ |
|
| 223 |
+ private BroadcastReceiver PrinterStatusBroadcastReceiver = new BroadcastReceiver() {
|
|
| 224 |
+ @Override |
|
| 225 |
+ public void onReceive(Context context, Intent intent) {
|
|
| 226 |
+ if (GpCom.ACTION_CONNECT_STATUS.equals(intent.getAction())) {
|
|
| 227 |
+ int type = intent.getIntExtra(GpPrintService.CONNECT_STATUS, 0); |
|
| 228 |
+ int id = intent.getIntExtra(GpPrintService.PRINTER_ID, 0); |
|
| 229 |
+ Log.d("czy", "connect status " + type);
|
|
| 230 |
+ if (type == GpDevice.STATE_CONNECTING) {
|
|
| 231 |
+ |
|
| 232 |
+ } else if (type == GpDevice.STATE_NONE) {
|
|
| 233 |
+ |
|
| 234 |
+ } else if (type == GpDevice.STATE_VALID_PRINTER) {
|
|
| 235 |
+ |
|
| 236 |
+ } else if (type == GpDevice.STATE_INVALID_PRINTER) {
|
|
| 134 | 237 |
|
| 238 |
+ } |
|
| 239 |
+ } |
|
| 240 |
+ } |
|
| 241 |
+ }; |
|
| 135 | 242 |
|
| 136 | 243 |
} |