佚名通过本文主要向大家介绍了stm32 android,android字节编辑器,android 字节流,android 字节转字符,android 蓝牙开发等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: android蓝牙软件接收STM32发送过来字节流问题
描述:
/**
* 本线在连接与远程设备。
* 它处理所有传入和传出的传输。
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// 继续听InputStream同时连接
while (true) {
try {
// 读取输入流
bytes = mmInStream.read(buffer);
if (buffer == null)
continue;
if (mHandler== null) {
return;
}
// 发送获得的字节的用户界面
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {&n
描述:
本帖最后由 YJK2515279130 于 2015-06-08 11:42:42 编辑
androidsocket蓝牙stm32线程
本人是一个android开关的菜鸟,最近开发一个android蓝牙控制STM32下位机软件,能正常的进行每5S发送一次询问信息,然后在下位机返回具体的信息时,出现接收数据不完整或过多的问题,具体发送接收线程如下:/**
* 本线在连接与远程设备。
* 它处理所有传入和传出的传输。
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// 继续听InputStream同时连接
while (true) {
try {
// 读取输入流
bytes = mmInStream.read(buffer);
if (buffer == null)
continue;
if (mHandler== null) {
return;
}
// 发送获得的字节的用户界面
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {&n