# Android Bluetooth to Arduino and vice versa

**URL:** <https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578>\
**Category:** Processing for Android\
**Created:** [December 29, 2020, 8:52am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578 "2020-12-29T08:52:21Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 10:56am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/82 "2021-01-04T10:56:04Z")

</div>

You are using “Serial.println(analogRead(A0));”  
This will include a ‘\n’ to the bytes you read from the analog port, which will be used as a delimiter in the processing’s sketch where you use “myPort.bufferUntil(’\n’);”  
The Ketai library is different from the Serial library and has no such method. You have to implement a Ring buffer(type FIFO) yourself, to avoid losing data after the delimiter. But for simple sketches where no high speed is required such as playing music, you can give a higher delay() in your Arduino sketch and use write() even without a delimiter, or use one and trim it. As you already have your own Terminal sketch, just try empirically what the best delay time is in your Arduino sketch. If you use a delimiter it doesn’t necessarily need to be ‘\n’ it can be any character.

---

<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:** [January 4, 2021, 11:51am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/83 "2021-01-04T11:51:02Z")

</div>

> [@noel](#):
>
> The Ketai library is different from the Serial library and has no such method. You have to implement a Ring buffer(type FIFO) yourself, to avoid losing data after the delimiter. But for simple sketches where no high speed is required such as playing music, you can give a higher delay() in your Arduino sketch

form arduino i can send any thing but i need working processing sketch

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 12:15pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/84 "2021-01-04T12:15:09Z")

</div>

I don’t understand this. Your Terminal sketch isn’t working?  
Here’s another one. It will change the background from black to white when you turn the potentiometer.  
Use Serial.write(analogRead(A0)); and delay(100); in the Arduino code.  
Btw. Only one solution is excepted in the topic.

```auto
import ketai.net.bluetooth.*; 
import android.os.Bundle; 
String device_name = "BT04-A"; 
String Str; 

KetaiBluetooth bt; 

void setup() { 
  fullScreen(); 
  orientation(PORTRAIT); 
  bt.getPairedDeviceNames(); 
  bt.connectToDeviceByName(device_name); 
  bt.start();
} 

void draw() { 
  background(int(Str));
} 

void onBluetoothDataEvent(String who, byte[] data) { 
  Str = new String(data); 
  println(Str);
} 
void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  bt = new KetaiBluetooth(this);
}

```

---

<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:** [January 4, 2021, 12:18pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/85 "2021-01-04T12:18:08Z")

</div>

Hello,

If you have not already set up serial over Bluetooth:

> [@Communication over laptop's Bluetooth](https://discourse.processing.org/t/communication-over-laptops-bluetooth/14056):
>
> How can I send & receive data through the laptop’s bluetooth? For example, have an android sketch communicate with a java sketch (running on the laptop) over bluetooth? It’s fairly straightforward how to do the android side (with Ketai library), but how can I implement the PC side? Note: I’m interested in using the laptop’s embedded Bluetooth. Not in any solutions involving 3rd party modules (i.e. arduino + Bluetooth module over Serial).

`:)`

---

<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:** [January 4, 2021, 12:22pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/86 "2021-01-04T12:22:17Z")

</div>

hi sir

i did and i make great progress but i need to learn now how to send from arduino to processing sketch

thank you very much

---

<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:** [January 4, 2021, 1:55pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/87 "2021-01-04T13:55:33Z")

</div>

hi

the sketch keep stopping unless i remove this line works with just white screen receiving data from arduino

```auto
} 

void draw() { 
 // background(int(Str)); // work when mark this line
} 

void onBluetoothDataEvent(String who, byte[] data) { 
  Str = new String(data); 
  println(Str);
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 2:15pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/88 "2021-01-04T14:15:36Z")

</div>

Try this instead.

void onBluetoothDataEvent(String who, byte[] data) {  
Str = new String(data);  
background(int(Str));  
}  
And leave 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:** [January 4, 2021, 2:37pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/89 "2021-01-04T14:37:19Z")

</div>

its working but what if i want to use it inside void draw how to ?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 2:48pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/90 "2021-01-04T14:48:05Z")

</div>

I am sorry about the confusion because I totally forgot that the data string for some reason is not captured within draw(); although it is declared as global. All my Bluetooth sketches work this way. I use a function called void plot() that is called from within the onBluetoothDataEvent(), wherein I draw everything related to the data string Str. All other things not related to the string can be drawn normally within the draw() function.  
Try to draw the text as well, you will see that all will be drawn if it were in draw(), because void onBluetoothDataEvent() is also a (time) driven function.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 3:11pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/91 "2021-01-04T15:11:35Z")

</div>

I was thinking about this, and I am writing code without testing, but could you try the following?  
Declare a string as global like:

`String bt_string;`

Then:

```auto
void onBluetoothDataEvent(String who, byte[] data) {
  String Str = new String(data);
  bt_string = Str;
}

```

And in draw():  
background(int(bt\_string));

---

<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:** [January 4, 2021, 3:48pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/92 "2021-01-04T15:48:48Z")

</div>

same result

```auto
void onBluetoothDataEvent(String who, byte[] data) {
  String Str = new String(data);
  bt_string = Str;
}
And in draw():
background(int(bt_string));

did not work

```

work like this

}

void draw() {  
// background(int(bt\_string)); // work when mark this line  
} ```  
void onBluetoothDataEvent(String who, byte[] data) {  
String Str = new String(data);  
bt\_string = Str;  
background(int(bt\_string));

}

```auto

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 3:53pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/93 "2021-01-04T15:53:16Z")

</div>

Yes, I must have tested this long ago when I started. Thus the way to go is the one I described above. It works well in my sketches.

---

<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:** [January 4, 2021, 4:03pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/94 "2021-01-04T16:03:33Z")

</div>

so is this is (int(Str)); which we can use it but in

a function called void plot()

```auto
void onBluetoothDataEvent(String who, byte[] data) {
Str = new String(data);
background(int(Str));
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 4:17pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/95 "2021-01-04T16:17:08Z")

</div>

Yes, but not necessarily. You can draw everything in the void onBluetoothDataEvent() function, but my code is quite large there, because of the ring buffer I placed there. My sketches run using various channels with a baud rate velocity over 115,200, without any data loss, using the Ketai lib. I use a separated function to plot the data only because of oversight reasons.

---

<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:** [January 4, 2021, 5:08pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/96 "2021-01-04T17:08:32Z")

</div>

so how to draw rect and change high or width or position etc upon this (int(Str)); ?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 4, 2021, 5:13pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/97 "2021-01-04T17:13:46Z")

</div>

Just use the normal functions within the bluetooth event function like for example:

```auto
void onBluetoothDataEvent(String who, byte[] data) {
  Str = new String(data);
  background(255);
  rect(0, 0, int(Str), int(Str));
}

```

---

<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:** [January 5, 2021, 6:50pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/98 "2021-01-05T18:50:25Z")

</div>

[noel](https://discourse.processing.org/u/noel)

hi i made simple Bluetooth oscilloscope

 ![Screenshot_2021-01-05-22-39-38-346](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7d9844b5331e30e199e54b9ae54e17aea709f752.jpeg)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 5, 2021, 8:32pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/99 "2021-01-05T20:32:36Z")

</div>

Nice! What frequency can you reach? If necessary I can make some code for better string handling. I can’t give you the ring buffer code, because it’s part of a commercial app I made.  
But just pure string handling also can do a good job.

---

<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:** [January 5, 2021, 8:40pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/100 "2021-01-05T20:40:31Z")

</div>

> [@noel](#):
>
> I can make some code for better string handling. I  
> But just pure string handling also can do a good job.

that is what i need if you kind[quote="noel,

> [@noel](#):
>
> Nice! What frequency can you reach?

---

<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:** [January 5, 2021, 8:44pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/101 "2021-01-05T20:44:16Z")

</div>

> [@noel](#):
>
> Nice! What frequency can you reach?

using arduino with out divider or any external components reach about 1 mhz

[Previous page](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578.md?page=4)

[Next page](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578.md?page=6)
