# 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:** 4

<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 3, 2021, 3:57pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/62 "2021-01-03T15:57:42Z")

</div>

No, the second method is to avoid (but even then not always) confusion about the name.  
Using the module name has never failed for me.

---

<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 3, 2021, 4:00pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/63 "2021-01-03T16:00:37Z")

</div>

my module name BT04-A and works with sliders sketch

---

<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 3, 2021, 4:02pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/64 "2021-01-03T16:02:07Z")

</div>

my module name BT04-A and works with sliders sketch even with terminal worked both send receive

---

<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 3, 2021, 4:05pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/65 "2021-01-03T16:05:54Z")

</div>

> [@jafal](#):
>
> BT04-A

So using this name as the device\_name works with the code above?

---

<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 3, 2021, 4:13pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/66 "2021-01-03T16:13:20Z")

</div>

> [@noel](#):
>
> So using this name as the device\_name works in the code above?

i did not try your last code yet but it works with other sketch you gave me for the sliders

but sure i change the name of module from HC-05 to BT04-A inside the sketch

---

<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 3, 2021, 4:18pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/67 "2021-01-03T16:18:37Z")

</div>

> [@noel](#):
>
> ```auto
> import ketai.net.bluetooth.*;
> import android.os.Bundle;
> 
> String device_name = " BT04-A ";
> String Str; 
> 
> KetaiBluetooth bt;
> 
> void setup() { 
> fullScreen();
> orientation(PORTRAIT);
> 
> ```

i well try this and back to you with the result

and great thanks for you

---

<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 3, 2021, 4:20pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/68 "2021-01-03T16:20:52Z")

</div>

Don’t put spaces between the quotations!

---

<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 3, 2021, 4:22pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/69 "2021-01-03T16:22:46Z")

</div>

> [@jafal](#):
>
> but it works with other sketch you gave me for the sliders

Which one? I’ve posted so many.

---

<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 3, 2021, 4:26pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/70 "2021-01-03T16:26:25Z")

</div>

this works and control arduino

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

ControlP5 cP5a;
ControlP5 cP5b;

String device_name = "BT04-A";
//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:** [January 3, 2021, 4:38pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/71 "2021-01-03T16:38:29Z")

</div>

> [@jafal](#):
>
> this works and control arduino

Really?  
The integer val1 is converted to a string how?  
` send("b" + val1 +"\n");`

```auto
void send(String str) {
  byte[] data = str.getBytes();
  bt.writeToDeviceName(device_name, data);
}

```

---

<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 3, 2021, 5:18pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/72 "2021-01-03T17:18:28Z")

</div>

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;
     
      {
    
   
     
     
      break;

    case 'b': value2 = Serial.parseInt();
     TX_PULSE = value2;
       
   break;
      
  
      

   
     }
   

```

 ![Screenshot_2021-01-03-21-13-28-898](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3570647d885338c0a1105fbc6385312249080ebe.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:** [January 3, 2021, 6:23pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/73 "2021-01-03T18:23:48Z")

</div>

hi

this sketch keeps stop

if i remove this lines it compile with blue screen but without action

```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();
  textSize(60); /// this line
  textAlign(CENTER); //// this line
}

void draw() {
  background(0, 0, 200);
  text(Str, width/2, height/2);/// this line
}

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:** ![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 3, 2021, 6:26pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/74 "2021-01-03T18:26:17Z")

</div>

But did it println(); a string to the console?

---

<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 3, 2021, 6:32pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/75 "2021-01-03T18:32:08Z")

</div>

i just link the bluetooth module with out data still prepare the arduino

---

<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 3, 2021, 6:35pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/76 "2021-01-03T18:35:44Z")

</div>

I don’t understand this.

> [@jafal](#):
>
> i just link the bluetooth module with out data still prepare the arduino

---

<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 3, 2021, 6:44pm UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/77 "2021-01-03T18:44:14Z")

</div>

i am not sure if it println(); a string to the console i well try again and see

---

<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, 9:54am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/78 "2021-01-04T09:54:50Z")

</div>

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

hi

it is work

 ![Screenshot_2021-01-04-13-19-17-016](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4229227ff60f5f1dabd6e0ffbfe9dc9a768d9e3c.jpeg)

 ![Screenshot_2021-01-04-13-20-40-193](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/45eb99f81722583e028dd224e17adcb68329c58a.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 4, 2021, 9:58am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/79 "2021-01-04T09:58:54Z")

</div>

> [@jafal](#):
>
> it is working

Nice!..

---

<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, 10:07am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/80 "2021-01-04T10:07:15Z")

</div>

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

i need a few help more if you kind

---

<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, 10:30am UTC](https://discourse.processing.org/t/android-bluetooth-to-arduino-and-vice-versa/26578/81 "2021-01-04T10:30:23Z")

</div>

this is simple arduino code reading from potentiometer

```auto
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter to stabilize after the last
  // reading:
  delay(2);
}

```

and this is the processing sketch its working with serial i need to send it with bluetooth how to do it ??

```auto

 import processing.serial.*;

  Serial myPort; // The serial port
  int xPos = 1; // horizontal position of the graph
  float inByte = 0;

  void setup () {
    // set the window size:
    size(400, 300);

    // List all the available serial ports
    // if using Processing 2.1 or later, use Serial.printArray()
    println(Serial.list());

    // I know that the first port in the serial list on my Mac is always my
    // Arduino, so I open Serial.list()[0].
    // Open whatever port is the one you're using.
    myPort = new Serial(this, Serial.list()[0], 9600);

    // don't generate a serialEvent() unless you get a newline character:
    myPort.bufferUntil('\n');

    // set initial background:
    background(0);
  }

  void draw () {
    // draw the line:
    stroke(127, 34, 255);
    line(xPos, height, xPos, height - inByte);

    // at the edge of the screen, go back to the beginning:
    if (xPos >= width) {
      xPos = 0;
      background(0);
    } else {
      // increment the horizontal position:
      xPos++;
    }
  }

  void serialEvent (Serial myPort) {
    // get the ASCII string:
    String inString = myPort.readStringUntil('\n');

    if (inString != null) {
      // trim off any whitespace:
      inString = trim(inString);
      // convert to an int and map to the screen height:
      inByte = float(inString);
      println(inByte);
      inByte = map(inByte, 0, 1023, 0, height);
    }
  }

```

i try many times but could not make it

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

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