【发布时间】:2023-03-30 08:25:01
【问题描述】:
问候 Stack Overflow 社区,
我正在编写一个从 HC05 蓝牙模块读取一些数据的 Android 应用程序。
我只是想读取一个字节,但它只是挂在那里几分钟,然后就崩溃了。也许它是一个硬件问题,但也许我的代码中有一些我没有看到的错误。如果有人知道为什么我无法从这个模块中阅读,我将不胜感激。
谢谢!
for (BluetoothDevice iterator : bondedDevices) {
if (iterator.getName().equals("HC-05")) {
BluetoothDevice device = iterator;
try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);} catch (IOException ex) {}
try {btSocket.connect();} catch (IOException ex) {}
if (btSocket.isConnected()) {
Toast.makeText(getApplicationContext(), "Connected to HC05", Toast.LENGTH_SHORT).show();
//Write 'r' to arduino to tell it to send data.
try {
outStream = btSocket.getOutputStream();
outStream.write('r');
} catch (IOException ex) {}
int byteCount = 1;
byte[] rawBytes = new byte[byteCount];
try {
inputStream= btSocket.getInputStream();
int a = inputStream.read(rawBytes, 0, 1);
}
【问题讨论】:
标签: java android bluetooth arduino