# 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:** 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:** [December 29, 2020, 8:52am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/1 "2020-12-29T08:52:21Z")

</div>

hi all

i made 2 sliders sending values to arduino

i want to use same in android mode and control arduino via bluetooth using ‘ketai’ library and i need to know how

```auto
import controlP5.*;
import processing.serial.*;
 
ControlP5 cP5a;
ControlP5 cP5b;
 
Serial arduino;
 
void setup() {
  size(350,120);
 printArray(Serial.list());
 
  arduino = new Serial(this, Serial.list()[0], 9600);
 
  cP5a = new ControlP5(this);
  cP5a.addSlider("CYCLE_TIME", 999, 1999,999, 10, 10, 255, 35);
  cP5b = new ControlP5(this);
  cP5b.addSlider("PULSE", 55, 455, 55, 10, 55, 255, 35);
}
 
void draw() {
  background(0);
 
  }
 
void controlEvent(ControlEvent theEvent) {
  if(theEvent.isController()) {
 
 print("control event from : "+theEvent.getController().getName());
 println(", value : "+theEvent.getController().getValue());
 
 if(theEvent.getController().getName()=="CYCLE_TIME") {
   int val1 = int(theEvent.getController().getValue());
   arduino.write("a" + val1+"\n");
 
 }
 
 if(theEvent.getController().getName()=="PULSE") {
   int val2 = int(theEvent.getController().getValue());
   arduino.write("b" + val2+"\n" );
   }
  }
}

```

---

<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:** [December 29, 2020, 9:16am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/2 "2020-12-29T09:16:17Z")

</div>

Hi, @jafal See the first file [here](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android) for an example. The setup wiring image you can find in the README.

---

<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:** [December 29, 2020, 9:35am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/3 "2020-12-29T09:35:14Z")

</div>

hi  
thanks for you a lot i know how to wir tx with rx i just need to add my code via andoid with bt

thanks

---

<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:** [December 29, 2020, 9:37am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/4 "2020-12-29T09:37:50Z")

</div>

It’s in [this](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/Android%20Arduino%20Bluetooth%20example) file.

---

<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:** [December 29, 2020, 9:43am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/5 "2020-12-29T09:43:51Z")

</div>

thanks again sir

its very long code

i am just learning if you please explain to me what add to my code

---

<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:** [December 29, 2020, 9:53am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/6 "2020-12-29T09:53:10Z")

</div>

Extracted code necessary for you would be the below.  
It’s not clear to me how you are sending the string, How is it split in your Arduino code to receive two separate values for each slider?

```auto
import ketai.net.bluetooth.*;

String device_name = "HC-05";
//String device_mac = "40:45:DA:00:25:05";

KetaiBluetooth bt;

void setup() {
  bt.getPairedDeviceNames();
  bt.start();
  //bt.connectDevice("40:45:DA:00:25:05");
  bt.connectToDeviceByName(device_name);
}

void send() {
  String str = "<"+str(pgr_r)+",0,"+str(button_toggle_value)+">";
  byte[] data = str.getBytes();
  //bt.write(device_mac, data);
  bt.writeToDeviceName(device_name, data);
  //OscMessage m = new OscMessage(str);
  //bt.broadcast(m.getBytes());
}

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

```

---

<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:** [December 29, 2020, 10:12am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/7 "2020-12-29T10:12:21Z")

</div>

hi

here is my result using serial and arduino

the sliders controls my signal as i want

 ![jafar](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f0bf349a7744003aa677c69a2705cee5bf4b4238.jpeg) controls my signal as i want

---

<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:** [December 29, 2020, 10:35am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/8 "2020-12-29T10:35:19Z")

</div>

hi

where to use mine in you code ???`

> [@jafal](#):
>
> ```auto
> if(theEvent.getController().getName()=="CYCLE_TIME") {
> int val1 = int(theEvent.getController().getValue());
> arduino.write("a" + val1+"\n");
>  
> }
>  
> if(theEvent.getController().getName()=="PULSE") {
> int val2 = int(theEvent.getController().getValue());
> arduino.write("b" + val2+"\n" );
> }
> }
> 
> ```

```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:** [December 29, 2020, 10:42am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/9 "2020-12-29T10:42:28Z")

</div>

Nice looking program.  
Well you didn’t tell me how you split the string on the Arduino site, but if it’s working I guess it would be like:

