java - 从蓝牙连接接收字节
问题描述
我有一个连接到蓝牙模块以发送和接收数据的 android 应用程序。在发送逻辑中,它工作正常并正确发送数据。
变量:
private BluetoothSocket bluetoothSocket;
private BluetoothDevice bluetoothDevice;
private static InputStream inputStream;
private OutputStream outputStream;
连接到模块:
private void connectToDevice(String address) {
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
BluetoothSocket tmp;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Snackbar.make(parentLayout, e.getMessage(), Snackbar.LENGTH_LONG)
.setAction("IOException", null).show();
return;
}
bluetoothDevice = device;
bluetoothSocket = tmp;
mBluetoothAdapter.cancelDiscovery();
try {
bluetoothSocket.connect();
} catch (IOException connectException) {
Snackbar.make(parentLayout, connectException.getMessage(), Snackbar.LENGTH_LONG)
.setAction("IOException", null).show();
try {
bluetoothSocket.close();
} catch (IOException closeException) {
Snackbar.make(parentLayout, closeException.getMessage(), Snackbar.LENGTH_LONG)
.setAction("IOException", null).show();
}
return;
}
try {
outputStream = bluetoothSocket.getOutputStream();
inputStream = bluetoothSocket.getInputStream();
} catch (IOException e) {
Snackbar.make(parentLayout, e.getMessage(), Snackbar.LENGTH_LONG)
.setAction("IOException", null).show();
}
}
发送逻辑:
public void SendMessage(byte Destination, byte Source, int Code, byte[] Payload) {
if (Code > 255)
Opt2_16_BIT_Code = "1";
else
Opt2_16_BIT_Code = "0";
String optionsString = Opt8_Next_Message +
Opt67_Response_Options +
Opt5_Reserved +
Opt34_Trace_Options +
Opt2_16_BIT_Code +
Opt1_Extended_Flag;
byte Options = GetBytes(optionsString)[1]; // 00100000 // 0x20
Message _Message = new Message(Destination, Source, Options, Code, Payload);
AllMessage = _Message.GetAll(); // We get the whole buffer bytes to be sent to the Hexabitz modules.
try {
outputStream.write(AllMessage, 0, AllMessage.length);
} catch (IOException e) {
Snackbar.make(parentLayout, e.getMessage(), Snackbar.LENGTH_LONG)
.setAction("IOException", null).show();
}
}
以上所有工作都很好,但是当我来接收数据时,即使在调试模式下,应用程序也根本没有响应。
这是接收逻辑,我正在使用 AsyncTask,因为在正常模式下它会冻结主 UI:
public static class ReceiveDataTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String r) {
}
@Override
protected String doInBackground(String... strings) {
InputStream socketInputStream = inputStream;
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = socketInputStream.read(buffer); // Here its falling asleep and dosen't respond at all!
String readMessage = new String(buffer, 0, bytes);
testLBL.setText(readMessage);
// Send the obtained bytes to the UI Activity via handler
Log.i("logging", readMessage + "");
} catch (IOException e) {
Snackbar.make(parentLayout, e.getMessage(), Snackbar.LENGTH_LONG)
.setAction("IOException", null).show();
break;
}
}
return null;
}
}
任何建议或如何接收数据,这将是一个字节流。
解决方案
推荐阅读
- batch-file - 如何在命令行中粘贴(不使用快捷方式/鼠标)?
- java - itext DeviceGrey 与 Java 项目中的 setBackgroundColor 不兼容
- javascript - 使用包装类将每 2 个不同的元素包装在特定的 div 中
- java - 爪哇 | 类路径 | 找不到类
- oop - Pytorch tensor.method()与torch.method(张量)?
- node.js - 通过 Google OAuth2 使用用户和身份验证的 RESTful API?
- c# - 我可以使用普通目录列出方法列出 smb 共享中的文件吗?
- swiftui - SwiftUI:随机“调用中的额外参数”错误
- javascript - 向 Ag Grid Vue 显示数据
- typescript - 无需导入文件的 TypeScript 访问定义