Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Bluetooth List devices array

$
0
0

Hello, I have been looking at other examples on the forum of getting a bluetooth device connected on an Android device. I have the below code successfully working. I'm not using the popular Ketai library because I want to utilise RSSI signal and display all discoverable devices and RSSI signal strength on the canvas.

This is the point at which I am stuck. I have one discoverable device displayed and if a new device is discovered this supersedes the last one. However, I would prefer to add each discovered device into an ArrayList to get its name and its rssi info further up the draw loop for future drawing methods. I have the below code compiling but I don't think how I have written the ArrayList is correct as I am unable to access get() and size() in the draw loop.

In short I am not understanding my ArrayList method very well when used with the Bluetooth Android class.

Any advice or pointers would be greatly appreciated. Thanks

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import android.os.Looper;
import android.app.Activity;
import android.app.Fragment;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

Activity act;
Context mc;

String discoveredDeviceName;
String device;
short rssi;
boolean foundDevice=false; //When this is true, the screen turns green.
ArrayList<String> devicesDiscovered = new ArrayList();


//Get the default Bluetooth adapter
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

void setup()
{
  fullScreen();
  orientation(LANDSCAPE);
  textSize(28);
  textAlign(CENTER, CENTER);
  fill(225);

  /*IF Bluetooth is NOT enabled, then ask user permission to enable it */
  if (!bluetooth.isEnabled()) {
    Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    this.getActivity().startActivityForResult(requestBluetooth, 0);
  }

  /*If Bluetooth is now enabled, then register a broadcastReceiver to report any
   discovered Bluetooth devices, and then start discovering */
  if (bluetooth.isEnabled()) {
    this.getActivity().registerReceiver(myDiscoverer, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    //registerReceiver(myDiscoverer, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    //Start bluetooth discovery if it is not doing so already
    if (!bluetooth.isDiscovering()) {
      bluetooth.startDiscovery();
    }
  }
}

void draw()
{

  background(25, 25, 220);



  if (foundDevice) {
    background(0, 255, 0, 100);
    fill(0);
    text("Found Device" +discoveredDeviceName+"\nRSSI: " + abs(rssi), width/2, (height/2+150));


   //cannot access get() and size()
    //for (int i = 0; i < device.size(); i++) {
    //text(device.get(i), 10, 10);
    //}
  }


  //update the RSSI values every 10th of a frame.
  if (frameCount%10==0) {
    discoverDevices();
    //  delay(500);//don't use delay in Processing.
  }
}

public void discoverDevices() {
  if (bluetooth.isDiscovering()) {
    if (frameCount%5==0) {
      bluetooth.cancelDiscovery();
    }
    //delay(100); // DONT CHANGE 100ms GOOD VALUE
    bluetooth.startDiscovery();
  } else {
    bluetooth.startDiscovery();
  }
}


@Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode==0) {
    if (resultCode == 1) {
      text("Bluetooth has been switched ON", 10, 10);
    } else {
      text("You need to turn Bluetooth ON !!!", 10, 10);
    }
  }
}

/* Create a Broadcast Receiver that will later be used to
 receive the names of Bluetooth devices in range. */
BroadcastReceiver myDiscoverer = new myOwnBroadcastReceiver();

/* This BroadcastReceiver will display discovered Bluetooth devices */
public class myOwnBroadcastReceiver extends BroadcastReceiver {
  @Override
    public void onReceive(Context context, Intent intent) {
    discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
    rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
    println("RSSI: " + rssi);
    //Display the name of the discovered device
    println("Discovered: " + discoveredDeviceName);

    //Change foundDevice to true which will make the screen turn green
    foundDevice=true;

    devicesDiscovered.add(discoveredDeviceName + "\n" + rssi);
    for (String device : devicesDiscovered) {

      printArray(device);
    }
  }
}

Viewing all articles
Browse latest Browse all 941

Trending Articles