Android 到外部蓝牙轮询
我正在尝试对我的蓝牙适配器进行脉冲,看看它是否在范围内。 我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需尝试连接到设备,如果连接成功,您就知道它存在,并立即断开连接。
Just try connecting to the device and if the connection goes through you know it is present, and immediately disconnect.