We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
}
}
}
Answers
Ok I have made some progress in looking at the backend java code of ketai library and how that accesses the ArrayList discoverable names.
I still cannot seem to instance the getDiscoveredDeviceNames().get(i) correctly in the draw loop so at the moment its doing nothing particularly useful. The aim is to associate an RSSI value to each new DiscoveredDeviceName.
@pigeon===
Looking quickly to your code i think that it cannot run because you never instantiate your receiver and never call your broadcast class.
@akenation Thank you -yes that makes sense! A case of not seeing the wood for the trees there. So in instantiating the receiver like so..
myOwnBroadcastReceiver bt;
in setup
bt = new myOwnBroadcastReceiver();
when calling the onReceive(Context context, Intent intent) function in draw loop like so
bt.onReceive(Context context, Intent intent);
is my intent 'discoveredDeviceName' I'm unsure what my context is here?
bt.onReceive(Context context, discoveredDeviceName);
@pigeon===
code snippet tested (with required permissions): it works; i have simplified your code (and suppressed some errors: your background cannot never be seen as green!!!!) but you can now easily get all infos about blueTooth devices: could be better to make an arrayList of all blueTooth objects and methods to get name, mac adress, rssi and so on...Think to unregister your receiver onPause (with cancelDiscovery()); see also that rssi can change, or new devices be discovered: your text in draw() can be difficult to read!
//@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { println(requestCode); 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. / / This BroadcastReceiver will display discovered Bluetooth devices */
public class myOwnBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
} }
void onPause() { super.onPause(); if (myDiscoverer!=null) { bluetooth.cancelDiscovery();
} }
@akenation ---> WoW! thank you. i really appreciate you taking the time to compile this for me. I don't think i would have gotten to the intentFilter part at all by myself. I will post a version here soon taking on board your advice about unregistering the receiver. Looking forward to utilising the discovered devices in interactive installation contexts. Thanks again