Android 到外部蓝牙轮询

发布于 2024-10-01 03:30:58 字数 1293 浏览 2 评论 0原文

我正在尝试对我的蓝牙适配器进行脉冲,看看它是否在范围内。 我的 Android 每秒都会向加密狗发送一个字符,然后加密狗会用一个字符进行响应。如果没有响应,或者字符错误,那么应该断定没有连接。

我能够从 Android 发送到加密狗(连接到 MCU),并且加密狗能够响应。 我所需要的只是一种简单的方法来读取每个字符(收到时)以进行比较。我正在尝试在单独的线程中执行此操作,以使其在自己的循环中运行。

到目前为止,这是我的代码,但我想我可能已经关闭了,所以如果你能给我一个指示!

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

但似乎每次我尝试评估某些东西时它都会冻结!

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 (attached 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2024-10-08 03:30:58

只需尝试连接到设备,如果连接成功,您就知道它存在,并立即断开连接。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文