Problem with BluetoothAdapter class in processing 3

edited September 2017 in Android Mode

hi all ,

i'm trying to run the following code , but it says : 1-the class BluetoothAdapter doesn't exist
2-the class Bundle doesn't exist 3-the class Intent doesn't exist

the code is :

import android.content.Intent;
import android.os.Bundle;
import ketai.ui.*;
import android.os.Environment;
import android.bluetooth.BluetoothAdapter;
import android.view.KeyEvent;

//__End of imports__//

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
KetaiBluetooth bt;

KetaiSimpleFace[] faces;
String directory;

KetaiCamera cam;

int posX,posY,i,info;
int pev_posX,pev_posY,pev_sec;
String cninfo = "";
boolean send_flag=true;

//**To start BT when app is launched**// 
void onCreate(Bundle savedInstanceState) {
 bt = new KetaiBluetooth(this);
void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
//__BT launched__//

//**To select bluetooth device if needed**// (not required for our program
void onKetaiListSelection(KetaiList klist)
 String selection = klist.getSelection();
 //dispose of list for now
 klist = null;
//__End of selection__//

//** To get data from blue tooth**/
void onBluetoothDataEvent(String who, byte[] data)
info = (data[0] & 0xFF) ;
//__data received_//

//**To get connection status**//
String getBluetoothInformation()
  String btInfo = "Connected to :";

  ArrayList<String> devices = bt.getConnectedDeviceNames();
  for (String device: devices)
    btInfo+= device+"\n";

  return btInfo;
//--connection status received_//

void settings()

void setup() {
  bt.start(); //start listening for BT connections
  bt.connectToDeviceByName("HC-05"); //Connect to our HC-06 bluetooth module
  stroke(0, 255, 0);
  cam = new KetaiCamera(this, 320, 240, 24);
  directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath());



void draw() {

 if (cam != null)
       if (!cam.isStarted())

       image(cam, width/2, height/2, width, height); //display CAM on phone screen 

       faces = KetaiFaceDetector.findFaces(cam, 20);    //detect faces

       if(faces.length!=0) //if face found 
       println("Faces found: " + faces.length); //notify the face detection on console 
        pev_posX=posX; posX = int(map(faces[0].location.x,20,300,1,100));
        pev_posY=posY; posY = int(map(faces[0].location.y,20,200,101,201));
        if(posX<=100 && posY>=101)
        text("X position(1-100): " + posX,(width/12),height/12);
        text("Y position(101-201): " + posY,(width/12),height/8);

        for ( i=0; i < faces.length; i++) // Box the faces and print its location
        rect((faces[i].location.x)*width/320, (faces[i].location.y)*height/240, (2.5*faces[i].distance)*width/320, (3*faces[i].distance)*height/240);



  if (send_flag==true && faces.length!=0 && pev_sec!=second()) //we can send data

  if (pev_posX==posX)
    if (posX<40)
    {byte[] data = {'1'}; bt.broadcast(data); println("LEFT");}

    if (posX>60)  
    {byte[] data = {'2'}; bt.broadcast(data); println("RIGHT");}
    if (pev_posY==posY)
    if (posY<140)
    {byte[] data = {'3'}; bt.broadcast(data); println("UP");}

    if (posY>160)
    {byte[] data = {'4'}; bt.broadcast(data); println("DOWN");}

  if (posX>40 && posX<60 && posY>140 &&posY<160)
  {byte[] data = {'5'}; bt.broadcast(data); println("No Change");}

println("send from BT");

  if (pev_sec!=second())


void onCameraPreviewEvent()

void onSavePhotoEvent( String filename) {

public void mousePressed(){
  text("Photo captured",width/2,height/2);

void textfun()
  cninfo = getBluetoothInformation();    //get connection information status

what could be the problem , please help me

Kind regards


  • @ColinJack2017===

    • are you sure that the android os environment is solved?
    • if yes i dont understand why bundle is not solved
    • if no the problem is with your sdk
  • thank you @akenaton for the replay , problem solved , I'm new to processing 3 and i should put that code in a folder with the same name of the .pde file , anyway there is a thing about the Bluetooth connection i want to ask about can i do it here or should i start a new question ?! Kind regards my friend

  • @ColinJack2017===

    i am not an admin && cannot be sure, yet i think that you can ask here as it is about BlueTooth

  • @akenaton , the above code is used for face detection , the app fist ask to start bluetooth then it should connect to paired bluetooth device with the name "HC-05" , the thing is when i start the app it starts the bluetooth ,but it doesn't connect to the device ! the function getBluetoothInformation() return null ! just the string "Connected to " with no bluetooth Name or mac address ! can you please help me to solve this problem . P.S: i have Bluetooth "HC-05" and i tried it using other apps and i worked fine !

  • @ColinJack2017===

    first : Are permissions ok??? (blue tooth, blue tooth admin)???

    then verify with code (in setup)::

        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
         if (!bluetooth.isEnabled()) {
          Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
          getActivity().startActivityForResult(requestBluetooth, 0);
    //and add  the method
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(requestCode==0) {
      if(resultCode == RESULT_OK) {
      println ("Bluetooth has been switched ON");
      } else {
      println("You need to turn Bluetooth ON !!!");
  • @akenaton , sorry for asking i'm still new , do you want me to add this code at the start of setup() function ?! when the application start it turn on the bluetooth without any problem ,but with no real connection to the bluetooth HC-05 ! as i said the getBluetoothInformation() return null !

  • edited August 2017


    first part of the code is in the setup, in order to be sure that blue tooth is enabled; second part, the method is the result for the first 1: onActivityResult: so you can be sure that everything is ok (supposing that permissions were added in the Manifest like that::

        <manifest ... >
          <uses-permission android:name="android.permission.BLUETOOTH" />
          <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
          <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    ). Of course the method must be outside of the setup and draw, like every method. looking to your code i see that you use at the same moment android native blue tooth and ketai bluetooth; as i have never used ketai for that i cannot see why ( i dont speak about the cam); as for me i will use the native android classes for finding (broadcast receiver) and listing the devices; then you can try to get the name and/or adress for the discovered device and finally connect to it.

    more details here:

    Anyway, as i think that ketai is only some kind of wrapper for theses classes it is probably a bad idea to mix the twos.Looking at ketai bluetooth example (but perhaps i have an old version) you can see that there is not any android bluetooth instance.

  • @akenaton , once again thanks for the replay , i understand this , but the real problem is that the function bt.getConnectedDeviceNames(); return nothing ! so when i start the app , it's connected to nothing , even if i pair the two devices and there is no function i know that can confirm any send operation via Bluetooth whether it's done or failed ! is there any way to know that ?! that could help to test if the broadcast is done successfully or not !

  • Answer ✓


    normally (i mean with android native as i dont use ketai for that):

    • a) you verify that blue tooth is enabled : i have put the code for that.

    • b) if devices are not already paired (you can know that with getBondedDevices()) you start the discovery process creating a receiver for broadcasting and an intent with filter ACTION_FOUND; it s there that you can know the mac adress (device.getAdress()) and name (device.getName()); the filter can be declared in your manifest or by code. at the end of this process you must call cancelDiscovery();

    • c) at this point you can create the connection between the devices, with a socket for client and server and some common channel. Normally you make that in a thread.

    i have done that successfully many times but never using ketai; when i get some time left i ll have a look to the ketai src (if available!) _ BTW what version of this lib are you using??

  • problem fixed finally , i used the mac address instead of using Bluetooth name and all worked fine . thanks for your help my friend

Sign In or Register to comment.