```auto
import ketai.net.bluetooth.*;
import controlP5.*;
import android.os.Bundle;

ControlP5 cP5a;
ControlP5 cP5b;

String device_name = "HC-05";
//String device_mac = "40:45:DA:00:25:05";

KetaiBluetooth bt;

void setup() { 
  cP5a = new ControlP5(this);
  cP5a.addSlider("CYCLE_TIME", 999, 1999,999, 10, 10, 255, 35);
  cP5b = new ControlP5(this);
  cP5b.addSlider("PULSE", 55, 455, 55, 10, 55, 255, 35);
  bt.getPairedDeviceNames();
  bt.start();
  //bt.connectDevice("40:45:DA:00:25:05");
  bt.connectToDeviceByName(device_name);
}

void draw() {
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isController()) {
    print("control event from : "+theEvent.getController().getName());
    println(", value : "+theEvent.getController().getValue());
    if (theEvent.getController().getName()=="CYCLE_TIME") {
      int val1 = int(theEvent.getController().getValue());
      send("a" + val1+"\n");
    }
    if (theEvent.getController().getName()=="PULSE") {
      int val2 = int(theEvent.getController().getValue());
      send("b" + val2+"\n");
    }
  }
}

void send(String str) {
  byte[] data = str.getBytes();
  //bt.write(device_mac, data);
  bt.writeToDeviceName(device_name, data);
  //OscMessage m = new OscMessage(str);
  //bt.broadcast(m.getBytes());
}

void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

```

---

<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:** [December 29, 2020, 10:47am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/10 "2020-12-29T10:47:24Z")

</div>

But you have an integer in the string, so you’ll have to adapt that part using str(int), and convert it in the Arduino code again.

---

<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:** [December 29, 2020, 10:56am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/11 "2020-12-29T10:56:35Z")

</div>

thanks for you

its easy for me control arduino its work with sliders very fine i well try your code

this is the arduino code

```auto
  void bob() {  
  
 
  
     if (Serial.available() >= 2)
 
  {
    int value2;
    char command = Serial.read();
    switch (command)
    {
    case 'a': value2 = Serial.parseInt();
      CYCLE_TIME = value2;
     
      {
      if(CYCLE_TIME > CYCLE_TIME_MAX)CYCLE_TIME = CYCLE_TIME_MAX;
       }
     
      {
      if(CYCLE_TIME < CYCLE_TIME_MIN) CYCLE_TIME = CYCLE_TIME_MIN;
      }
     
      
      break;

    case 'b': value2 = Serial.parseInt();
     TX_PULSE = value2;
       
      {
       if(TX_PULSE < TX_PULSE_MIN) TX_PULSE = TX_PULSE_MIN;
       }
      
      {
      if(TX_PULSE > TX_PULSE_MAX) TX_PULSE = TX_PULSE_MAX;
      }
      break;

   
     }
    }
  }

```

---

<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:** [December 29, 2020, 11:14am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/12 "2020-12-29T11:14:03Z")

</div>

Don’t forget to give all Bluetooth permissions.

---

<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:** [December 29, 2020, 11:16am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/13 "2020-12-29T11:16:31Z")

</div>

Don’t forget to give all Bluetooth permissions.

what do you mean ?

---

<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:** [December 29, 2020, 11:20am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/14 "2020-12-29T11:20:58Z")

</div>

In the Processing’s IDE Android mode Menu/Android/Sketch permissions. Or are you using 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:** [December 29, 2020, 11:27am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/15 "2020-12-29T11:27:57Z")

</div>

> [@jafal](#):
>
> Bluetooth permissions.

i am using Processing IDE on pc but my sketch i use java mode until now i enable the android mode but did not use it yet

---

<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:** [December 29, 2020, 11:31am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/16 "2020-12-29T11:31:12Z")

</div>

If you want to test quickly, download the app APDE on the Play Store. It’s a tool that runs Processing for Android on the device itself, and compiles in seconds.

---

<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:** [December 29, 2020, 11:41am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/17 "2020-12-29T11:41:31Z")

</div>

i have it on my tab i work some sketches but i do not know how to import libraries yet i am new to processing what i am thinking to do is to make a software defined metal detector same as software defined radio i am planing to make a smart metal detector which can react and learn and update itself it is fantasia idea but i make progress

processing is a very great amazing program

---

<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:** [December 29, 2020, 11:51am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/18 "2020-12-29T11:51:33Z")

</div>

> [@jafal](#):
>
> i have it on my tab i work some sketches but i do not now how to import libraries yet

The easiests way is to copy the library existent on your PC to the sketchbook libraries folder. If the libraries folder doesn’t exist, create one. [Here is a link](https://drive.google.com/file/d/19GXV5Rn20IFKTAC3Fmdv8Zuw898jYFV3/view?usp=sharing) of a working Ketai lib. Just unzip it into the libraries folder.

---

<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:** [December 29, 2020, 11:55am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/19 "2020-12-29T11:55:01Z")

</div>

i mean import the libraries to app APDE i do not know 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:** [December 29, 2020, 11:58am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/20 "2020-12-29T11:58:39Z")

</div>

Exactly how I said. on your tablet download the link given above. Make a folder libraries in the Sketchbook folder, and unzip the ketai folder into it. That’s all

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