关于JavaScriptInterface的一系列问题

先明确主题,主要做了什么。

接手时app主体是混合开发,以elipse+android SDK为开发环境,但我是个渣渣,我只会用AS,就转成了AS,这部分等会新开文章写。

主要的view只有一个activity_main,里边写了个webview,在mainactivity里去掉了actionbar,一个导入库都没有有四个jar包,然后所有的HTML代码放在了assets里,怎么调用你们也懂得。然后mainactivity执行了html调用的所有操作,然后我遇到的问题就是一系列的线程问题。

app主要功能是调用蓝牙打印机。

遇到问题,一调用JavaScriptInterface里的serviceANR

其实千篇一律别人的博客也都有的就简单说一下。

1. 17及以上/SDK4.4以后的版本中,就会出现js调用不了android的代码,这是版本兼容的问题,需要在调用的方法上面加一个注解:@JavascriptInterface, 这个注解需要导入一个包:import android.webkit.JavascriptInterface;

2. android3.2以上才可以调用蓝牙打印机,用的是他家的方法,传送门:http://my.csdn.net/CaysnPrinter

3.还有就是打包混淆之后不可用的问题,懒得写,传送门:https://www.2cto.com/kf/201410/344068.html

现在看我的问题,先贴代码:

        /**
         * 预加载
         * @return
         */
        @JavascriptInterface
        public String beforeLoad(){
            Log.i("Main", "预加载...");
            if(mHandler == null){
                mHandler = new MHandler(MainActivity.this);
                WorkService.addHandler(mHandler);
            }
            if (null == WorkService.workThread) {
                Intent intent = new Intent(MainActivity.this, WorkService.class);
                startService(intent);
            }
            Log.i("Main", "isStartPrint:--------->"+isStartPrint);
            handleIntent(getIntent());
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
                // finish();
                return null;
            }
            /* 启动蓝牙 */
            adapter = BluetoothAdapter.getDefaultAdapter();
            if (null == adapter) {
                return null;
            }
            if (!adapter.isEnabled()) {
                if (adapter.enable()) {
                    while (!adapter.isEnabled())
                        ;
                    Log.i("Main", "Enable BluetoothAdapter");
                } else {
                    Toast.makeText(MainActivity.this, "请允许开启蓝牙设备!", Toast.LENGTH_SHORT).show();
                    return null;
                }
            }
            return  getPrinter() ;
        }

我的问题是在:

 Intent intent = new Intent(MainActivity.this, WorkService.class);
                startService(intent);

这里出的问题,其实到现在我也没搞懂addJavaScriptInterface这里是子线程还是主线程,但是如果他不是主线程的话,我后边的耗时操作估计就不会阻塞了。

