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.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
import ketai.camera.*;
import ketai.cv.facedetector.*;
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) {
 super.onCreate(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();
 bt.connectToDeviceByName(selection);
 //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()
{fullScreen(P2D);
}

void setup() {
  bt.start(); //start listening for BT connections
  bt.getPairedDeviceNames();
  bt.connectToDeviceByName("HC-05"); //Connect to our HC-06 bluetooth module
  delay(2000);
  stroke(0, 255, 0);
  strokeWeight(8);
  noFill();
  textSize(38);
  rectMode(CENTER);
  imageMode(CENTER);
  cam = new KetaiCamera(this, 320, 240, 24);
  directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath());

  cam.setCameraID(0);
  cam.setSaveDirectory(directory);
  cam.setPhotoSize(1280,720);
  cam.autoSettings();

}

void draw() {

 if (cam != null)
 {
       if (!cam.isStarted())
       cam.start();

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

       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)
        {
        fill(#2C1BF5);
        textAlign(LEFT);
        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
        {
        noFill();
        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);
        println(i,faces[i].location.x,faces[i].location.y); 
        }



}

textfun();




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

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

send_flag=false; 
println("send from BT");
  }

  if (pev_sec!=second())
  send_flag=true;



}



void onCameraPreviewEvent()
{
  cam.read();
}


void onSavePhotoEvent( String filename) {
 cam.addToMediaLibrary(filename); 
 textAlign(CENTER);
}

public void mousePressed(){
  cam.savePhoto("CD_selfie.jpg");
  text("Photo captured",width/2,height/2);
  delay(2000);
}

void textfun()
{ 
  textSize(30);
  textAlign(CENTER);
  fill(255);
  cninfo = getBluetoothInformation();    //get connection information status
  text(cninfo,width/2,height-height/1.03);
  noFill();
}

what could be the problem , please help me

Kind regards

Answers

  • @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
    
    //@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(requestCode==0) {
      if(resultCode == RESULT_OK) {
      background(0);
      println ("Bluetooth has been switched ON");
    
    
      } else {
      background(0);
      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

    @ColinJack2017===

    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" />
          ...
        </manifest>
    

    ). 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:

    https://developer.android.com/guide/topics/connectivity/bluetooth.html

    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 ✓

    @ColinJack2017===

    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.