通过蓝牙将超声波传感器的数据从 Arduino 发送到 Android

我正在写我的文凭论文,但在使用蓝牙与 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 中没有错误。感谢帮助。



杨魅力
浏览 181回答 1
1回答

跃然一笑

您显然似乎有线程问题。在您的代码中HomeActivity,您已经注释掉了允许在手机上打开蓝牙服务器的代码,以便您的 Arduino 设备可以连接到它,并在UUIDRFCOM 模式下提供相关和其他相关参数。然而,该代码与网络相关并且是阻塞的,因此永远不应该在应用程序UI 线程上执行,该线程负责处理所有 UI 任务,例如显示视图、监视用户交互(触摸事件)等。这就是您的手机显示白屏且有延迟的原因。因此,您绝对应该在单独的线程上执行蓝牙逻辑。我建议使用以下类来处理所有与蓝牙相关的逻辑。这非常简单。public class BluetoothHandler {&nbsp; &nbsp; private final Handler handler;&nbsp; &nbsp; private final BluetoothAdapter bluetoothAdapter;&nbsp; &nbsp; @Nullable&nbsp; &nbsp; private BluetoothServerSocket serverSocket;&nbsp; &nbsp; private BluetoothSocket bluetoothSocket;&nbsp; &nbsp; public BluetoothHandler(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; final HandlerThread ht = new HandlerThread("Bluetooth Handler Thread", Thread.NORM_PRIORITY);&nbsp; &nbsp; &nbsp; &nbsp; ht.start(); // starting thread&nbsp; &nbsp; &nbsp; &nbsp; this.handler = new Handler(ht.getLooper());&nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void startBluetoothServer() {&nbsp; &nbsp; &nbsp; &nbsp; // execute code in our background worker thread&nbsp; &nbsp; &nbsp; &nbsp; this.handler.post(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("name", "your UUID");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bluetoothSocket = serverSocket.accept(); // will wait as long as possible (no timeout) so there is blocking&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do your logic to retrieve in and out put streams to read / write data from / to your Arduino device&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; catch (IOException ioe) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @AnyThread&nbsp; &nbsp; public void writeData(byte[] data) {&nbsp; &nbsp; &nbsp; &nbsp; // remember, all network operation are to be executed in a background thread&nbsp; &nbsp; &nbsp; &nbsp; this.handler.post(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write data in output stream&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @AnyThread&nbsp; &nbsp; public void readData(OnDataReadCallback callback) {&nbsp; &nbsp; &nbsp; &nbsp; // remember, all network operation are to be executed in a background thread&nbsp; &nbsp; &nbsp; &nbsp; this.handler.post(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // read data and notify via callback.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @AnyThread // should be call from your Activity onDestroy() to clear resources and avoid memory leaks.&nbsp; &nbsp; public void termainte() {&nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (serverSocket != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;serverSocket.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (bluetoothSocket != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bluetoothSocket.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;} catch (IOException ioe) {&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; this.handler.getLooper().quit(); // will no longer be usable. Basically, this class instance is now trash.&nbsp; &nbsp; }&nbsp; &nbsp; public interface OnDataReadCallback {&nbsp; &nbsp; &nbsp; &nbsp; @WorkerThread // watch out if you need to update some view, user your Activity#runOnUiThread method !&nbsp; &nbsp; &nbsp; &nbsp; void onDataRead(byte[] data);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java