在 Android 中的 BluetoothSocket inputstream.read() 中实现超时 [英] Implement a timeout in BluetoothSocket inputstream.read() in Android

查看:23
本文介绍了在 Android 中的 BluetoothSocket inputstream.read() 中实现超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Android 中的 BluetoothSocket 的 inputstream.read() 函数中实现超时?

Is it possible to implement a timeout in an inputstream.read() function from a BluetoothSocket in Android?

我尝试过使用 Thread.sleep() 但这只会暂停我的活动.

I've tried using Thread.sleep() but this only pauses my activity.

---更新---

我有一个想法,在此处创建 2 个线程代码(t1 & t2),其中每个线程中断另一个线程,其中一个(t1)执行 sleep(5000)然后从另一侧中断另一个线程(t2)另一个线程(t2)如果在读取输入流检测到一些字符为 0x0D 中断另一个线程(t1),但这是我的问题,有人可以帮助我吗?因为我没有使用线程的interrupt()方法,希望有人能帮帮我,谢谢...

I had an idea, make 2 thread code herereads(t1 & t2) where each thread interrupt other, one of them(t1) do a sleep(5000) then interrupt the other thread(t2), from the other side the other thread(t2) if in read the inputstream detects some character as 0x0D interrupt the other thread(t1), but here is my question, does anybody can help me? because i didn't use interrupt() method of threads, I hope someone can help me, thank you...

---更新---



        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();
            }
        }
    }

这是我的实现,当某些东西进入不同的线程时,问题是如果我没有从 de mmInStream 中获取 0x3e 字符,就会达到超时.

This is my implementation when something comes in a different Thread, the problem is that the timeout will be reached if i dont get the 0x3e character from de mmInStream.

我认为在第二个例子中我必须使用notifyAll(),但是,我什么时候必须启动readThread()?

I supposed that in the second example i must use a notifyAll(), but, when do I have to start the readThread()?

谢谢@weeman

推荐答案

你可以这样做:

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);

通过这种方式,您可以最初从具有特定超时的流中读取.如果你想在阅读的任何时间实现超时,你可以尝试这样的事情:

This way you are able to initially read from a stream with a specific timeout. If u want to implement a timeout at any time of reading you can try something like this:

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) {}

使用它,您可能需要某种事件处理程序来通知您的应用程序线程是否确实从输入流中读取了某些内容.

using this you may need some kind of event handler to notify your application if the thread actually read something from the input stream.

希望能帮到你!

----

我在不使用任何处理程序的情况下实现了一个示例.

I implemented an example without using any handlers.

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();
        }

    }

这篇关于在 Android 中的 BluetoothSocket inputstream.read() 中实现超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