Невозможно отправить строковые значения по одному в Arduino из приложения Bluetooth Android - PullRequest
0 голосов
/ 23 апреля 2020

Я работаю над проектом, где я передаю строку одну за другой в Arduino из моего приложения android через Bluetooth. Ниже приведен мой java код. Я отправляю строковые значения этой функции одно за другим. Проблема в том, что я получаю только одно значение в последовательном мониторе Arduino. Остальные значения не принимаются.

public static  void getDistance(float x){
    String xMoved = Float.toString(x);

    Log.d(TAG, "getDistance: called");
    try {
        mBTSocket.getOutputStream().write(xMoved.getBytes());

    } catch (IOException e) {
        e.printStackTrace();
        Log.d(TAG, "getDistance: "+e);
    }
    finally {
        Log.d(TAG, "getDistance:"+"try catch successful");
    }
}

Ниже приведен код в моем arduino:

/* Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board and Arduino UNO. More info: https://www.makerguides.com */
// Include the Arduino Stepper.h library:
#include <Stepper.h>
// Define number of steps per rotation:
const int stepsPerRevolution = 2048;
// Wiring:
// Pin 8 to IN1 on the ULN2003 driver
// Pin 9 to IN2 on the ULN2003 driver
// Pin 10 to IN3 on the ULN2003 driver
// Pin 11 to IN4 on the ULN2003 driver
// Create stepper object called 'myStepper', note the pin order:
int  state;
String command;
Stepper myStepper = Stepper(stepsPerRevolution,2,4,3,5);
void setup() {
  // Set the speed to 5 rpm:
  myStepper.setSpeed(15);

  // Begin Serial communication at a baud rate of 9600:
  Serial.begin(9600);
}
void loop() {

  if(Serial.available()){

    command = Serial.readStringUntil('\n');
    state = command.toInt();
    Serial.println(state);

    myStepper.step(state);

}
}

Вот мой logcat:

enter image description here

Я не понимаю, что я делаю не так? Кто-нибудь, пожалуйста, помогите мне с этим.

1 Ответ

2 голосов
/ 23 апреля 2020

Как уже упоминалось, вам нужна "новая строка". Попробуйте

mBTSocket.getOutputStream().write(xMoved.getBytes() +"\n");

Также может быть "проблема" нежелательных символов, отправляемых, чтобы уклониться от этого, вы должны flu sh буфер после передачи

mBTSocket.flush();
...