所以他是主线程,然后贴全部的service代码,就知道我这里是怎么卡的了

  1 package com.printer.service;
  2 
  3 import java.lang.ref.WeakReference;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 import com.printer.constant.Global;
  8 
  9 import android.app.Service;
 10 import android.content.Intent;
 11 import android.os.Handler;
 12 import android.os.IBinder;
 13 import android.os.Message;
 14 import android.util.Log;
 15 
 16 /**
 17  * 观察者模式
 18  * 
 19  * @author Administrator
 20  * 
 21  */
 22 public class WorkService extends Service {
 23 
 24     // Service和workThread通信用mHandler
 25     public static WorkThread workThread = null;
 26     private static Handler mHandler = null;
 27     private static List<Handler> targetsHandler = new ArrayList<Handler>(5);
 28 
 29     @Override
 30     public IBinder onBind(Intent arg0) {
 31         // TODO Auto-generated method stub
 32         return null;
 33     }
 34 
 35     @Override
 36     public void onCreate() {
 37         mHandler = new MHandler(this);
 38         /*workThread = new WorkThread(mHandler);
 39         workThread.start();*/
 40         new Thread(new Runnable() {
 41             @Override
 42             public void run() {
 43                 workThread = new WorkThread(mHandler);
 44                 workThread.start();
 45             }
 46         }).start();
 47     }
 48 
 49     @Override
 50     public int onStartCommand(Intent intent, int flags, int startId) {
 51         Log.v("WorkService", "onStartCommand");
 52         Message msg = Message.obtain();
 53         msg.what = Global.MSG_ALLTHREAD_READY;
 54         notifyHandlers(msg);
 55         return START_NOT_STICKY;
 56     }
 57 
 58     @Override
 59     public void onDestroy() {
 60         workThread.disconnectBt();
 61         workThread.disconnectBle();
 62         workThread.disconnectNet();
 63         workThread.disconnectUsb();
 64         workThread.quit();
 65         workThread = null;
 66         Log.v("DrawerService", "onDestroy");
 67     }
 68 
 69     static class MHandler extends Handler {
 70 
 71         WeakReference<WorkService> mService;
 72 
 73         MHandler(WorkService service) {
 74             mService = new WeakReference<WorkService>(service);
 75         }
 76 
 77         @Override
 78         public void handleMessage(Message msg) {
 79             notifyHandlers(msg);
 80         }
 81     }
 82 
 83     /**
 84      * 
 85      * @param handler
 86      */
 87     public static void addHandler(Handler handler) {
 88         if (!targetsHandler.contains(handler)) {
 89             targetsHandler.add(handler);
 90         }
 91     }
 92 
 93     /**
 94      * 
 95      * @param handler
 96      */
 97     public static void delHandler(Handler handler) {
 98         if (targetsHandler.contains(handler)) {
 99             targetsHandler.remove(handler);
100         }
101     }
102 
103     /**
104      * 
105      * @param msg
106      */
107     public static void notifyHandlers(Message msg) {
108         for (int i = 0; i < targetsHandler.size(); i++) {
109             Message message = Message.obtain(msg);
110             targetsHandler.get(i).sendMessage(message);
111         }
112     }
113 
114 }

我是在onCreate里出的问题:

@Override
public void onCreate() {
mHandler = new MHandler(this);
workThread = new WorkThread(mHandler);
workThread.start();
}

这是我接手时的代码,然后加了一个newTheard(new Runnable(){}).start();把耗时操作也就是workthread放进去。

然后就解决了这样一个很脑残的问题,网上零零散散解释不清楚,联机测试run的时候就没问题但是打包不管混淆不混淆都会ANR。

甚至还有人给的解决方法是在JavaScriptInterface这类上继承activity,这个我试过了如果里边的耗时操作不多,估计,,,就脑残了。

 

