Android 蓝牙更新UI线程
我正在为Infineon XMC4500微处理器编写一个嵌入式应用程序。 我从不同的传感器读取数据,然后通过蓝牙模块发送这些数据,以便在Android smarphone的屏幕上显示它们。我使用GraphView库 我根据Android教程实现了BluetoothClass。如果我从单个传感器获得数据,那么一切都正常。但不幸的是,当我从多个传感器获取数据时,我的应用程序无法顺利运行 当然,我在单独的线程中进行蓝牙连接,并尝试在处理程序中更新UI。 请告诉我我做错了什么。当然,我通过JSON交换格式发送数据: 我从微控制器端发送类似的信息:Android 蓝牙更新UI线程,android,multithreading,bluetooth,Android,Multithreading,Bluetooth,我正在为Infineon XMC4500微处理器编写一个嵌入式应用程序。 我从不同的传感器读取数据,然后通过蓝牙模块发送这些数据,以便在Android smarphone的屏幕上显示它们。我使用GraphView库 我根据Android教程实现了BluetoothClass。如果我从单个传感器获得数据,那么一切都正常。但不幸的是,当我从多个传感器获取数据时,我的应用程序无法顺利运行 当然,我在单独的线程中进行蓝牙连接,并尝试在处理程序中更新UI。 请告诉我我做错了什么。当然,我通过JSON交换格
sprintf(json_data, "{"
"\"m\":"
"["
"{"
"\"id\":a,"
"\"x\":%.2f,"
"\"y\":%.2f,"
"\"z\":%.2f"
"},"
"{"
"\"id\":g,"
"\"x\":%.2f,"
"\"y\":%.2f,"
"\"z\":%.2f"
"},"
"{"
"\"id\":m,"
"\"x\":%.2f,"
"\"y\":%.2f,"
"\"z\":%.2f"
"},"
"{"
"\"id\":t,"
"\"x\":%.2f"
"},"
"{"
"\"id\":h,"
"\"x\":%.2f"
"}"
"]"
"}", getAccelXf(), getAccelYf(), getAccelZf(), getGyroXf(), getGyroYf(), getGyroZf(), getMagnetXf(), getMagnetYf(), getMagnetZf(), readTemperature(), readHumidity());
作为请求的附件,我添加了几段源代码:
public class ConnectedThread extends Thread
{
BluetoothSocket connectedSocket;
InputStream inStream;
OutputStream outStream;
public ConnectedThread(BluetoothSocket socket)
{
connectedSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try
{
if (connectedSocket != null)
{
tmpIn = connectedSocket.getInputStream();
tmpOut = connectedSocket.getOutputStream();
mHandler.obtainMessage(Constants.MESSAGE_DEVICE_CONNECTED_SUCCESSFULLY).sendToTarget();
// Toast.makeText(activityContext, "connectedSocket != null", Toast.LENGTH_LONG);
}
}
catch (IOException e) {
mHandler.obtainMessage(Constants.MESSAGE_INPUT_OUTPUT_STREAM_UNAVAILABLE).sendToTarget();
e.printStackTrace();
}
inStream = tmpIn;
outStream = tmpOut;
}
public void run()
{
if(bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
byte[] buffer = new byte[4096];
int bytes;
String dupa;
while (true)
{
try {
if (inStream != null)
{
bytes = inStream.available();
if(bytes > 0 && bytes <= 200)
{
byte[] pocketBytes = new byte[bytes];
inStream.read(pocketBytes);
//Log.d(TAG, "setState() " + mState + " -> " + state);
System.out.println(pocketBytes.toString());
dupa = pocketBytes.toString();
System.out.println(dupa);
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, pocketBytes).sendToTarget();
}
}
} catch (IOException e) {
mHandler.obtainMessage(Constants.MESSAGE_REMOTE_DEV_DISCONNECTED).sendToTarget();
break;
}
/*try
{
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
outStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
connectedSocket.close();
} catch (IOException e) { }
}
}
public类ConnectedThread扩展线程
{
蓝牙插座连接插座;
流内输入流;
输出流外流;
公共连接线程(BluetoothSocket)
{
connectedSocket=插座;
InputStream tmpIn=null;
OutputStream tmpOut=null;
尝试
{
if(connectedSocket!=null)
{
tmpIn=connectedSocket.getInputStream();
tmpOut=connectedSocket.getOutputStream();
mHandler.ActainMessage(常量。已成功连接设备的消息)。sendToTarget();
//Toast.makeText(activityContext,“connectedSocket!=null”,Toast.LENGTH\u LONG);
}
}
捕获(IOE异常){
mHandler.ActainMessage(常量。消息输入输出流不可用)。sendToTarget();
e、 printStackTrace();
}
流内=tmpIn;
外流=tmpOut;
}
公开募捐
{
if(bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
字节[]缓冲区=新字节[4096];
整数字节;
字符串重复;
while(true)
{
试一试{
如果(流内!=null)
{
字节=inStream.available();
如果(bytes>0&&bytesMateusz;查看操作块迫使您的代码以低于UI代码的优先级进入后台。您也可以尝试GCD。这里是一个非常简单的代码块,作为一个单独的过程分离出来
将UI代码置于最高优先级,将bluetooth置于最低优先级;确保您在UI更新中输入的语句都是这样做的,不要将任何不更新UI的内容放在那里