4APP蓝牙通讯协议 格式:10Hz, 以ASCII字符串发送,英文逗号分隔,\r\n结束。 表5数据通讯协议 序号 数据定义 数据类型 备注 1 帧序号 unsigned int 每一帧数据+1,单独计数 2 系统状态 unsigned char 分别为 0 1 2 3 4 5 10,见表2 3 寻北进度 unsigned char 1~100 4 航向角 float 0~360° 5 俯仰角 float -90°~90° 6 横滚角 float -180°~180° 7 实时更新数据 float 帧序号进行除以10取余计算,见表3以里面的要求在andriod studio中写一个类
时间: 2024-03-09 07:51:22 浏览: 63
以下是在Android Studio中编写的一个类,用于发送和接收4APP蓝牙通讯协议格式的数据:
```
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class BluetoothConnectionService {
private static final String TAG = "BluetoothConnectionServ";
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private final BluetoothAdapter mBluetoothAdapter;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private Handler mHandler;
private int mState;
private static final int STATE_NONE = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;
public BluetoothConnectionService(Handler handler) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mHandler = handler;
}
private synchronized void setState(int state) {
Log.d(TAG, "setState() " + mState + " -> " + state);
mState = state;
mHandler.obtainMessage(MainActivity.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
}
public synchronized int getState() {
return mState;
}
public synchronized void connect(BluetoothDevice device) {
Log.d(TAG, "connect to: " + device);
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
Log.d(TAG, "connected");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME);
msg.obj = device.getName();
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
}
public synchronized void stop() {
Log.d(TAG, "stop");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
setState(STATE_NONE);
}
public void write(String message) {
ConnectedThread r;
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
byte[] msgBuffer = (message + "\r\n").getBytes();
r.write(msgBuffer);
}
private void connectionFailed() {
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
msg.obj = "Unable to connect device";
mHandler.sendMessage(msg);
BluetoothConnectionService.this.start();
}
private void connectionLost() {
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_TOAST);
msg.obj = "Device connection was lost";
mHandler.sendMessage(msg);
BluetoothConnectionService.this.start();
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException e) {
connectionFailed();
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
BluetoothConnectionService.this.start();
return;
}
synchronized (BluetoothConnectionService.this) {
mConnectThread = null;
}
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
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;
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;
while (true) {
try {
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
mHandler.obtainMessage(MainActivity.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
}
```
这个类中包含了一个ConnectThread和一个ConnectedThread。ConnectThread用于在蓝牙设备上建立连接,而ConnectedThread用于处理连接后的输入和输出流。根据4APP蓝牙通讯协议的格式,我们可以在ConnectedThread的write()方法中将要发送的字符串转换为字节数组,然后将它们写入到输出流中。接收到的数据可以在ConnectedThread的run()方法中使用mmInStream读取,并将读取的字节转换为字符串后发送到主线程中进行处理。
阅读全文