如何通过Android中的蓝牙接收数据时检测超时

时间:2017-10-31 03:08:23

标签: android bluetooth settimeout connection-timeout read-data

在我的Android应用程序中,我通过蓝牙向嵌入式硬件设备发送数据或从嵌入式硬件设备接收数据。我使用背景服务来实现这一目标。要阅读蓝牙数据,我使用下面的代码,

public void run() {
        Log.d("DEBUG BT", "IN CONNECTED THREAD RUN");

        byte[] buffer = new byte[10240]; //1kb=1024bytes
        int begin = 0;
        int bytes = 0;

        // Keep listening to the InputStream until an exception occurs.

        while (true) {
            try {
                System.out.println("-------------New Data received");
                al = new ArrayList<>();

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("InterruptedException during sleep");
                }

                // Read from the InputStream.

                bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);

                    for(int i = begin; i < bytes; i++) {
                        if(buffer[i] == "#".getBytes()[0]) {

                            // Send the obtained bytes to the UI activity.
                            bluetoothIn.obtainMessage(1, begin, i, buffer).sendToTarget();
                            begin = i + 1;
                            if(i == bytes - 1) {
                                bytes = 0;
                                begin = 0;
                            }
                        }
                    }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Problem in reading Data:Input stream was disconnected "+e.getMessage());
                Handler hand = new Handler(Looper.getMainLooper());
                hand.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(BluetoothDataService.this.getApplicationContext(),"Problem in reading Data",Toast.LENGTH_SHORT).show();
                        dismissDialog();
                    }
                });
                break;
            }
        }
    }

我将向我的硬件单元发送查询并将通过蓝牙等待数据,但我不知道如何检查超时,我使用进度条直到我收到数据但有时如果我没有收到数据超过20秒我需要提醒用户&#34;超时&#34;但在我的情况下,进度仍在加载,我被困在如何实现这一点,请有人建议如何检查超时

1 个答案:

答案 0 :(得分:0)

public void run() {
        Log.d("DEBUG BT", "IN CONNECTED THREAD RUN");

        byte[] buffer = new byte[10240]; //1kb=1024bytes
        int begin = 0;
        int bytes = 0;
        int timeOut = 10000;
        int currTime = 0;
        int interval = 50;

        // Keep listening to the InputStream until an exception occurs.

        while (true) {
            try {
                al = new ArrayList<>();

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("InterruptedException during sleep");
                }

                // Read from the InputStream.
                if(mmInStream.available() > 0){
                    // something just arrived?

                    bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);

                    currTime = 0;                               // resets the timeout

                    for(int i = begin; i < bytes; i++) {
                        if(buffer[i] == "#".getBytes()[0]) {

                            // Send the obtained bytes to the UI activity.
                            bluetoothIn.obtainMessage(1, begin, i, buffer).sendToTarget();
                            begin = i + 1;
                            if(i == bytes - 1) {
                                bytes = 0;
                                begin = 0;
                            }
                        }
                    }
                }else if(currTime < timeOut) {
                    // do we have to wait some more?
                    try {
                        Thread.sleep(interval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.out.println("InterruptedException occurs while waiting for data from TPRS, "+e.getMessage());
                        printMessage("InterruptedException occurs while waiting for data from TPRS, ");
                    }
                    currTime += interval;
                }else {
                    // timeout detected
                    handleTimeOut();
                    dismissDialog();
                    currTime = 0;                           // resets the timeout
                }

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Problem in reading Data:Input stream was disconnected "+e.getMessage());
                printMessage("Problem in reading Data");
                break;
            }
        }
    }