Android Bluetooth to Arduino and vice versa

thanks for your kind and co-operate i well flow your instructions if i stick i am going to ask you again if you do not mind good man

noel

hi sir

i did import libelers to APDE ketai and controlP5 every time while compiling say app stop i made permission to bt

even no example from controlP5 works i made dx dexer i see library in APDE open example compile it never run i change the name of example same result just few examples from ketai works witch with out bluetooth

i have 2 issues first controlP5 library not working on APDE the othe one no library example run `

this code from Androidmode from pc work on the tablet when iam used it as emulator

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" );
   }
  }
}

i got the sliders on my tablet

I’ve tried the controlP5 library before, using directly the Processings IDE on the PC with a USB connection and it did partially work. On APDE it doesn’t, but I can try to fix that. I haven’t done this before because personally, I prefer to use native android widgets because they are totally customizable, and always work. I have an easy ready code for that, but the topic was re(?)tagged as homework, so I can’t post it. If you didn’t start this topic as homework but instead it’s a personal project on which you want some help to transport it to Android you need to declare that properly and undo it. (untag) As for the Ketai library probably you didn’t give the Bluetooth permissions. On the APDE you can do that by choosing: Three dots / Sketch Properties / Sketch Permissions.

1 Like

its my first share and really i dont know what is the point from the question i am brand-new for the forum

anyway how to untag ??

i did give permission to ketai for bluetooth

When you compile it on the PC did the Ketai Bluetooth also give some error? Don’t worry I’m quite certain that the Ketai Bluetooth is going to work. As for the controlP5 lib on APDE; I am working on it, but it still has a structural problem with the Classes.

1 Like

thanks for you when i run from pc and using the tablet as emulator first time its stopped i back to the sketch and disable the ketai stuff emulate it again tablet run the sliders just with no issue

I don’t quite understand what you are trying to say. Is it that the sketch works with the sliders, but when you use the Ketai Bluetooth, the sketch crashes. What is the error given?

1 Like

yes sir true
it is that the sketch works with the sliders, but when i use the Ketai Bluetooth the sketch crashes

says when crush sketch has stopped
open app again

I’m sorry. Still don’t get it. Can you post a screenshot of the error. If you feel more comfortable with it you can post in your native language. (Arabic?)

1 Like

When this happens an error will appear in the console. (Sometimes only after a second re-run of the build app)

this on APDE

Yes but this error is caused by the controlP5 library. Let’s test first the more important Ketai Bluetooth library. Below is a code that will broadcast data to any device that’s paired with it. You need however to install the oscP5 lib as well. I managed to restructure/adapt it for APDE. You can download it on this link. Just unzip it in the the Sketchbook/libraries folder. If you are going to work more with Android you will certainly need it, so it´s good to test it as well. You’ll need another Android device to read the data. On that device, you download from the Play Store an app called “Serial Bluetooth Terminal”. In the settings of that app, you set DisplayMode Terminal. Before connecting to the paired sender phone with the two opposite heart icons, you run the sketch I posted below to start sending.

import ketai.net.bluetooth.*; 
import android.os.Bundle; 
import oscP5.*; 
import netP5.*; 

OscMessage m; 
KetaiBluetooth bt; 

String str = "myData"; 
int start_time; 

void setup() { 
  bt.start(); 
  m = new OscMessage(str);
} 

void draw() { 
  if (millis() - start_time > 300) { 
    bt.broadcast(m.getBytes()); 
    start_time = millis();
  }
} 

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

many thanks for you give me sometime to test this and i well inform you what happens

thanks a lot

So I managed to get the controlP5 library working on APDE. You can download the library folder at this link. Below an example code, to set the textSize and font. I used this font. Also, since you can’t open a second window, tap on the upper part of the screen to toggle the visibility of the sliders. Remember that in Android you have to set everything relative to width and height. The text is placed differently on each different font/ text size, and I couldn’t get a solution for that. For that reason, I prefer to work with native Android widgets. But that’s your choice.

import controlP5.*;

PFont font;
float sliderValue;
boolean toggle;

ControlP5 cp5a, cp5b;

void setup() {
  size(900, 550);
  noStroke();
  font = createFont("Crispy.ttf", 60);
  cp5a = new ControlP5(this);
  cp5a.addSlider("slider")
    .setPosition(width/3, height-height/5)
    .setSize(3*width/5, height/10)
    .setRange(0, 255)
    .setValue(128)
    .setLabel("") 
    .setFont(font); 
  cp5b = new ControlP5(this);
  cp5b.addSlider("slider")
    .setPosition(width/3, height-4*height/9)
    .setSize(3*width/5, height/10)
    .setRange(0, 255)
    .setValue(60)
    .setLabel("") 
    .setFont(font);
}

void draw() {
  background(50, 150, 255);
}

void slider(float slider_value) {
  sliderValue = slider_value;
}

void mousePressed() { 
  if ( mouseY < height/2) {
    if (toggle) {
      cp5a.setVisible(true);
      cp5b.setVisible(true);
    } else {
      cp5a.setVisible(false);
      cp5b.setVisible(false);
    }
    toggle =! toggle;
  }
}
1 Like

thank you very much

what you made more than great

when i finish formatting my pc and tablet i well test your instructions and see what i can do

happy new year for you

1 Like

noel
hi sir

i have try it and works fine and thanks for you

what if i want to read from arduino to processing using ketai instead of serial

how to use this code with blutooth ?

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 inital 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);
  }
}

thanks in advance

To receive data from the Arduino you use this function in your code.
Now, this data array can vary much in length, depending on the speed the Arduino is sending. In part caused by Bluetooth’s channel frequency-hopping, it comes in chunks from a few bytes to hundreds of bytes at once. So you have to separate the incoming String Str accordingly with the String Class methods.

void onBluetoothDataEvent(String who, byte[] data) {
  String Str = new String(data); 
 // Handle your String
}
1 Like

hi

where put this code `

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

in this sketch ?

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 inital 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);
  }
}

Swap this function

void serialEvent (Serial myPort) {
  String inString =
  // Handle inString
}

by this function

void onBluetoothDataEvent(String who, byte[] data) {
  String Str = new String(data); 
 // Handle your String
}
1 Like