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

查看:1616
本文介绍了在实施的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线code herereads(T1&安培; T2),其中每个线程中断等,它们(T1)的一个做一个睡眠(5000)再打断对方的线程(T2),从另一侧的另一个线程(T2)如果在读取输入流检测到某些字符作为0X0D中断其他线程(T1),但这里是我的问题,没有任何人能帮助我吗?因为我没有使用的线程中断()方法,我希望有人能帮助我,谢谢...

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

这是我实现当这个声音在不同的线程,问题是,如果我不得到德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

Thank you, @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();
        }

    }

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

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