diff --git a/src/android_serialport_api/print_tool/MobilePrintTool.java b/src/android_serialport_api/print_tool/MobilePrintTool.java index c6d7d88..881aacb 100644 --- a/src/android_serialport_api/print_tool/MobilePrintTool.java +++ b/src/android_serialport_api/print_tool/MobilePrintTool.java @@ -516,130 +516,142 @@ public class MobilePrintTool extends Activity { } } - // 蓝牙连接等待弹框 - private ProgressDialog dialog = null; - // 连接的设备的名称 - private String mConnectedDeviceName = null; - // 更新标题栏右边状态和读写状态的Handler - @SuppressLint("HandlerLeak") - private final Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case BluetoothService.MESSAGE_STATE_CHANGE: - switch (msg.arg1) { - case BluetoothService.STATE_CONNECTED: - MyApp.getInstance().setBluConnection(true); - if (!isprint) { - isprint = true; - isprint2 = true; - } - print(info); - if (dialog != null) { - dialog.dismiss(); - dialog = null; - } - break; - case BluetoothService.STATE_CONNECTING: - if (dialog == null) { - // 显示ProgressDialog - dialog = new ProgressDialog(MobilePrintTool.this); - dialog.setMessage("连接打印机中..."); - dialog.setCanceledOnTouchOutside(true); - dialog.setCancelable(true); - if (type == DeviceType.HANDSET.getValue()) { - dialog.getWindow() - .setType( - WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); - } - try { - dialog.show(); - } catch (Exception e) { - e.printStackTrace(); - } - } else { - dialog.setMessage("连接打印机中..."); - } - break; - case BluetoothService.STATE_NONE: - break; - case BluetoothService.STATE_LISTEN: - // MyApp.getInstance().setBluConnection(false); - if (dialog != null) { - dialog.dismiss(); - dialog = null; - } - break; - case BluetoothService.STATE_FAILL: - if (dialog != null) { - dialog.dismiss(); - dialog = null; - } - finish(); - break; - } - break; - case BluetoothService.STATE_FAILL: - if (dialog != null) { - dialog.dismiss(); - dialog = null; - } - finish(); - break; - case BluetoothService.MESSAGE_WRITE: - break; - case BluetoothService.MESSAGE_READ: - break; - case BluetoothService.MESSAGE_DEVICE_NAME: - // 保存连接设备的名字 - mConnectedDeviceName = msg.getData().getString( - BluetoothService.DEVICE_NAME); - if (type != null && type == DeviceType.HANDSET.getValue()) { - MToast.makeText(MobilePrintTool.this, - "连接到" + mConnectedDeviceName, MToast.LENGTH_SHORT) - .show(); - } else { - Toast.makeText(MobilePrintTool.this, - "连接到" + mConnectedDeviceName, Toast.LENGTH_SHORT) - .show(); - } - if (!isprint) { - isprint = true; - isprint2 = true; - } - print(info); - break; - case BluetoothService.MESSAGE_TOAST: - if (!msg.getData().getBoolean("sucess", false)) { - finish(); - return; - } - if (Thread.currentThread() != Looper.getMainLooper() - .getThread()) { - Looper.prepare(); - } - String info = msg.getData().getString(BluetoothService.TOAST); - if (info != null && !info.equals("")) { - if (type != null && type == DeviceType.HANDSET.getValue()) { - MToast.makeText( - MobilePrintTool.this, - msg.getData().getString(BluetoothService.TOAST), - MToast.LENGTH_LONG).show(); - } else { - Toast.makeText( - MobilePrintTool.this, - msg.getData().getString(BluetoothService.TOAST), - Toast.LENGTH_LONG).show(); - } - if (Thread.currentThread() != Looper.getMainLooper() - .getThread()) { - Looper.loop(); - } - } - break; - } - } - }; + // 蓝牙连接等待弹框 + private ProgressDialog dialog = null; + // 连接的设备的名称 + private String mConnectedDeviceName = null; + // 更新标题栏右边状态和读写状态的Handler + @SuppressLint("HandlerLeak") + private final Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case BluetoothService.MESSAGE_STATE_CHANGE: + switch (msg.arg1) { + case BluetoothService.STATE_CONNECTED: + MyApp.getInstance().setBluConnection(true); + if (!isprint) { + isprint = true; + isprint2 = true; + } + if (!TextUtils.isEmpty(info)) { + print(info); + } else { + Toast.makeText(MobilePrintTool.this, + "没有可打印数据!", Toast.LENGTH_SHORT) + .show(); + } + if (dialog != null) { + dialog.dismiss(); + dialog = null; + } + break; + case BluetoothService.STATE_CONNECTING: + if (dialog == null) { + // 显示ProgressDialog + dialog = new ProgressDialog(MobilePrintTool.this); + dialog.setMessage("连接打印机中..."); + dialog.setCanceledOnTouchOutside(true); + dialog.setCancelable(true); + if (type == DeviceType.HANDSET.getValue()) { + dialog.getWindow() + .setType( + WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); + } + try { + dialog.show(); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + dialog.setMessage("连接打印机中..."); + } + break; + case BluetoothService.STATE_NONE: + break; + case BluetoothService.STATE_LISTEN: + // MyApp.getInstance().setBluConnection(false); + if (dialog != null) { + dialog.dismiss(); + dialog = null; + } + break; + case BluetoothService.STATE_FAILL: + if (dialog != null) { + dialog.dismiss(); + dialog = null; + } + finish(); + break; + } + break; + case BluetoothService.STATE_FAILL: + if (dialog != null) { + dialog.dismiss(); + dialog = null; + } + finish(); + break; + case BluetoothService.MESSAGE_WRITE: + break; + case BluetoothService.MESSAGE_READ: + break; + case BluetoothService.MESSAGE_DEVICE_NAME: + // 保存连接设备的名字 + mConnectedDeviceName = msg.getData().getString( + BluetoothService.DEVICE_NAME); + if (type != null && type == DeviceType.HANDSET.getValue()) { + MToast.makeText(MobilePrintTool.this, + "连接到" + mConnectedDeviceName, MToast.LENGTH_SHORT) + .show(); + } else { + Toast.makeText(MobilePrintTool.this, + "连接到" + mConnectedDeviceName, Toast.LENGTH_SHORT) + .show(); + } + if (!isprint) { + isprint = true; + isprint2 = true; + } + if (!TextUtils.isEmpty(info)) { + print(info); + } else { + Toast.makeText(MobilePrintTool.this, + "没有可打印数据!", Toast.LENGTH_SHORT) + .show(); + } + break; + case BluetoothService.MESSAGE_TOAST: + if (!msg.getData().getBoolean("sucess", false)) { + finish(); + return; + } + if (Thread.currentThread() != Looper.getMainLooper() + .getThread()) { + Looper.prepare(); + } + String info = msg.getData().getString(BluetoothService.TOAST); + if (info != null && !info.equals("")) { + if (type != null && type == DeviceType.HANDSET.getValue()) { + MToast.makeText( + MobilePrintTool.this, + msg.getData().getString(BluetoothService.TOAST), + MToast.LENGTH_LONG).show(); + } else { + Toast.makeText( + MobilePrintTool.this, + msg.getData().getString(BluetoothService.TOAST), + Toast.LENGTH_LONG).show(); + } + if (Thread.currentThread() != Looper.getMainLooper() + .getThread()) { + Looper.loop(); + } + } + break; + } + } + }; @Override protected void onDestroy() {