开发者

Android to external bluetooth polling

I'm trying to pulse my bluetooth dongle, to see if it is range. Every second my Android sends a char to the dongle, which then responds with a char. If there is no response, or the char is wrong, then it should conclude that there is no connection.

I'm able to send from Android to dongle (attach开发者_如何学运维ed to MCU), and the dongle is able to respond. All I need is a simple way to read each char (when received) to compare it. I'm trying to do it in a seperate thread to make it run in its own loop.

Here is my code so far, but I think I might be off, so if you could give me a pointer!

void readFromInputStream()
{ 
    Thread readFromInputStream = new Thread()
    {
        public void run()
        {

            String streng = "";

            try
            {
                //just 100 times for now
                for(int i = 0; i < 100; i++)
                {

                     BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

                    if(in.read() == 'z')
                    {
                        //set variable or something
                    }
                    else
                    {
                        //do something else
                    }

                    writeTerminal('z');
                    sleep(1000);

                }

            }
            catch(Exception e)
            {
                Log.e("Threading", e.toString());
            }
            finally
      {
                finish();
      }
        }
    };
    readFromInputStream.start();
    }
}   

But it seems every time I try and evaluate something it freezes!


Just try connecting to the device and if the connection goes through you know it is present, and immediately disconnect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