Android Bluetooth native code

hi this code for member was helping what i need how to send 6 controlp5 sliders values to raduino

any one can help???

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.os.Build.VERSION_CODES; 
import processing.core.PConstants;
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Activity activity;
Permission bp, afl;

Thread runThread;
byte[] readBuffer;
int buffer_index;
boolean stop_thread, init = true;
String device_name = "HC-05";
boolean permissions_granted;
String Str = " "; 
String t_str;
float a, increment = 0.04;
int y;

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  bp = new Permission(this, "BLUETOOTH");
  afl = new Permission(this, "ACCESS_FINE_LOCATION");
  textAlign(CENTER);
  textSize(70);
  t_str = "Click to connect.";
  textSize(12); // To create a "text width factor",
  float twf = width/textWidth(t_str);
  int f = 4; // Empericaly found 
  textSize(f*twf); // Making it  proportional for other screen resolutions
}

void draw() {
  if (init) {
    background(150);
    text(t_str, width/2, height/2);
    strokeWeight(10);
    noFill();
    stroke(50);
    rect(0, 0, width, height);
  } else {
    y = int(80 * sin(a));
    a += increment;
    if (a > TWO_PI)  a = 0;
    try {
      sendData();
    }  
    catch (IOException ex) {
    }
  }
}

void mousePressed () {
  if (init) {
    try {
      init = false;
      findBluetooth();
      connect();
      background(80);
      text("Connecting to send.", width/2, height/2);
    } 
    catch (IOException ex) {
      println(ex);
    }
  }
}

void sendData() throws IOException {
  mmOutputStream.write(y);
}

void findBluetooth() {
  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if (mBluetoothAdapter == null) {
    println("No bluetooth adapter available");
  }
  if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    activity.startActivityForResult(enableBluetooth, 0);
  }
  Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  if (pairedDevices.size() > 0) {
    print("Paired devices MAC Adress:");
    printArray(pairedDevices);
    for (BluetoothDevice device : pairedDevices) {
      print("Paired devices by name:");
      println(device.getName());
      if (device.getName().equals(device_name)) {
        mmDevice = device;
        println("Bluetooth device name found");
        break;
      }
    }
  }
}

void connect() throws IOException {
  UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
  mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
  mmSocket.connect();
  mmOutputStream = mmSocket.getOutputStream();
  mmInputStream = mmSocket.getInputStream();
  println("Bluetooth connected");
  final byte delimiter = 33; //This is the ASCII code for a ! character
  stop_thread = false;
  buffer_index = 0;
  readBuffer = new byte[1024];
  runThread = new Thread(new Runnable() {
    public void run() {                
      while (!Thread.currentThread().isInterrupted() && !stop_thread) {
        try {
          int bytesAvailable = mmInputStream.available();                        
          if (bytesAvailable > 0) {
            byte[] packetBytes = new byte[bytesAvailable];
            mmInputStream.read(packetBytes);
            for (int i = 0; i < bytesAvailable; i++) {
              byte b = packetBytes[i];
              if (b == delimiter) {
                byte[] encodedBytes = new byte[buffer_index];
                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                final String data = new String(encodedBytes, "US-ASCII");
                //plot(data);
                buffer_index = 0;
              } else {
                readBuffer[buffer_index++] = b;
              }
            }
          }
        } 
        catch (IOException ex) 
        {
          stop_thread = true;
        }
      }
    }
  }
  );
  runThread.start();
}

void closeBluetooth() throws IOException {
  stop_thread = true;
  mmOutputStream.close();
  mmInputStream.close();
  mmSocket.close();
  println("Bluetooth stopped");
}

class Permission {
  PApplet parent;
  boolean requestedPortraitImage = false;

  Permission(PApplet pParent, String permissionName) {
    parent = pParent;
    parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
  }

  void onPermissionResult(boolean granted) {
    if (granted)  println("User did grant permission.");
    else println("User did NOT grant permission.");
  }
}

void onPause() {
  try {
    closeBluetooth();
  } 
  catch (Exception ex) {
  }
  super.onPause();
}

