Connecting Arduino with Android - Bluetooth

@Kagami hi this example for @noel it’s working I have tested year ago

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

#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++;
}


This is the run

1 Like