通过蓝牙将字符串从Arduino发送到Android上的Processing应用程序

时间:2018-11-30 16:24:43

标签: android bluetooth arduino processing

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

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

代码编译成功,但是只显示我的文本(“ test text”,20,120);在我的手机上,而不是在我的文本(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

1 个答案:

答案 0 :(得分:0)

您可以尝试吗?

 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更改,似乎您从蓝牙事件中检索数据的功能只是返回了。