通过蓝牙将数据从Arduino 从Arduino发送到Android

Sending data from ultrasonic sensor from Arduino to Android via Bluetooth

本文关键字:Arduino Android 数据      更新时间:2023-10-16

我正在处理我的毕业论文,但我在使用蓝牙与Arduino -> Android通信时遇到问题。 这是我的应用程序: 我想显示到障碍物的距离的活动

在 TextView 中,我想将来自 Arduino 的数据与距离放在一起,我需要想法,我找不到什么,如何将数据从不同的传感器发送到不同的视图(例如前、后保险杠、左右(。

这里有arduino代码:

#include <SoftwareSerial.h>
// Mid-back sensor
#define trigPinLeft 11 
#define echoPinLeft 10
// Right-back sensor (looking from back)
#define trigPinRight 7
#define echoPinRight 6
SoftwareSerial btSerial = SoftwareSerial(0,1);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
btSerial.begin(115200);
// Mid-back sensor
pinMode(trigPinLeft, OUTPUT);
pinMode(echoPinLeft, INPUT);
// Right-back sensor 
pinMode(trigPinRight, OUTPUT);
pinMode(echoPinRight, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
long durationLeft, distanceLeft;
digitalWrite(trigPinLeft, LOW);
delayMicroseconds(5);
digitalWrite(trigPinLeft, HIGH);
delayMicroseconds(5);
digitalWrite(trigPinLeft, LOW);
durationLeft = pulseIn(echoPinLeft, HIGH);
distanceLeft = (durationLeft *0.034 / 2);
if (distanceLeft>=400 || distanceLeft<=18){
Serial.println("Out of range"); 
btSerial.println("Out of range");
}
else{
Serial.print("BACK LEFT: ");
Serial.print(distanceLeft);
Serial.println(" cm");
btSerial.println(distanceLeft + "cm");
}
//delayMicroseconds(1);
long durationRight, distanceRight;
digitalWrite(trigPinRight, LOW);
delayMicroseconds(5);
digitalWrite(trigPinRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRight, LOW);
durationRight = pulseIn(echoPinRight, HIGH);
distanceRight = (durationRight *0.034 / 2);
if (distanceRight>=400 || distanceRight<=18){
Serial.println("Out of range"); 
btSerial.println("Out of range");
}
else{
Serial.print("BACK RIGHT: ");
Serial.print(distanceRight);
Serial.println(" cm");
btSerial.println(distanceRight + "cm");
}
delay(10000);
}

这是Android-Studio代码,我想一次将数据从Arduino获取到一个textView(不起作用(:

public class HomeActivity extends AppCompatActivity {
ImageView imageView;
TextView rearLeft,rearMid,rearRight,frontLeft,frontMid,frontRight;
public static final String PREFS_NAME = "ParkingPrefsFile";
public static final String FIRST_TIME = "firstTime";
public static final String IMAGE_VAL = "imageValue";
private BluetoothServerSocket mmServerSocket;
private BluetoothAdapter mAdapter;
private BluetoothDevice mDevice;
private static final UUID MY_UUID = UUID.fromString("5951c386-e2e7-485d-aebe-a32eec769f7b");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
imageView = (ImageView) findViewById(R.id.carView);
rearLeft = (TextView) findViewById(R.id.rearLeftText);
rearMid = (TextView) findViewById(R.id.rearMidText);
rearRight = (TextView) findViewById(R.id.rearRightText);
frontLeft = (TextView) findViewById(R.id.frontLeftText);
frontMid = (TextView) findViewById(R.id.frontMidText);
frontRight = (TextView) findViewById(R.id.frontRightText);
BluetoothSocket socket = null;
mAdapter = BluetoothAdapter.getDefaultAdapter();
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean firstTime = sharedPreferences.getBoolean(FIRST_TIME,false);
if(!firstTime){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(FIRST_TIME,true);
int image = getIntent().getIntExtra("image", R.drawable.ic_default);
imageView.setImageResource(image);
editor.putString(IMAGE_VAL, String.valueOf(getIntent().getIntExtra("image",R.drawable.ic_default)));
editor.commit();
}
else {
SharedPreferences.Editor editor = sharedPreferences.edit();
int image = getIntent().getIntExtra("image", Integer.parseInt(sharedPreferences.getString(IMAGE_VAL,null )));
imageView.setImageResource(image);
editor.putString(IMAGE_VAL, String.valueOf(getIntent().getIntExtra("image",image)));
editor.commit();
}
/*try{
//mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord("My Adapter", MY_UUID);
mmServerSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}*/
/*byte[] buffer = new byte[256];
int bytes;
try{
mmServerSocket.close();
InputStream inputStream = null;
OutputStream outputStream = null;
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
DataInputStream mmInputStream = new DataInputStream(inputStream);
DataOutputStream mmOutputStream = new DataOutputStream(outputStream);
bytes = mmInputStream.read(buffer);
String readMessage = new String(buffer, 0 , bytes);
rearLeft.setText(readMessage);
} catch (IOException e) {
e.printStackTrace();
}*/
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_activity_menu,menu);
return true;
}
}

我编译并运行了应用程序,但是在看到启动画面后,我看到白屏并且手机滞后。 我在Android-Studio中没有错误。 感谢您的帮助。

您显然似乎有线程问题。

在您的HomeActivity中,您注释掉了允许在手机上打开蓝牙服务器的代码,以便您的 Arduino 设备可以连接到它,在 RFCOM 模式下提供相关UUID和其他相关参数。

但是,该代码与网络相关且阻塞,因此永远不应在负责处理所有 UI 任务(如显示视图、监视用户交互(触摸事件(等(的应用UI 线程上执行。

这就是您的手机显示滞后的白屏的原因。

因此,您绝对应该在单独的线程上执行蓝牙逻辑。

我建议使用以下类来处理所有与蓝牙相关的逻辑。这很简单。

public class BluetoothHandler {
private final Handler handler;
private final BluetoothAdapter bluetoothAdapter;
@Nullable
private BluetoothServerSocket serverSocket;
private BluetoothSocket bluetoothSocket;

public BluetoothHandler(Context context) {
final HandlerThread ht = new HandlerThread("Bluetooth Handler Thread", Thread.NORM_PRIORITY);
ht.start(); // starting thread
this.handler = new Handler(ht.getLooper());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
this.bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
} else {
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
}

public void startBluetoothServer() {
// execute code in our background worker thread
this.handler.post(new Runnable() {
@Override
public void run() {
try {
serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("name", "your UUID");
bluetoothSocket = serverSocket.accept(); // will wait as long as possible (no timeout) so there is blocking
// do your logic to retrieve in and out put streams to read / write data from / to your Arduino device
}  catch (IOException ioe) {
}
} 
});
}

@AnyThread
public void writeData(byte[] data) {
// remember, all network operation are to be executed in a background thread
this.handler.post(new Runnable() {
@Override
public void run() {
// write data in output stream     
}
});
}

@AnyThread
public void readData(OnDataReadCallback callback) {
// remember, all network operation are to be executed in a background thread
this.handler.post(new Runnable() {
@Override
public void run() {
// read data and notify via callback.
}
});
}

@AnyThread // should be call from your Activity onDestroy() to clear resources and avoid memory leaks.
public void termainte() {
try {
if (serverSocket != null) {
serverSocket.close();
}
if (bluetoothSocket != null) {
bluetoothSocket.close();
}
} catch (IOException ioe) {
}
this.handler.getLooper().quit(); // will no longer be usable. Basically, this class instance is now trash.
}

public interface OnDataReadCallback {
@WorkerThread // watch out if you need to update some view, user your Activity#runOnUiThread method !
void onDataRead(byte[] data);
}
}