void onStop () {
  try {
    closeBluetooth();
  } 
  catch (Exception ex) {
  }
  super.onStop();
}

void onRestart() {
  try {
    findBluetooth();
    connect();
  } 
  catch (IOException ex) {
    println(ex);
  }
}

this is a start to solve it

For openers, where are the five sliders in the Processing window and where is the Arduino code?

Hi sir

this is Processing sketch

import controlP5.*;
import processing.serial.*;
 
ControlP5 cP5a;
ControlP5 cP5b;
ControlP5 cP5c;
ControlP5 cP5d;
ControlP5 cP5e;
ControlP5 cP5f; 
Serial arduino;
 
void setup() {
  size(350,660);
 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);
cP5c = new ControlP5(this);
  cP5c.addSlider("dely1", 999, 1999,999, 10, 101, 255, 35);
  cP5d = new ControlP5(this);
  cP5d.addSlider("dely2", 55, 455, 55, 10, 155, 255, 35);

cP5e = new ControlP5(this);
  cP5e.addSlider("de1", 999, 1999,999, 10, 199, 255, 35);
  cP5f = new ControlP5(this);
  cP5f.addSlider("de2", 55, 455, 55, 10, 244, 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" );
   }
  }

 
 

// print("control event from : "+theEvent.getController().getName());
// println(", value : "+theEvent.getController().getValue());
 
 if(theEvent.getController().getName()=="dely1") {
   int val1 = int(theEvent.getController().getValue());
   arduino.write("c" + val1+"\n");

  }


  

 
 if(theEvent.getController().getName()=="dely2") {
 int val2 = int(theEvent.getController().getValue());
 arduino.write("d" + val2+"\n" );
  
 


} 

 if(theEvent.getController().getName()=="de1") {
   int val2 = int(theEvent.getController().getValue());
   arduino.write("e" + val2+"\n" );

 }
if(theEvent.getController().getName()=="de2") {
   int val2 = int(theEvent.getController().getValue());
   arduino.write("f" + val2+"\n" );

}

}

svan

Hi Sir

Arduino code

void bob() {  
  
 
  
     if (Serial.available() >= 1)
 delay (1);
  {
    int value2;
    char command = Serial.read();
    switch (command)
    {
    case 'a': value2 = Serial.parseInt();
      CYCLE_TIME = value2;
     Serial.print ( CYCLE_TIME); 
    
     
      
      break;

    case 'b': value2 = Serial.parseInt();
     TX_PULSE = value2;
     Serial.print ( TX_PULSE); 
      {
       
      break;
case 'c': value2 = Serial.parseInt();
    del = value2;
     Serial.print ( del); 
     
      break;

   
     }
   case 'd': value2 = Serial.parseInt();
    del1 = value2;
     Serial.print ( del1); 
     
      break;
 }
   case 'e': value2 = Serial.parseInt();
    del2 = value2;
     Serial.print ( del2); 
     
      break;
    }
   case 'f': value2 = Serial.parseInt();
    del3 = value2;
     Serial.print ( del3); 
     
      break; 
    
    }
  }

svan

Thanks for your response this is screenshot testing 2 sliders over processing serial

jafal

  1. Can you combine the slider code and the bluetooth code so that we see five sliders in the bluetooth window?
  2. Arduino code usually has a setup() and loop() ; not bob()?

Can you combine the slider code and the bluetooth code so that we see five sliders in the bluetooth window? That what i am asking for help and i don’t know how to do it

. Arduino code usually has a setup() and loop() ; not bob()?

bob()? is a function name

It appears that you are not ready to delve into bluetooth communication. First you should probably learn how to create a window on an Android app.

You should really try the search function on the forum, this is probably the third android Bluetooth post this month, and yes sliders have already been discussed and solutions have already been found.

Just wanted to make sure sure your questions hadn’t been answered in previous posts. I am not familiar with the android Bluetooth. Anyways hopefully you’ll get your solution.

Thank you…

solved here

1 Like