总结:不要在JavaScriptInterface这个类里的任何方法加入耗时操作,还有一篇关于ANR写的特别好。传送门:http://www.jianshu.com/p/f14e89641109

          这里唠叨一句,大家最开始找工作的时候都背过什么面试宝典,关于ANR只是说过两个原因,总体来说我的算第三种,所以建议还是好好看看。

          感觉我的问题和JavaScriptInterface一点关系都没有。。。。随手贴一篇我这里边蓝牙打印机是咋写的。

  1 package com.printer.service;
  2 
  3 import android.graphics.Bitmap;
  4 import android.hardware.usb.UsbDevice;
  5 import android.hardware.usb.UsbManager;
  6 import android.os.Build;
  7 import android.os.Bundle;
  8 import android.os.Handler;
  9 import android.os.Looper;
 10 import android.os.Message;
 11 import android.util.Log;
 12 
 13 import com.lvrenyang.io.BLEPrinting;
 14 import com.lvrenyang.io.BTPrinting;
 15 import com.lvrenyang.io.NETPrinting;
 16 import com.lvrenyang.io.Pos;
 17 import com.lvrenyang.io.USBPrinting;
 18 import com.printer.constant.Global;
 19 
 20 public class WorkThread extends Thread {
 21 
 22     private static final String TAG = "WorkThread";
 23     public static Handler workHandler = null;
 24     private static Looper mLooper = null;
 25     public static Handler targetHandler = null;
 26     private static boolean threadInitOK = false;
 27     private static boolean isConnecting = false;
 28     private static BTPrinting bt = null;
 29     private static BLEPrinting ble = null;
 30     private static NETPrinting net = null;
 31     private static USBPrinting usb = null;
 32     private static Pos pos = new Pos();
 33 
 34     public WorkThread(Handler handler) {
 35         threadInitOK = false;
 36         targetHandler = handler;
 37         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
 38             if (usb == null)
 39                 usb = new USBPrinting();
 40         }
 41         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
 42             if (ble == null)
 43                 ble = new BLEPrinting();
 44         }
 45         if (bt == null)
 46             bt = new BTPrinting();
 47         if (net == null)
 48             net = new NETPrinting();
 49     }
 50 
 51     @Override
 52     public void start() {
 53         super.start();
 54         while (!threadInitOK)
 55             ;
 56     }
 57 
 58     @Override
 59     public void run() {
 60         Looper.prepare();
 61         mLooper = Looper.myLooper();
 62         if (null == mLooper)
 63             Log.v(TAG, "mLooper is null pointer");
 64         else
 65             Log.v(TAG, "mLooper is valid");
 66         workHandler = new WorkHandler();
 67         threadInitOK = true;
 68         Looper.loop();
 69     }
 70 
 71     private static class WorkHandler extends Handler {
 72 
 73         @Override
 74         public void handleMessage(Message msg) {
 75 
 76             switch (msg.what) {
 77             case Global.MSG_WORKTHREAD_HANDLER_CONNECTBT: {
 78                 isConnecting = true;
 79 
 80                 pos.Set(bt);
 81 
 82                 String BTAddress = (String) msg.obj;
 83                 boolean result = bt.Open(BTAddress);
 84 
 85                 Message smsg = targetHandler
 86                         .obtainMessage(Global.MSG_WORKTHREAD_SEND_CONNECTBTRESULT);
 87                 smsg.arg1 = result ? 1 : 0;
 88                 targetHandler.sendMessage(smsg);
 89 
 90                 isConnecting = false;
 91                 break;
 92             }
 93 
 94             case Global.MSG_WORKTHREAD_HANDLER_CONNECTBLE: {
 95                 isConnecting = true;
 96 
 97                 pos.Set(ble);
 98 
 99                 String BTAddress = (String) msg.obj;
100                 boolean result = ble.Open(BTAddress);
101 
102                 Message smsg = targetHandler
103                         .obtainMessage(Global.MSG_WORKTHREAD_SEND_CONNECTBLERESULT);
104                 smsg.arg1 = result ? 1 : 0;
105                 targetHandler.sendMessage(smsg);
106 
107                 isConnecting = false;
108                 break;
109             }
110 
111             case Global.MSG_WORKTHREAD_HANDLER_CONNECTNET: {
112                 isConnecting = true;
113 
114                 pos.Set(net);
115 
116                 int PortNumber = msg.arg1;
117                 String IPAddress = (String) msg.obj;
118                 boolean result = net.Open(IPAddress, PortNumber);
119 
120                 Message smsg = targetHandler
121                         .obtainMessage(Global.MSG_WORKTHREAD_SEND_CONNECTNETRESULT);
122                 smsg.arg1 = result ? 1 : 0;
123                 targetHandler.sendMessage(smsg);
124 
125                 isConnecting = false;
126                 break;
127             }
128 
129             case Global.MSG_WORKTHREAD_HANDLER_CONNECTUSB: {
130                 isConnecting = true;
131 
132                 pos.Set(usb);
133 
134                 UsbManager manager = (UsbManager) msg.obj;
135                 Bundle data = msg.getData();
136                 UsbDevice device = data.getParcelable(Global.PARCE1);
137 
138                 boolean result = usb.Open(manager, device);
139                 Message smsg = targetHandler
140                         .obtainMessage(Global.MSG_WORKTHREAD_SEND_CONNECTUSBRESULT);
141                 smsg.arg1 = result ? 1 : 0;
142                 targetHandler.sendMessage(smsg);
143 
144                 isConnecting = false;
145                 break;
146             }
147 
148             case Global.CMD_WRITE: {
149                 Bundle data = msg.getData();
150                 byte[] buffer = data.getByteArray(Global.BYTESPARA1);
151                 int offset = data.getInt(Global.INTPARA1);
152                 int count = data.getInt(Global.INTPARA2);
153 
154                 Message smsg = targetHandler
155                         .obtainMessage(Global.CMD_WRITERESULT);
156                 if (pos.IO.Write(buffer, offset, count) == count) {
157                     smsg.arg1 = 1;
158                 } else {
159                     smsg.arg1 = 0;
160                 }
161                 targetHandler.sendMessage(smsg);
162 
163                 break;
164             }
165 
166             // USB写入自带流控制,不需要读取状态�?
167             case Global.CMD_POS_WRITE: {
168                 Bundle data = msg.getData();
169                 byte[] buffer = data.getByteArray(Global.BYTESPARA1);
170                 int offset = data.getInt(Global.INTPARA1);
171                 int count = data.getInt(Global.INTPARA2);
172 
173                 Message smsg = targetHandler
174                         .obtainMessage(Global.CMD_POS_WRITERESULT);
175 
176                 boolean result = false;
177 
178                 if (USBPrinting.class.getName().equals(
179                         pos.IO.getClass().getName()))
180                     result = true;
181                 else
182                     result = pos.POS_QueryOnline(1000);
183 
184                 if (result) {
185                     if (pos.IO.Write(buffer, offset, count) == count)
186                         smsg.arg1 = 1;
187                     else
188                         smsg.arg1 = 0;
189                 } else {
190                     smsg.arg1 = 0;
191                 }
192                 targetHandler.sendMessage(smsg);
193 
194                 break;
195             }
196 
197             case Global.CMD_POS_SETKEY: {
198                 Bundle data = msg.getData();
199                 byte[] key = data.getByteArray(Global.BYTESPARA1);
200 
201                 boolean result = false;
202 
203                 if (USBPrinting.class.getName().equals(
204                         pos.IO.getClass().getName()))
205                     result = true;
206                 else
207                     result = pos.POS_QueryOnline(1000);
208 
209                 Message smsg = targetHandler
210                         .obtainMessage(Global.CMD_POS_SETKEYRESULT);
211 
212                 if (result) {
213                     pos.POS_SetKey(key);
214                     smsg.arg1 = 1;
215                 } else {
216                     smsg.arg1 = 0;
217                 }
218                 targetHandler.sendMessage(smsg);
219 
220                 break;
221             }
222 
223             case Global.CMD_POS_CHECKKEY: {
224                 Bundle data = msg.getData();
225                 byte[] key = data.getByteArray(Global.BYTESPARA1);
226 
227                 boolean result = false;
228 
229                 if (USBPrinting.class.getName().equals(
230                         pos.IO.getClass().getName()))
231                     result = true;
232                 else
233                     result = pos.POS_QueryOnline(1000);
234 
235                 Message smsg = targetHandler
236                         .obtainMessage(Global.CMD_POS_CHECKKEYRESULT);
237                 if (result) {
238                     if (pos.POS_CheckKey(key))
239                         smsg.arg1 = 1;
240                     else
241                         smsg.arg1 = 0;
242                 } else {
243                     smsg.arg1 = 0;
244                 }
245                 targetHandler.sendMessage(smsg);
246 
247                 break;
248             }
249             case Global.CMD_POS_PRINTPICTURE: {
250                 Bundle data = msg.getData();
251                 Bitmap mBitmap = (Bitmap) data.getParcelable(Global.PARCE1);
252                 int nWidth = data.getInt(Global.INTPARA1);
253                 int nMode = data.getInt(Global.INTPARA2);
254 
255                 boolean result = false;
256 
257                 if (USBPrinting.class.getName().equals(
258                         pos.IO.getClass().getName()))
259                     result = true;
260                 else
261                     result = pos.POS_QueryOnline(1000);
262 
263                 Message smsg = targetHandler
264                         .obtainMessage(Global.CMD_POS_PRINTPICTURERESULT);
265                 if (result) {
266                     pos.POS_PrintPicture(mBitmap, nWidth, nMode);
267 
268                     if (USBPrinting.class.getName().equals(
269                             pos.IO.getClass().getName()))
270                         result = true;
271                     else
272                         result = pos.POS_QueryOnline(1000);
273 
274                     if (result)
275                         smsg.arg1 = 1;
276                     else
277                         smsg.arg1 = 0;
278                 } else {
279                     smsg.arg1 = 0;
280                 }
281                 targetHandler.sendMessage(smsg);
282 
283                 break;
284             }
285 
286             case Global.CMD_POS_PRINTBWPICTURE: {
287                 Bundle data = msg.getData();
288                 // Bitmap mBitmap = (Bitmap) data.get(Global.OBJECT1);
289                 Bitmap mBitmap = (Bitmap) data.getParcelable(Global.PARCE1);
290                 int nWidth = data.getInt(Global.INTPARA1);
291                 int nMode = data.getInt(Global.INTPARA2);
292 
293                 boolean result = false;
294 
295                 if (USBPrinting.class.getName().equals(
296                         pos.IO.getClass().getName()))
297                     result = true;
298                 else
299                     result = pos.POS_QueryOnline(1000);
300 
301                 Message smsg = targetHandler
302                         .obtainMessage(Global.CMD_POS_PRINTPICTURERESULT);
303                 if (result) {
304                     pos.POS_PrintBWPic(mBitmap, nWidth, nMode);
305 
306                     if (USBPrinting.class.getName().equals(
307                             pos.IO.getClass().getName()))
308                         result = true;
309                     else
310                         result = pos.POS_QueryOnline(1000);
311 
312                     if (result)
313                         smsg.arg1 = 1;
314                     else
315                         smsg.arg1 = 0;
316                 } else {
317                     smsg.arg1 = 0;
318                 }
319                 targetHandler.sendMessage(smsg);
320 
321                 break;
322             }
323 
324             case Global.CMD_POS_SALIGN: {
325                 Bundle data = msg.getData();
326                 int align = data.getInt(Global.INTPARA1);
327 
328                 boolean result = false;
329 
330                 if (USBPrinting.class.getName().equals(
331                         pos.IO.getClass().getName()))
332                     result = true;
333                 else
334                     result = pos.POS_QueryOnline(1000);
335 
336                 Message smsg = targetHandler
337                         .obtainMessage(Global.CMD_POS_SALIGNRESULT);
338                 if (result) {
339                     pos.POS_S_Align(align);
340                     smsg.arg1 = 1;
341                 } else {
342                     smsg.arg1 = 0;
343                 }
344                 targetHandler.sendMessage(smsg);
345 
346                 break;
347             }
348 
349             case Global.CMD_POS_SETLINEHEIGHT: {
350                 Bundle data = msg.getData();
351                 int nHeight = data.getInt(Global.INTPARA1);
352 
353                 boolean result = false;
354 
355                 if (USBPrinting.class.getName().equals(
356                         pos.IO.getClass().getName()))
357                     result = true;
358                 else
359                     result = pos.POS_QueryOnline(1000);
360 
361                 Message smsg = targetHandler
362                         .obtainMessage(Global.CMD_POS_SETLINEHEIGHTRESULT);
363                 if (result) {
364                     pos.POS_SetLineHeight(nHeight);
365                     smsg.arg1 = 1;
366                 } else {
367                     smsg.arg1 = 0;
368                 }
369                 targetHandler.sendMessage(smsg);
370 
371                 break;
372             }
373 
374             case Global.CMD_POS_SETRIGHTSPACE: {
375                 Bundle data = msg.getData();
376                 int nDistance = data.getInt(Global.INTPARA1);
377 
378                 boolean result = false;
379 
380                 if (USBPrinting.class.getName().equals(
381                         pos.IO.getClass().getName()))
382                     result = true;
383                 else
384                     result = pos.POS_QueryOnline(1000);
385 
386                 Message smsg = targetHandler
387                         .obtainMessage(Global.CMD_POS_SETRIGHTSPACERESULT);
388                 if (result) {
389                     pos.POS_SetRightSpacing(nDistance);
390                     smsg.arg1 = 1;
391                 } else {
392                     smsg.arg1 = 0;
393                 }
394                 targetHandler.sendMessage(smsg);
395 
396                 break;
397             }
398 
399             case Global.CMD_POS_STEXTOUT: {
400                 Bundle data = msg.getData();
401                 String pszString = data.getString(Global.STRPARA1);
402                 String encoding = data.getString(Global.STRPARA2);
403                 int nOrgx = data.getInt(Global.INTPARA1);
404                 int nWidthTimes = data.getInt(Global.INTPARA2);
405                 int nHeightTimes = data.getInt(Global.INTPARA3);
406                 int nFontType = data.getInt(Global.INTPARA4);
407                 int nFontStyle = data.getInt(Global.INTPARA5);
408 
409                 boolean result = false;
410 
411                 if (USBPrinting.class.getName().equals(
412                         pos.IO.getClass().getName()))
413                     result = true;
414                 else
415                     result = pos.POS_QueryOnline(1000);
416 
417                 Message smsg = targetHandler
418                         .obtainMessage(Global.CMD_POS_STEXTOUTRESULT);
419                 if (result) {
420                     pos.POS_S_TextOut(pszString, encoding, nOrgx, nWidthTimes,
421                             nHeightTimes, nFontType, nFontStyle);
422                     smsg.arg1 = 1;
423                 } else {
424                     smsg.arg1 = 0;
425                 }
426                 targetHandler.sendMessage(smsg);
427 
428                 break;
429             }
430 
431             case Global.CMD_POS_SETCHARSETANDCODEPAGE: {
432                 Bundle data = msg.getData();
433                 int nCharSet = data.getInt(Global.INTPARA1);
434                 int nCodePage = data.getInt(Global.INTPARA2);
435 
436                 boolean result = false;
437 
438                 if (USBPrinting.class.getName().equals(
439                         pos.IO.getClass().getName()))
440                     result = true;
441                 else
442                     result = pos.POS_QueryOnline(1000);
443 
444                 Message smsg = targetHandler
445                         .obtainMessage(Global.CMD_POS_SETCHARSETANDCODEPAGERESULT);
446                 if (result) {
447                     pos.POS_SetCharSetAndCodePage(nCharSet, nCodePage);
448                     smsg.arg1 = 1;
449                 } else {
450                     smsg.arg1 = 0;
451                 }
452                 targetHandler.sendMessage(smsg);
453 
454                 break;
455             }
456 
457             case Global.CMD_POS_SETBARCODE: {
458                 Bundle data = msg.getData();
459                 String strBarcode = data.getString(Global.STRPARA1);
460                 int nOrgx = data.getInt(Global.INTPARA1);
461                 int nType = data.getInt(Global.INTPARA2);
462                 int nWidthX = data.getInt(Global.INTPARA3);
463                 int nHeight = data.getInt(Global.INTPARA4);
464                 int nHriFontType = data.getInt(Global.INTPARA5);
465                 int nHriFontPosition = data.getInt(Global.INTPARA6);
466 
467                 boolean result = false;
468 
469                 if (USBPrinting.class.getName().equals(
470                         pos.IO.getClass().getName()))
471                     result = true;
472                 else
473                     result = pos.POS_QueryOnline(1000);
474 
475                 Message smsg = targetHandler
476                         .obtainMessage(Global.CMD_POS_SETBARCODERESULT);
477                 if (result) {
478                     pos.POS_S_SetBarcode(strBarcode, nOrgx, nType, nWidthX,
479                             nHeight, nHriFontType, nHriFontPosition);
480                     smsg.arg1 = 1;
481                 } else {
482                     smsg.arg1 = 0;
483                 }
484                 targetHandler.sendMessage(smsg);
485 
486                 break;
487             }
488 
489             case Global.CMD_POS_SETQRCODE: {
490                 Bundle data = msg.getData();
491                 String strQrcode = data.getString(Global.STRPARA1);
492                 int nWidthX = data.getInt(Global.INTPARA1);
493                 int nVersion = data.getInt(Global.INTPARA2);
494                 int necl = data.getInt(Global.INTPARA3);
495 
496                 boolean result = false;
497 
498                 if (USBPrinting.class.getName().equals(
499                         pos.IO.getClass().getName()))
500                     result = true;
501                 else
502                     result = pos.POS_QueryOnline(1000);
503 
504                 Message smsg = targetHandler
505                         .obtainMessage(Global.CMD_POS_SETQRCODERESULT);
506                 if (result) {
507                     pos.POS_S_SetQRcode(strQrcode, nWidthX, nVersion, necl);
508                     smsg.arg1 = 1;
509                 } else {
510                     smsg.arg1 = 0;
511                 }
512                 targetHandler.sendMessage(smsg);
513 
514                 break;
515             }
516 
517             case Global.CMD_EPSON_SETQRCODE: {
518                 Bundle data = msg.getData();
519                 String strQrcode = data.getString(Global.STRPARA1);
520                 int nWidthX = data.getInt(Global.INTPARA1);
521                 int nVersion = data.getInt(Global.INTPARA2);
522                 int necl = data.getInt(Global.INTPARA3);
523 
524                 boolean result = false;
525 
526                 if (USBPrinting.class.getName().equals(
527                         pos.IO.getClass().getName()))
528                     result = true;
529                 else
530                     result = pos.POS_QueryOnline(1000);
531 
532                 Message smsg = targetHandler
533                         .obtainMessage(Global.CMD_EPSON_SETQRCODERESULT);
534                 if (result) {
535                     pos.POS_EPSON_SetQRCode(strQrcode, nWidthX, nVersion, necl);
536                     smsg.arg1 = 1;
537                 } else {
538                     smsg.arg1 = 0;
539                 }
540                 targetHandler.sendMessage(smsg);
541 
542                 break;
543             }
544 
545             case Global.CMD_EMBEDDED_SEND_TO_UART: {
546                 Bundle data = msg.getData();
547                 byte[] buffer = data.getByteArray(Global.BYTESPARA1);
548                 int offset = data.getInt(Global.INTPARA1);
549                 int count = data.getInt(Global.INTPARA2);
550 
551                 Message smsg = targetHandler
552                         .obtainMessage(Global.CMD_EMBEDDED_SEND_TO_UART_RESULT);
553                 if (pos.EMBEDDED_WriteToUart(buffer, offset, count)) {
554                     smsg.arg1 = 1;
555                 } else {
556                     smsg.arg1 = 0;
557                 }
558                 targetHandler.sendMessage(smsg);
559 
560                 break;
561             }
562             }
563 
564         }
565 
566     }
567 
568     public void quit() {
569         try {
570             if (null != mLooper) {
571                 mLooper.quit();
572                 mLooper = null;
573             }
574         } catch (Exception e) {
575             e.printStackTrace();
576         }
577     }
578 
579     public void disconnectBt() {
580         try {
581             bt.Close();
582         } catch (Exception e) {
583             e.printStackTrace();
584         }
585     }
586 
587     public void disconnectBle() {
588         try {
589             ble.Close();
590         } catch (Exception e) {
591             e.printStackTrace();
592         }
593     }
594 
595     public void disconnectNet() {
596         try {
597             net.Close();
598         } catch (Exception e) {
599             e.printStackTrace();
600         }
601     }
602 
603     public void disconnectUsb() {
604         try {
605             usb.Close();
606         } catch (Exception e) {
607             e.printStackTrace();
608         }
609     }
610 
611     public void connectBle(String BTAddress) {
612         if ((null != workHandler) && (null != mLooper)) {
613             Message msg = workHandler
614                     .obtainMessage(Global.MSG_WORKTHREAD_HANDLER_CONNECTBLE);
615             msg.obj = BTAddress;
616             workHandler.sendMessage(msg);
617         } else {
618             if (null == workHandler)
619                 Log.v(TAG, "workHandler is null pointer");
620 
621             if (null == mLooper)
622                 Log.v(TAG, "mLooper is null pointer");
623         }
624     }
625 
626     public void connectBt(String BTAddress) {
627         if ((null != workHandler) && (null != mLooper)) {
628             Message msg = workHandler
629                     .obtainMessage(Global.MSG_WORKTHREAD_HANDLER_CONNECTBT);
630             msg.obj = BTAddress;
631             workHandler.sendMessage(msg);
632         } else {
633             if (null == workHandler)
634                 Log.v(TAG, "workHandler is null pointer");
635 
636             if (null == mLooper)
637                 Log.v(TAG, "mLooper is null pointer");
638         }
639     }
640 
641     public void connectNet(String IPAddress, int PortNumber) {
642         if ((null != workHandler) && (null != mLooper)) {
643             Message msg = workHandler
644                     .obtainMessage(Global.MSG_WORKTHREAD_HANDLER_CONNECTNET);
645             msg.arg1 = PortNumber;
646             msg.obj = IPAddress;
647             workHandler.sendMessage(msg);
648         } else {
649             if (null == workHandler)
650                 Log.v("WorkThread connectNet", "workHandler is null pointer");
651 
652             if (null == mLooper)
653                 Log.v("WorkThread connectNet", "mLooper is null pointer");
654         }
655     }
656 
657     public void connectUsb(UsbManager manager, UsbDevice device) {
658         if ((null != workHandler) && (null != mLooper)) {
659             Message msg = workHandler
660                     .obtainMessage(Global.MSG_WORKTHREAD_HANDLER_CONNECTUSB);
661             msg.obj = manager;
662             Bundle data = new Bundle();
663             data.putParcelable(Global.PARCE1, device);
664             msg.setData(data);
665             workHandler.sendMessage(msg);
666         } else {
667             if (null == workHandler)
668                 Log.v("WorkThread connectUsb", "workHandler is null pointer");
669 
670             if (null == mLooper)
671                 Log.v("WorkThread connectUsb", "mLooper is null pointer");
672 
673         }
674     }
675 
676     public boolean isConnecting() {
677         return isConnecting;
678     }
679 
680     public boolean isConnected() {
681         if (bt.IsOpened() || net.IsOpened() || usb.IsOpened() || ble.IsOpened())
682             return true;
683         else
684             return false;
685     }
686 
687     /**
688      * 
689      * @param cmd
690      */
691     public void handleCmd(int cmd, Bundle data) {
692         if ((null != workHandler) && (null != mLooper)) {
693             Message msg = workHandler.obtainMessage(cmd);
694             msg.setData(data);
695             workHandler.sendMessage(msg);
696         } else {
697             if (null == workHandler)
698                 Log.v(TAG, "workHandler is null pointer");
699             if (null == mLooper)
700                 Log.v(TAG, "mLooper is null pointer");
701         }
702     }
703 }

 



posted @ 2017-11-03 18:01  李小姐的猫咪生活  阅读(5007)  评论(0编辑  收藏  举报