Connecting Arduino with Android - Bluetooth

Hi,
Im trying to run this code (/dev/jarzebski: Przetwarzanie i wizualizacja danych) but with gyroscope instead of joystick.
While connecting my arduino with PC via Bluetooth it works, but when i try connecting arduino with phone also via Bluetooth im reciving Error : the import processing.serial cannot be resolved. Im using APDE app.
Is there any easy way to connect them? without rewriting whole code?

have you tried @noel’s resources
here is is github

also please make use of the search function the question has been asked often, and @jafal and @noel have both attempted this succesfully. @noel is no longer active on the forum but has left lots of answers and working code.

i searched that question and found that specific topic, but it didnt gave me enough answer. My biggest concern is - is processing.serial even working with android? or i have to use ketai? its my first time with processing, so i would prefer to keep code as simple as possible.

theres a very lengthy thread with serial apde, you are welcome to read it, the guy who posted did resolve the issue and get it to work, but its honestly a headache trying to make sense of it.

As I recall, @noel recommended using bluetooth as usb was proving difficult.
I personally began to test, and have yet to come up with anything conclusive, I only had a usb cable at the time, and using the examples on the site I could not get it to work.

I’ve since purchased a bluetooth module, however I havent tested it since Ive been busy with other things. I’m tweaking a few things in a library at the moment, hopefully I can take a look later tonight.

1 Like

Hi

The Bluetooth using android has no relation with serial library you can to use ketai library to communicate via Android Bluetooth

You can pass OTG because your android already has Bluetooth

You can use use serial library if you like with android

1 Like

Yes it’s working but not same serial library that using in processing there is other library for Android mode

1 Like

@Kagami - I have successfully used this in the past:

https://github.com/vsquared/Processing_Android/tree/main/Bluetooth_android_rev1

1 Like

ok, i changed my code to Ketai, but now, when im trying to run it on APDE app, nothing is appering and im recivieng “java.lang.NullPointerException: Attempt to invoke virtual method ‘java.util.ArrayList ketai.net.bluetooth.KetaiBluetooth.getPairedDeviceNames()’ on a null object reference”
i have my BT module paired with phone, and put its name in code, and gave app permisions to use BT

@jafal @svan
any input?

1 Like

< any input?
@paulgoux I have no experience with either KetaiBluetooth or APDE.

@paulgoux hi

First he can try application from Google play store ( Bluetooth serial terminal ) there is more than version he can use… And two android devices needed one for the Bluetooth serial terminal application and the other for APDE scketch

The aim of this is to make sure that ketai sketch connected and transmitting/receiving Data and everything works fine

Then this code must adept to suit ketai

void serialEvent(Serial port)
{
  // odczytujemy dane do momentu wystąpienia znaku nowej linii n
  String input = port.readStringUntil(10);
 
  if (input != null)
  {
    // rozbijamy odczyt po przecinku i nowej linii
    int[] vals = int(splitTokens(input, "\n"));
    
    // przypisujemy do zmiennych
    x = vals[0];
    y = vals[1];
    b = vals[2];   
  }
}

This code from @Kagami link

1 Like

Here is example

Edit this code not mine I found it on the net about year ago don’t remember the link

1 Like

Hello,

This topic may be of interest:

:)

2 Likes

i had used other apps to check if my phone is recivieng data, and it shows it, but still, no results from APDE. There is my code with ketai library. Am i missing something? (Edit UGH, i dont know how to format this properly D:)

import ketai.net.bluetooth.*;

String device_name = "BT04-A";
String Str; 
KetaiBluetooth bt; 
int x;   // zmienna osi x (pin A0)
int y;   // zmienna osi y (pin A1)
PFont f; // zmienna czcionki
String portName;

void setup()
{
  fullScreen(); // rozmiar okna
  orientation(PORTRAIT);
  bt.getPairedDeviceNames();
  bt.connectToDeviceByName(device_name);
  bt.start();
  f = createFont("Arial", 16, true); // Arial, 16px, anti-aliasing
  textFont(f, 16); // rozmiar 16px
}

// pętla rysująca
void draw()
{
  fill(0); // ustawiamy kolor wypełnienia na czarny
  clear(); // czyścimy ekran

  fill(255); // ustawiamy kolor wypełnienia na biały

    // rysujemy okrąg o określonych współrzędnych
    ellipse(x, 1024-y, 10, 10);

  // wyświetlamy dane
  text("AnalogX="+(1023-x)+" AnalogY="+(1023-y),10,20);
}

void onBluetoothDataEvent(String who, byte[] data) {
String Str = new String(data); 
if (Str != null)
  {
    // rozbijamy odczyt po przecinku i nowej linii
    int[] vals = int(splitTokens(Str, ","));

    // przypisujemy do zmiennych
    x = vals[0];
    y = vals[1];
  }}

@Kagami hi try this

draw everything in the void onBluetoothDataEvent() function

Leave void draw empty

@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

@Kagami note that in the example above

void draw is empty

And everything in the void onBluetoothDataEvent(String who, byte[] data) {

Edit replace String device_name = “HC-05”; with your module
String device_name = “BT04-A”;