【问题标题】:Sending String from Arduino to Processing app on Android over Bluetooth通过蓝牙将字符串从 Arduino 发送到 Android 上的处理应用程序
【发布时间】:2018-11-30 16:24:42
【问题描述】:

我对 Arduino 和 Processing 还很陌生,这也是我第一次在互联网上询问有关编码的问题。 我目前正在尝试为一个学校项目做以下事情:我想通过蓝牙将一个包含来自我 Arduino 上传感器的数据的字符串发送到安卓手机或平板电脑。 为简单起见,我降低了代码的复杂性,删除了代码中不利于将字符串从 Arduino 传输到 Android 上的 Processing 的每一部分。以下代码只是尝试将字符串“S01E”发送到我手机上的处理应用程序,将字符串保存到我的“字符串信息”中并以文本形式显示该字符串(信息,20,110);元素。

附加信息: Arduino蓝牙模块是“Bluefruit EZ-Ling 蓝牙盾”。 手机是三星 Galaxy S8。 Processing App 是使用 Android 模式的 Processing 生成的。 我的手机已成功通过蓝牙与 arduino 配对。

代码编译成功,但只显示我的文本(“test text”, 20, 120);在我的手机上,但不是我的 text(info, 20, 110);,我认为这意味着没有收到字符串并且信息字符串保持为空。

我该如何从这里开始?我的代码中是否存在一些明显的问题。以及如何使用我使用的奇怪技术堆栈正确调试我的代码?

处理代码:

import netP5.*;

import android.content.Intent;
import android.os.Bundle;

import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;

KetaiBluetooth bt;
boolean isConfiguring = true;


String info = "";
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList();

// States of the two sensors
int B1in = 0;
int B2in = 0;

//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************


void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}

void setup() {
 size(displayWidth, displayHeight);
 frameRate(10);
 orientation(PORTRAIT);
 background(255);
 stroke(160);
 fill(50);

 //start listening for BT connections
 bt.start();
 //at app start select device…
 isConfiguring = true;
}

void draw() {
  background(255);

  text(info,20,110);
  text("test text",20,120);
  println(info);

}

void onKetaiListSelection(KetaiList klist)
{
  String selection = klist.getSelection();
  bt.connectToDeviceByName(selection);

  //dispose of list for now
  klist = null;
}

//Call back method to manage data received
void onBluetoothDataEvent(String who, byte[] data) {
 if (isConfiguring)
 return;
 //received
 info = new String(data);
}

这是我的 Arduino 代码:

#include <SoftwareSerial.h>

SoftwareSerial bt(2,3); // RX, TX


// Enthält den String, der an den PC geschickt wird
String data = "S01E";

// Serielle Schnittstelle einrichten, pinModes setzen
void setup() {

  bt.begin(9600);
  Serial.begin(9600);
}
void loop() {

  Serial.println(data);

}

我也在 discourse.processing.org 上问了同样的问题。我的问题的链接: https://discourse.processing.org/t/sending-string-from-arduino-to-processing-app-on-android-over-bluetooth/6106

【问题讨论】:

  • 为什么不发布信息字符串的内容?它可能包含一些不可打印的字符代码。考虑将其编码为 base64 之类的东西。

标签: android bluetooth arduino processing


【解决方案1】:

你能试试这个吗?

 void setup() {
  isConfiguring = true;
  size(displayWidth, displayHeight);
  frameRate(10);
  orientation(PORTRAIT);
  background(255);
  stroke(160);
  fill(50);

  //start listening for BT connections
  bt.start();
  //at app start select device…
  isConfiguring = false;
 }

您用于从蓝牙事件中检索数据的函数似乎只是返回,因为您的布尔标志永远不会从 true 改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多