实现Android中的BluetoothSocket inputstream.read()超时(Im

2019-06-24 06:52发布

是否有可能在Android上的BluetoothSocket实现的inputstream.read()函数超时?

我已经使用Thread.sleep()方法尝试,但这种仅停留我的活动。

---更新---

我有一个想法,使2个线程代码herereads(T1&T2),其中每个线程中断等,他们(T1)做一个睡眠(5000)则中断其他线程(T2),在另一侧的线程(之一T2)如果该InputStream读取检测到某些字符作为0X0D中断其他线程(T1),但这里是我的问题,没有任何人能帮助我吗? 因为我没有使用的线程中断()方法,我希望有人能帮助我,谢谢...

---更新---



        public void run(){
        while(true){
            try {
            char r;
            String respuesta = "";
            while (true) {
                    r = (char) mmInStream.read();
                respuesta += r;
                if (r == 0x3e) {
                break;
                    }
                }
            respuesta = respuesta.replaceAll(" ", "");
            Log.d("respuesta", respuesta);
            rHandler.obtainMessage(MESSAGE_READ, -1, -1, respuesta).sendToTarget();
            } catch (IOException readException) {
            Log.e("ServicioGeneral", "Error de lectura", readException);
            this.interrupt();
            connectionLost();
            // posibly break();
            }
        }
    }

这是我实现的时候出现一些这样在不同的线程,问题是,如果我不得到德mmInStream的0x3E的字符超时将达到。

我推测,在第二个例子我必须使用notifyAll的(),但是,当我不得不开始readThread()?

谢谢你,@weeman

Answer 1:

你可以这样做:

InputStream in = someBluetoothSocket.getInputStream();
int timeout = 0;
int maxTimeout = 8; // leads to a timeout of 2 seconds
int available = 0;

while((available = in.available()) == 0 && timeout < maxTimeout) {
    timeout++;
    // throws interrupted exception
    Thread.sleep(250);
}

byte[] read = new byte[available];
in.read(read);

这样,您就能够从最初具有特定超时流中读取。 如果妳想要在阅读,你可以尝试这样的事情的任何时间执行超时:

Thread readThread = new ReadThread(); // being a thread you use to read from an InputStream
try {
   synchronized (readThread) {
      // edit: start readThread here
      readThread.start();
      readThread.wait(timeOutInMilliSeconds);
   }
catch (InterruptedException e) {}

使用这个你可能需要某种形式的事件处理程序的线程是否实际阅读从输入流的东西通知你的应用程序。

我希望帮助!

- - 编辑:

我实现了一个例子,而无需使用任何处理程序。

Socket s = new Socket("localhost", 8080);
    final InputStream in = s.getInputStream();

    Thread readThread = new Thread(new Runnable() {
        public void run() {
            int read = 0;
            try {
                while((read = in.read()) >= 0) {
                    System.out.println(new String(new byte[]{ (byte) read }));
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    synchronized (readThread) {
        readThread.start();
        try {
            readThread.wait(2000);

            if(readThread.isAlive()) {
                                    // probably really not good practice!
                in.close();
                System.out.println("Timeout exceeded!");
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


文章来源: Implement a timeout in BluetoothSocket inputstream.read() in Android