Android屏幕在向USB设备发送数据时冻结

时间:2016-06-06 07:12:50

标签: android usb

我有以下问题 - 我需要将数据发送到我的USB设备并读取设备的标签。首先,我需要打开设备 - 这就是桌面伙伴所做的。接下来,当设备的响应等于DAAD0674016F6B26时,我需要开始扫描 - 表readId。之后取决于响应是否为DAAD046F62ADA900 - 这意味着在3秒内设备未找到要扫描的标签并关闭设备。我的任务是再次发送命令将其打开......再次,再次等等(我知道它非常昂贵 - 每3秒命令一次 - 这不是我的想法)。但是,当响应与DAAD046F62ADA900不同时 - 这是我的标签:)总结 - 如果响应是DAAD046F62ADA900我需要发送命令再次扫描我尝试用while循环执行:

while(shouldReceive) {
sendCommand
} 

但屏幕冻结,所以我尝试在线程中进行,现在它看起来像这样:

final Thread receive= new Thread(new Runnable() {
        public UsbDevice device;

        @Override
        public void run() {
            usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
            PendingIntent mPermissionIntent = PendingIntent.getBroadcast(getContext(), 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            getActivity().registerReceiver(usbReceiver, filter);
            shouldReceaive = true;
            HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            while (deviceIterator.hasNext()) {
                final UsbDevice device = deviceIterator.next();

                if (device.getVendorId() == 1659 && device.getProductId() == 8963) {
                    this.device = device;
                    usbManager.requestPermission(device, mPermissionIntent);
                    break;
                }
            }
            final UsbConnector.CallbackListener listener = new UsbConnector.CallbackListener() {
                @Override
                public void onStatusChanged(UsbConnector.Status newStatus) {
                    Toast.makeText(getContext(), "Status" + newStatus, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onScanCompleted(String result) {
                    Toast.makeText(getContext(), "Result" + result, Toast.LENGTH_SHORT).show();
                }
            };
            shouldReceaive = true;

                UsbConnector connector = new UsbConnector(getContext(), device, listener);
                connector.run();
                connector.send(pal);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                byte[] received = connector.receive(36);
                if (received == null) {
                    Toast.makeText(getContext(), "Błąd inicjalizacji skanera", Toast.LENGTH_SHORT).show();
                }
                if (received != null) {
                    String response = null;
                    long longValue = ByteBuffer.wrap(received).getLong();
                    response = Long.toHexString(longValue).toUpperCase();
                    Toast.makeText(getContext(), "RESPONSE: " + response, Toast.LENGTH_SHORT).show();
                    if (response.contentEquals("DAAD0674016F6B26")) {
                        Toast.makeText(getContext(), "Rozpoczynam skanowanie ...", Toast.LENGTH_SHORT).show();
                        connector.send(readId);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        byte[] receivedTag = connector.receive(36);
                        if (receivedTag != null) {
                            String tag = null;
                            long tagValue = ByteBuffer.wrap(receivedTag).getLong();
                            tag = Long.toHexString(tagValue).toUpperCase();
                            while (tag.contentEquals("DAAD046F62ADA900")) {
                                Toast.makeText(getContext(), "PING, SKANUJE DALEJ!", Toast.LENGTH_SHORT).show();
                                connector.send(readId);
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                                String tag2 = null;
                                long tagValue2 = ByteBuffer.wrap(receivedTag).getLong();
                                tag2 = Long.toHexString(tagValue2).toUpperCase();
                                Toast.makeText(getContext(), "ZESKANOWANY TAG!: " + tag2, Toast.LENGTH_SHORT).show();
                                for (Car cary : carList) {
                                    if (tag2.contentEquals(cary.getmNumber())) {
                                        Log.e(TAG, "setCars: JEST");
                                    } else {
                                        Log.e(TAG, "setCars: NIE MA");
                                    }
                                }

                        }
                    } else {
                        Toast.makeText(getContext(), "Błąd przy inicjalziacji skanera", Toast.LENGTH_SHORT).show();

                    }
                }
            }

    });
    mScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            receive.run();

        }
    });

但问题仍然存在。屏幕冻结,用户无能为力。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

当你调用run()方法时,它会在调用线程上执行(在 UI线程的情况下)。

 @Override
    public void onClick(View v) {
         receive.run();
    }

试试这个:

 @Override
    public void onClick(View v) {
        receive.start();
    }