gpt4 book ai didi

android - 无法使用蓝牙 HC-06 发送数据 - 应用程序停止工作

转载 作者:行者123 更新时间:2023-11-30 00:39:43 26 4
gpt4 key购买 nike

我正在尝试在 Arduino 和 Android 设备之间建立连接。我正在使用:

  • 阿杜诺·莱昂纳多
  • 蓝牙设备:HC-06

我的 Android 应用程序应该读取从 Arduino 设备发送的数据。这是 Arduino 的代码。

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:

delay(500);
Serial.print("#A~");
Serial1.print("#A~");
}

我的应用程序正在与 Arduino 建立连接,但问题是我的应用程序在从 Arduino 接收数据时崩溃了。它说“不幸的是 BluetoothApp 已停止”。

奇怪的是,我可以使用适用于 Android 的蓝牙终端应用程序从 Arduino 接收数据。

这是安卓的代码:

    package com.example.matt.onresumeapp;

import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.String;
import java.util.UUID;

public class ComunicationActivity extends AppCompatActivity {


TextView copyInfo;
TextView handlerInfo;
Handler h;
BluetoothAdapter btAdapter;

private String adress;
private ConnectedThread mConnectedThread;
private static UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothSocket mmSocket;
private StringBuilder stBuilder = new StringBuilder();

final int handlerState = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comunication);

copyInfo = (TextView) findViewById(R.id.copyInfo);
handlerInfo = (TextView) findViewById(R.id.handlerInfo);



h = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

if(msg.what == handlerState){

String readMessage = (String) msg.obj;
stBuilder.append(readMessage);

int endOfLine = stBuilder.indexOf("~");


if(stBuilder.charAt(0) == '#'){

String dataInPrint = stBuilder.substring(1, endOfLine);
handlerInfo.setText(dataInPrint);

}

stBuilder.delete(0, endOfLine);

}

}
};

btAdapter = BluetoothAdapter.getDefaultAdapter();

}

private BluetoothSocket btSocketConnection (BluetoothDevice device) throws IOException{

return device.createInsecureRfcommSocketToServiceRecord(myUUID);
}


@Override
public void onResume() {
super.onResume();

Intent newIntent = getIntent();
adress = newIntent.getStringExtra("EXTRA_ADRESS");


BluetoothDevice device = btAdapter.getRemoteDevice(adress);

try {
mmSocket = btSocketConnection(device);



} catch (IOException e) {

Toast.makeText(getBaseContext(), "Socket's method failed", Toast.LENGTH_LONG).show();

}

try {
mmSocket.connect();
} catch (IOException e){

try {

mmSocket.close();
} catch (IOException e1)
{
Toast.makeText(getBaseContext(), "Socket's connect() method failed", Toast.LENGTH_LONG).show();
}

}
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();


}



private class ConnectedThread extends Thread{

private InputStream mmInStream;
private OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket){

mmSocket = socket;

InputStream tmpIn = null;
OutputStream tmpOut= null;

try {

tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e){

Toast.makeText(getBaseContext(), "Error occurred when creating input stream", Toast.LENGTH_LONG).show();

}

try {
tmpOut = socket.getOutputStream();

} catch (IOException e){


Toast.makeText(getBaseContext(), "Error occurred when creating output stream", Toast.LENGTH_LONG).show();
}

mmInStream = tmpIn;
mmOutStream = tmpOut;

}


public void run() {
super.run();

byte[] mmBuffer = new byte[1024];
int numBytes;

while (true){
try{
numBytes = mmInStream.read(mmBuffer);

String readMessage = new String(mmBuffer, 0, numBytes);
h.obtainMessage(handlerState, numBytes, -1, readMessage).sendToTarget();

} catch (IOException e){

Toast.makeText(getBaseContext(), "Input stream was disconnected", Toast.LENGTH_LONG).show();
break;
}

}

}
}

}

我已经尝试解决这个问题几天了,但没有任何进展......我非常感谢任何帮助。

谢谢!

最佳答案

已解决:

只是将处理程序方法更改为:

 h = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

if(msg.what == handlerState){

byte[] rBuff = (byte[]) msg.obj;

String readMessage = new String(rBuff, 0, msg.arg1);
handlerInfo.setText(readMessage);

}

}
};

关于android - 无法使用蓝牙 HC-06 发送数据 - 应用程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com