Android Bluetooth to Arduino and vice versa

Today I have spent some time trying to plot some lines to the canvas like I’ve done before with the Ketai lib, but I found out that I can’t plot any data on my Android 10 phone. It connects and I can read the data in the console with the println() function but that’s it. So I installed the APDE version v0.4.1 Alpha which I used the last time for plotting and installed it on my Lollipop phone, Which worked again. But for me working with a library that can’ even transmit data to the draw() loop makes no sense, so I will try to write my own library.
I did write some code to handle the incoming string, and it works so that you can use a delay with just a few milliseconds in your Arduino code. It is as it is, for now, and needs some improvement, but the actual library is my priority now. If you test something please let me know, on what device with what versions of the software. I also saw your new topic with the graphic library. Did that work with plotting lines?

  • processing code
import ketai.net.bluetooth.*;
import android.os.Bundle;

String device_name = "HC-05";
String Str, opStr, pStr; 
int xPos, yPos, oldxPos, oldyPos, x, tyPos, m;

KetaiBluetooth bt;

void setup() { 
  fullScreen();
  orientation(LANDSCAPE);
  background(255);
  //strokeWeight(3);
  fill(0);
  stroke(0);
  bt.getPairedDeviceNames();
  bt.connectToDeviceByName(device_name);
  bt.start();
  m = height/2-128;
}

void draw() {
}

void onBluetoothDataEvent(String who, byte[] data) {
  if (data != null) {
    String Str = new String(data);
    //println(Str);

    if (Str.indexOf('!') == -1) {
      pStr = pStr + Str;
      Str = "";
    }

    if (Str == "!") {
      yPos = Integer.valueOf(pStr);
      line(xPos, oldyPos, xPos, yPos);
      oldyPos = yPos;
      xPos += 2;
      pStr = "";
      Str = "";
    }   

    if (Str.startsWith("!")) {
      yPos = Integer.valueOf(pStr);
      line(xPos, oldyPos, xPos, yPos);
      Str = Str.substring(1);
      oldyPos = yPos;
      xPos += 2;
      pStr = "";
    }

    if (Str.endsWith("!")) {
      Str = Str.substring(0, Str.length()-1); 
      if (Str.indexOf('!') != -1) { 
        Str = pStr + Str;
        for (String ts : Str.split("!")) {
          yPos = Integer.valueOf(ts);
          line(xPos, oldyPos, xPos, yPos);
          oldyPos = yPos;
          xPos += 2;
        }
        pStr = ""; 
        Str = "";
      } else {
        yPos = Integer.valueOf(pStr + Str);
        line(xPos, oldyPos, xPos, yPos);
        xPos += 2;
        pStr = "";
      }
    }

    if (Str.indexOf('!') != -1) { 
      opStr = pStr;
      pStr = Str.substring(Str.lastIndexOf('!')+1);
      Str = opStr + Str.substring(0, Str.lastIndexOf("!"));
      for (String ts : Str.split("!")) {
        yPos = Integer.valueOf(ts);
        line(xPos, oldyPos, xPos, yPos);
        oldyPos = yPos;
        xPos += 2;
      }
    }

    if (xPos >= width) {
      xPos = 0;
      background(255);
    }
    //   line(xPos, oldyPos, xPos, yPos);   
    //   point(xPos, yPos);
    //   println(xPos+" "+yPos);
  }
}


void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}
  • Arduino code I used for testing
#include <SoftwareSerial.h>
#include <Wire.h>

SoftwareSerial mySerial(11, 12); // RX, TX

int16_t counter = 0;
boolean toggle;

void setup() {
  mySerial.begin(9600); // Set the baudrate equal to HC06 setting
} 

void loop() {
  mySerial.print(String(counter)+'!');
  delay(5);
  if (counter > 255) toggle = true;
  if (counter < 0) toggle = false;
  if (toggle) counter--;
  else counter++;
}
1 Like