简体   繁体   English

通过蓝牙发送字符或字符串而不是ASCII码

[英]Sending Character or String instead of ASCII code with Bluetooth

I am writing an app to send a string of characters to an Arduino using HC-05 bluetooth module. 我正在编写一个使用HC-05蓝牙模块将字符串发送到Arduino的应用程序。

my problem is I am only able to send it as ascii bytes for example this is my sending method: 我的问题是我只能以ascii字节的形式发送它,例如,这是我的发送方法:

    private void sendData()
{
    if (!(btOutputStream == null)){
        try {
            btOutputStream.write("Hello".getBytes());
                ToastMaker("Data is sent");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

and this is the output of Arduino's serial monitor: 这是Arduino的串行监视器的输出:

https://i.stack.imgur.com/88l5w.png https://i.stack.imgur.com/88l5w.png

and this is the code of Arduino: 这是Arduino的代码:

#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(10, 9); // RX, TX
int Data; // the data received

void setup() {
  Bluetooth.begin(9600);
  Serial.begin(9600);
  Serial.println("Waiting for command...");
}

void loop() {
  if (Bluetooth.available()){ //wait for data received
    Data=Bluetooth.read();
    Bluetooth.println(Data);
    Serial.println(Data);
  }
delay(100);
}

In conclusion I am looking for a way to get received data(for example for Hello) like this: H ello 总之,我正在寻找一种获取接收数据的方法(例如,对于Hello):H ello

instead of this: 72 101 108 108 111 而不是:72 101 108 108 111

You can convert the ascii code to a character using the char() method on the Arduino: 您可以使用Arduino上的char()方法将ascii代码转换为字符:

char character;

...

character = char(Data);

See the documentation for more information. 请参阅文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM