无法从蓝牙 Android 应用程序将字符串值一个一个地发送到 Arduino
Cannot send string values one by one to Arduino from a Bluetooth Android app
我正在做一个项目,我使用蓝牙从我的 android 应用程序将字符串一个一个地传递给 Arduino。以下是我的 java code.I 通过 one.The 向此函数发送字符串值一个 one.The 问题是我在 Arduino 序列号中只收到一个值 monitor.The 其余值未收到。
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:
我很困惑我做错了什么?任何人都可以帮我解决这个问题。
如前所述,您需要 "newline" 尝试
mBTSocket.getOutputStream().write(xMoved.getBytes() +"\n");
可能还会发送 "problem" 不需要的字符来避免这种情况,您应该在传输后刷新缓冲区
mBTSocket.flush();
我正在做一个项目,我使用蓝牙从我的 android 应用程序将字符串一个一个地传递给 Arduino。以下是我的 java code.I 通过 one.The 向此函数发送字符串值一个 one.The 问题是我在 Arduino 序列号中只收到一个值 monitor.The 其余值未收到。
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:
我很困惑我做错了什么?任何人都可以帮我解决这个问题。
如前所述,您需要 "newline" 尝试
mBTSocket.getOutputStream().write(xMoved.getBytes() +"\n");
可能还会发送 "problem" 不需要的字符来避免这种情况,您应该在传输后刷新缓冲区
mBTSocket.flush();