# Connecting Arduino with Android - Bluetooth

**URL:** https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605
**Category:** Processing for Android
**Created:** [October 4, 2021, 11:30am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605 "2021-10-04T11:30:44Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Kagami](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Kagami](https://discourse.processing.org/u/Kagami)
#### Post date: [October 4, 2021, 11:30am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/1 "2021-10-04T11:30:44Z")

</div>

Hi,  
Im trying to run this code ([/dev/jarzebski: Przetwarzanie i wizualizacja danych](http://www.jarzebski.pl/arduino/wstep/przetwarzanie-i-wizualizacja-danych.html)) 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?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [October 4, 2021, 12:44pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/2 "2021-10-04T12:44:11Z")

</div>

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

> **[GitHub -...](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android)**
>
> Android code for Processing for Android. Contribute to EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android development by creating an account on 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.

---

<div class="post-metadata">

### Author: ![Kagami](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Kagami](https://discourse.processing.org/u/Kagami)
#### Post date: [October 4, 2021, 1:48pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/3 "2021-10-04T13:48:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [October 4, 2021, 1:57pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/4 "2021-10-04T13:57:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 4, 2021, 4:21pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/5 "2021-10-04T16:21:16Z")

</div>

> [@Kagami](#):
>
> 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

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

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 4, 2021, 4:24pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/6 "2021-10-04T16:24:32Z")

</div>

> [@Kagami](#):
>
> My biggest concern is - is processing.serial even working with android?

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

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 4, 2021, 5:16pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/7 "2021-10-04T17:16:45Z")

</div>

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

[https://github.com/vsquared/Processing\_Android/tree/main/Bluetooth\_android\_rev1](https://github.com/vsquared/Processing_Android/tree/main/Bluetooth_android_rev1)

---

<div class="post-metadata">

### Author: ![Kagami](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Kagami](https://discourse.processing.org/u/Kagami)
#### Post date: [October 4, 2021, 7:26pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/8 "2021-10-04T19:26:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [October 7, 2021, 4:48am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/9 "2021-10-07T04:48:13Z")

</div>

@jafal @svan  
any input?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 7, 2021, 6:46am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/10 "2021-10-07T06:46:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 7, 2021, 10:34am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/11 "2021-10-07T10:34:12Z")

</div>

@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

```auto
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

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 7, 2021, 10:41am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/12 "2021-10-07T10:41:59Z")

</div>

Here is example

> [@APDE ketai example](https://discourse.processing.org/t/apde-ketai-example/27133):
>
> Hi this code receive data from terminal but not sending what I missed ? import android.content.Intent; import android.os.Bundle; import ketai.net.bluetooth.\*; import ketai.ui.\*; import ketai.net.\*; PFont fontMy; boolean bReleased = true; //no permament sending when finger is tap KetaiBluetooth bt; boolean isConfiguring = true; String info = ""; KetaiList klist; ArrayList devicesDiscovered = new ArrayList(); //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* // The followin…

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 7, 2021, 11:49am UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/13 "2021-10-07T11:49:02Z")

</div>

Hello,

This topic may be of interest:

> [@Communication over laptop's Bluetooth](https://discourse.processing.org/t/communication-over-laptops-bluetooth/14056/4):
>
> Hello, You can use the [Bluetooth SPP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Serial_Port_Profile_(SPP)). I have done this with an app on my phone such as: [Bluetooth RGB](https://play.google.com/store/apps/details?id=nextprototypes.bluetoothtRGB&hl=en) And also with an Arduino + HC05, an app written with [Ketai library](http://ketai.org/) for an Android device (phone and tablet) and across two PCs. On the W10 PC side find “More Bluetooth Options” and set up a COM port: You can then use the Processing serial library to communicate over the COM port: Processing does not know it is serial over Bluetooth, serial over USB or otherwise… it is just se…

`:)`

---

<div class="post-metadata">

### Author: ![Kagami](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Kagami](https://discourse.processing.org/u/Kagami)
#### Post date: [October 7, 2021, 1:09pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/14 "2021-10-07T13:09:28Z")

</div>

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];
  }}
```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 7, 2021, 1:57pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/15 "2021-10-07T13:57:18Z")

</div>

@Kagami hi try this

draw everything in the void onBluetoothDataEvent() function

Leave void draw empty

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 7, 2021, 2:35pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/16 "2021-10-07T14:35:10Z")

</div>

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

```auto
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

```auto
#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

 ![8575e368cc3b4544a371cda4e34fc24f10aa684c_2_1035x646](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/befe61d994703ae9ca8e3327cda80bb2ba1df365.jpeg)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 7, 2021, 2:39pm UTC](https://discourse.processing.org/t/connecting-arduino-with-android-bluetooth/32605/17 "2021-10-07T14:39:48Z")

</div>

@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”;
