Monday, March 18, 2013

Bluetooth Android Processing 2

PART TWO

If you happened to land on this page and missed PART ONE, I would advise you go back and read that section first. You may get lost coming in half way through the story. This is what you'll find in part one.
  • Downloading and setting up the Android SDK
  • Downloading the Processing IDE
  • Setting up and preparing the Android device
  • Running through a couple of Processing/Android sketches on an Andoid phone.
In the last sketch we checked to see if Bluetooth was enabled, if not, we then asked for permission to turn it on. The screen would then display a different colour depending on the Bluetooth state. So let's keep on going,


ToastMaster - the master of all Toasts

I will now introduce you to Toast. What does "Toast" have to do with programming ? Toast is used by Android to quietly display little messages on the screen.
Have a look here for a a quick introduction to Toast, otherwise have a look at the Android Developers Toast information.

I will be creating my own method that relies on Toast to make the process of displaying messages easier. I have named this method: "ToastMaster".
A word of warning. Calling ToastMaster from within setup() will cause errorsin the DiscoverBluetooth sketch (further down this page).
This will not happen in every sketch, but the Discoverbluetooth sketch has subActivities which may cause some sort of conflict.. I did warn you.

Here is a quick look at my ToastMaster method (no need to compile this code):
1
2
3
4
5
6
7
8
/* My ToastMaster function to display a messageBox on the screen */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> ToastMaster(String textToDisplay){
Toast myMessage = Toast.makeText(getApplicationContext(),
textToDisplay,
Toast.LENGTH_LONG);
myMessage.setGravity(Gravity.CENTER, 0, 0);
myMessage.show();
}

Here is a breakdown of what this is doing:
  • Toast.makeText() - is used to construct the message to be displayed. 
  • getApplicationContext() - gets a handle on the Application
  • textToDisplay - is obvious, this is the text you want to display.
  • Toast.LENGTH_LONG - is how long you want the message to displayed for. (or LENGTH_SHORT)
  • setGravity() - sets the message position on the screen, in this case I have chosen to center the text.
  • show() - is used to actually show the message.

Broadcast Receivers : Looking out for Bluetooth devices
To listen/look out for any Bluetooth devices that are within range, we need to create and  register a Broadcast receiver.
When registering a BroadcastReceiver, you will need to tell the program what it is you are looking / listening out for. In our case we want to listen out for occasions whereby a Bluetooth device is FOUND.  This is represented by:
If a BluetoothDevice is found, then the designated BroadcastReceiver will be called. We make our own BroadcastReceiver in order to perform a task such as displaying the name of the discovered device on the phone. However, before you will find anything, you have to start Discovering. This is done by calling the startDiscovery() method of the default Bluetooth adapter.

Here are the relevant components:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
BroadcastReceiver myDiscoverer =
style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver();

style="color: rgb(0, 128, 0);">//Within Setup()style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (bluetooth.isEnabled()) {
registerReceiver(myDiscoverer, style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> IntentFilter(BluetoothDevice.ACTION_FOUND));
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (!bluetooth.isDiscovering()){
bluetooth.startDiscovery();
}
}


style="color: rgb(0, 128, 0);">/* This BroadcastReceiver will display discovered Bluetooth devices */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onReceive(Context context, Intent intent) {
String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);

style="color: rgb(0, 128, 0);">//Display the name of the discovered devicestyle="color: rgb(0, 0, 0);">
ToastMaster("style="color: rgb(139, 0, 0);">Discovered: style="color: rgb(0, 0, 0);">" + discoveredDeviceName);
}
}





Discovering Bluetooth devices: putting it all together

You will notice that in the following sketch, we have to import a whole lot more. Which is why I have tried to break it down into bite size chunks, to help you digest it all. Now we will put it all together into a sketch which will
  • ask to turn Bluetooth ON if it happens to be disabled.
  • If you don't turn on Bluetooth, it will tell you that you need to turn it on.
  • If you turn on bluetooth (or if it was already on), it will try to discover any bluetooth devices in range. These devices need to be made "discoverable" before running this sketch.
  • If the phone finds a bluetooth device, it will display the name of the device and will change the background screen colour to GREEN.
Android/Processing Sketch 4: DiscoverBluetooth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* DiscoverBluetooth: Written by ScottC on 18 March 2013 using 
Processing version 2.0b8
Tested on a Samsung Galaxy SII, with Android version 2.3.4
Android ADK - API 10 SDK platform */
style="color: rgb(0, 0, 0);">

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import android.view.Gravity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

boolean foundDevice=style="color: rgb(0, 0, 255);">falsestyle="color: rgb(0, 0, 0);">; style="color: rgb(0, 128, 0);">//When this is true, the screen turns green.style="color: rgb(0, 0, 0);">

style="color: rgb(0, 128, 0);">//Get the default Bluetooth adapterstyle="color: rgb(0, 0, 0);">
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();


style="color: rgb(0, 128, 0);">/*The startActivityForResult() within setup() launches an
Activity which is used to request the user to turn Bluetooth on.
The following onActivityResult() method is called when this
Activity exits. */style="color: rgb(0, 0, 0);">
@Override
style="color: rgb(0, 0, 255);">protectedstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onActivityResult(style="color: rgb(0, 0, 255);">intstyle="color: rgb(0, 0, 0);"> requestCode, style="color: rgb(0, 0, 255);">intstyle="color: rgb(0, 0, 0);"> resultCode, Intent data){
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(requestCode==0){
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(resultCode == RESULT_OK){
ToastMaster("style="color: rgb(139, 0, 0);">Bluetooth has been switched ONstyle="color: rgb(0, 0, 0);">");
} style="color: rgb(0, 0, 255);">elsestyle="color: rgb(0, 0, 0);"> {
ToastMaster("style="color: rgb(139, 0, 0);">You need to turn Bluetooth ON !!!style="color: rgb(0, 0, 0);">");
}
}
}



style="color: rgb(0, 128, 0);">/* Create a Broadcast Receiver that will later be used to
receive the names of Bluetooth devices in range. */style="color: rgb(0, 0, 0);">
BroadcastReceiver myDiscoverer = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver();



style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> setup(){
orientation(LANDSCAPE);
style="color: rgb(0, 128, 0);">/*IF Bluetooth is NOT enabled, then ask user permission to enable it */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (!bluetooth.isEnabled()) {
Intent requestBluetooth = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(requestBluetooth, 0);
}

style="color: rgb(0, 128, 0);">/*If Bluetooth is now enabled, then register a broadcastReceiver to report any
discovered Bluetooth devices, and then start discovering */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (bluetooth.isEnabled()) {
registerReceiver(myDiscoverer, style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> IntentFilter(BluetoothDevice.ACTION_FOUND));
style="color: rgb(0, 128, 0);">//Start bluetooth discovery if it is not doing so alreadystyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (!bluetooth.isDiscovering()){
bluetooth.startDiscovery();
}
}
}



style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> draw(){
style="color: rgb(0, 128, 0);">//Display a green screen if a device has been foundstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(foundDevice){
background(10,255,10);
}
}



style="color: rgb(0, 128, 0);">/* This BroadcastReceiver will display discovered Bluetooth devices */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onReceive(Context context, Intent intent) {
String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);

style="color: rgb(0, 128, 0);">//Display the name of the discovered devicestyle="color: rgb(0, 0, 0);">
ToastMaster("style="color: rgb(139, 0, 0);">Discovered: style="color: rgb(0, 0, 0);">" + discoveredDeviceName);

style="color: rgb(0, 128, 0);">//Change foundDevice to true which will make the screen turn greenstyle="color: rgb(0, 0, 0);">
foundDevice=style="color: rgb(0, 0, 255);">truestyle="color: rgb(0, 0, 0);">;
}
}



style="color: rgb(0, 128, 0);">/* My ToastMaster function to display a messageBox on the screen */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> ToastMaster(String textToDisplay){
Toast myMessage = Toast.makeText(getApplicationContext(),
textToDisplay,
Toast.LENGTH_LONG);
myMessage.setGravity(Gravity.CENTER, 0, 0);
myMessage.show();
}


Upgrading the Broadcast Receiver : More Device info

Ok, we have the device name. But what other information can we collect from the device? You can call
This will return the discovered BluetoothDevice, which can then be probed to find the following information.
  • .getName()   =  Which is a different way of getting the name of the BluetoothDevice.
  • .getAddress() = Returns the hardware address of the BluetoothDevice.  eg. "00:11:22:AA:BB:CC"
  • .getBondState() = Returns an integer which describes the BondState of the BluetoothDevice
These are the three possible BondStates 
Here is an updated version of the custom BroadcastReceiver class (myOwnBroadcastReceiver) from the DiscoverBluetooth sketch described above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* This BroadcastReceiver will display discovered Bluetooth devices */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onReceive(Context context, Intent intent) {

style="color: rgb(0, 128, 0);">//Display the name of the discovered devicestyle="color: rgb(0, 0, 0);">
String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
ToastMaster("style="color: rgb(139, 0, 0);">Discovered: style="color: rgb(0, 0, 0);">" + discoveredDeviceName);

style="color: rgb(0, 128, 0);">//Display more information about the discovered devicestyle="color: rgb(0, 0, 0);">
BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
ToastMaster("style="color: rgb(139, 0, 0);">getAddress() = style="color: rgb(0, 0, 0);">" + discoveredDevice.getAddress());
ToastMaster("style="color: rgb(139, 0, 0);">getName() = style="color: rgb(0, 0, 0);">" + discoveredDevice.getName());

style="color: rgb(0, 0, 255);">intstyle="color: rgb(0, 0, 0);"> bondyState=discoveredDevice.getBondState();
ToastMaster("style="color: rgb(139, 0, 0);">getBondState() = style="color: rgb(0, 0, 0);">" + bondyState);

String mybondState;
style="color: rgb(0, 0, 255);">switchstyle="color: rgb(0, 0, 0);">(bondyState){
style="color: rgb(0, 0, 255);">casestyle="color: rgb(0, 0, 0);"> 10: mybondState="style="color: rgb(139, 0, 0);">BOND_NONEstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
style="color: rgb(0, 0, 255);">casestyle="color: rgb(0, 0, 0);"> 11: mybondState="style="color: rgb(139, 0, 0);">BOND_BONDINGstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
style="color: rgb(0, 0, 255);">casestyle="color: rgb(0, 0, 0);"> 12: mybondState="style="color: rgb(139, 0, 0);">BOND_BONDEDstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
style="color: rgb(0, 0, 255);">defaultstyle="color: rgb(0, 0, 0);">: mybondState="style="color: rgb(139, 0, 0);">INVALID BOND STATEstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
}
ToastMaster("style="color: rgb(139, 0, 0);">getBondState() = style="color: rgb(0, 0, 0);">" + mybondState);

style="color: rgb(0, 128, 0);">//Change foundDevice to true which will make the screen turn greenstyle="color: rgb(0, 0, 0);">
foundDevice=style="color: rgb(0, 0, 255);">truestyle="color: rgb(0, 0, 0);">;
}
}

If you replace the old version of  myOwnBroadcastReceiver with this one, you will know a little bit more about the devices discovered.


Connecting to the Bluetooth Device:
While we now have more information about the Bluetooth device, we don't really need it, and we will get rid of it by the end of the tutorial, however we will keep it here for the time being. In the next updated sketch we will be making a connection to the discovered device, and turning the background purple when the connection is made. In order to do this we will need to
  • Create a boolean variable to hold the connection status
  • Create and register a new BroadcastReceiver to notify us when a connection broadcast action has been received.
  • Create a new thread to handle the connection
  • Change the background screen colour when a successful connection has been made
First we need the boolean to hold the connection status:
  • boolean BTisConnected=false;
When the boolean is true, the screen will change to purple. The draw() method will be updated to accommodate this requirement.
Next we will create and register a new BroadcastReceiver, it is created using this:
  • BroadcastReceiver checkIsConnected = new myOwnBroadcastReceiver();
This broadcastreceiver will be used to notify us when a connection has been made. Therefore we need to register the (BluetoothDevice.ACTION_ACL_CONNECTED)
action with the BroadcastReceiver in the following way
  • registerReceiver(checkIsConnected, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
We will need to update myOwnBroadcastReceiver() to be able to differentiate beween this action and the (BluetoothDevice.ACTION_FOUND) action used already. This is done by first getting the action from the intent variable described in the onReceive() method within myOwnBroadcastReceiver().
  • String action=intent.getAction();
We can differentiate the two actions using the following simplified code in myOwnBroadcastReceiver:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onReceive(Context context, Intent intent) {
String action=intent.getAction();

style="color: rgb(0, 128, 0);">//Notification that BluetoothDevice is FOUNDstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(BluetoothDevice.ACTION_FOUND.equals(action)){
foundDevice=style="color: rgb(0, 0, 255);">truestyle="color: rgb(0, 0, 0);">; style="color: rgb(0, 128, 0);">//Change the screen to greenstyle="color: rgb(0, 0, 0);">
}

style="color: rgb(0, 128, 0);">//Notification if bluetooth device is connectedstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
BTisConnected=style="color: rgb(0, 0, 255);">truestyle="color: rgb(0, 0, 0);">; style="color: rgb(0, 128, 0);">//turn screen purplestyle="color: rgb(0, 0, 0);">
}
}
}

Now that we can be notified about the connection made to the Bluetooth Device, lets go through the code required to make the connection. We will only connect if we have actually discovered a device, so we will put this code within the FOUND section of myOwnBroadcastReceiver.

1
2
3
4
5
6
7
8
 style="color: rgb(0, 128, 0);">//Connect to the discovered bluetooth device (SeeedBTSlave)style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(discoveredDeviceName.equals("style="color: rgb(139, 0, 0);">SeeedBTSlavestyle="color: rgb(0, 0, 0);">")){
unregisterReceiver(myDiscoverer);
ConnectToBluetooth connectBT = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> ConnectToBluetooth(discoveredDevice);
style="color: rgb(0, 128, 0);">//Connect to the the device in a new threadstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> Thread(connectBT).start();
}
}

We use the discoveredDeviceName variable to specifically target the Bluetooth device we wish to connect to. We then unregister the myDiscoverer BroadcastReceiver because we are going to stop discovering before we connect to the Bluetooth Device, plus if you don't, it will generate an error. We then pass our discovered device to a new Thread to connect to that device in the background.  The class used to handle the connection is the "ConnectToBluetooth" class as displayed below:

We will cancelDiscovery() on the bluetooth Adapter to prevent a slow connection.
Also we will need to use a specific UUID as per below:
  • private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
I have tried changing the UUID, but changing it to a different number prevented it from establishing a connection.
Before you can connect to the Bluetooth shield you need to use the UUID to create a BluetoothSocket.
  • mySocket = btShield.createRfcommSocketToServiceRecord(uuid);
Once you have the socket, you can then try to connect using:
  • mySocket.connect();
Make sure you have some way of closing the socket, this is done in the cancel() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> ConnectToBluetooth implements Runnable{
style="color: rgb(0, 0, 255);">privatestyle="color: rgb(0, 0, 0);"> BluetoothDevice btShield;
style="color: rgb(0, 0, 255);">privatestyle="color: rgb(0, 0, 0);"> BluetoothSocket mySocket = null;
style="color: rgb(0, 0, 255);">privatestyle="color: rgb(0, 0, 0);"> UUID uuid = UUID.fromString("style="color: rgb(139, 0, 0);">00001101-0000-1000-8000-00805F9B34FBstyle="color: rgb(0, 0, 0);">");

style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> ConnectToBluetooth(BluetoothDevice bluetoothShield) {
btShield = bluetoothShield;
style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);">{
mySocket = btShield.createRfcommSocketToServiceRecord(uuid);
}style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);">(IOException createSocketException){
style="color: rgb(0, 128, 0);">//Problem with creating a socketstyle="color: rgb(0, 0, 0);">
}
}

@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> run() {
style="color: rgb(0, 128, 0);">/* Cancel discovery on Bluetooth Adapter to prevent slow connection */style="color: rgb(0, 0, 0);">
bluetooth.cancelDiscovery();

style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);">{
style="color: rgb(0, 128, 0);">/*Connect to the bluetoothShield through the Socket. This will block
until it succeeds or throws an IOException */style="color: rgb(0, 0, 0);">
mySocket.connect();
} style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);"> (IOException connectException){
style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);">{
mySocket.close(); style="color: rgb(0, 128, 0);">//try to close the socketstyle="color: rgb(0, 0, 0);">
}style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);">(IOException closeException){
}
style="color: rgb(0, 0, 255);">returnstyle="color: rgb(0, 0, 0);">;
}
}

style="color: rgb(0, 128, 0);">/* Will cancel an in-progress connection, and close the socket */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> cancel() {
style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);"> {
mySocket.close();
} style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);"> (IOException e){
}
}
}

The major structure of this code was made possible using the following site:
http://jayxie.com/mirrors/android-sdk/guide/topics/wireless/bluetooth.html

And the following sites were also useful in getting some of the information I needed:
http://stackoverflow.com/questions/13238600/use-registerreceiver-for-non-activity-and-non-service-class
http://developer.android.com/guide/topics/connectivity/bluetooth.html


While I have described all the major components required to connect to the Bluetooth Device, I will now put it all together in a new and updated version of the "DiscoverBluetooth" Android/Processing sketch and call it "ConnectBluetooth". There is some additional code in this sketch which I did not specifically go through, for example, the code used to turn the background to purple in the draw() method. Look out for that one. Anyway, read through the following code, and make sure that you understand what each section is doing.

Android/Processing Sketch 5: ConnectBluetooth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* ConnectBluetooth: Written by ScottC on 18 March 2013 using 
Processing version 2.0b8
Tested on a Samsung Galaxy SII, with Android version 2.3.4
Android ADK - API 10 SDK platform */
style="color: rgb(0, 0, 0);">

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import android.view.Gravity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import java.util.UUID;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;

import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;

boolean foundDevice=style="color: rgb(0, 0, 255);">falsestyle="color: rgb(0, 0, 0);">; style="color: rgb(0, 128, 0);">//When true, the screen turns green.style="color: rgb(0, 0, 0);">
boolean BTisConnected=style="color: rgb(0, 0, 255);">falsestyle="color: rgb(0, 0, 0);">; style="color: rgb(0, 128, 0);">//When true, the screen turns purple.style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 0);">


style="color: rgb(0, 128, 0);">//Get the default Bluetooth adapterstyle="color: rgb(0, 0, 0);">
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();


style="color: rgb(0, 128, 0);">/*The startActivityForResult() within setup() launches an
Activity which is used to request the user to turn Bluetooth on.
The following onActivityResult() method is called when this
Activity exits. */style="color: rgb(0, 0, 0);">
@Override
style="color: rgb(0, 0, 255);">protectedstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onActivityResult(style="color: rgb(0, 0, 255);">intstyle="color: rgb(0, 0, 0);"> requestCode, style="color: rgb(0, 0, 255);">intstyle="color: rgb(0, 0, 0);"> resultCode, Intent data){
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(requestCode==0){
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(resultCode == RESULT_OK){
ToastMaster("style="color: rgb(139, 0, 0);">Bluetooth has been switched ONstyle="color: rgb(0, 0, 0);">");
} style="color: rgb(0, 0, 255);">elsestyle="color: rgb(0, 0, 0);"> {
ToastMaster("style="color: rgb(139, 0, 0);">You need to turn Bluetooth ON !!!style="color: rgb(0, 0, 0);">");
}
}
}



style="color: rgb(0, 128, 0);">/* Create a BroadcastReceiver that will later be used to
receive the names of Bluetooth devices in range. */style="color: rgb(0, 0, 0);">
BroadcastReceiver myDiscoverer = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver();

style="color: rgb(0, 128, 0);">/* Create a BroadcastReceiver that will later be used to
identify if the Bluetooth device is connected */style="color: rgb(0, 0, 0);">
BroadcastReceiver checkIsConnected = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver();


style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> setup(){
orientation(LANDSCAPE);
style="color: rgb(0, 128, 0);">/*IF Bluetooth is NOT enabled, then ask user permission to enable it */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (!bluetooth.isEnabled()) {
Intent requestBluetooth = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(requestBluetooth, 0);
}

style="color: rgb(0, 128, 0);">/*If Bluetooth is now enabled, then register a broadcastReceiver to report any
discovered Bluetooth devices, and then start discovering */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (bluetooth.isEnabled()) {
registerReceiver(myDiscoverer, style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> IntentFilter(BluetoothDevice.ACTION_FOUND));
registerReceiver(checkIsConnected, style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));

style="color: rgb(0, 128, 0);">//Start bluetooth discovery if it is not doing so alreadystyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);"> (!bluetooth.isDiscovering()){
bluetooth.startDiscovery();
}
}
}



style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> draw(){
style="color: rgb(0, 128, 0);">//Display a green screen if a device has been found,style="color: rgb(0, 0, 0);">
style="color: rgb(0, 128, 0);">//Display a purple screen when a connection is made to the devicestyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(foundDevice){
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(BTisConnected){
background(170,50,255); style="color: rgb(0, 128, 0);">// purple screenstyle="color: rgb(0, 0, 0);">
}style="color: rgb(0, 0, 255);">elsestyle="color: rgb(0, 0, 0);"> {
background(10,255,10); style="color: rgb(0, 128, 0);">// green screenstyle="color: rgb(0, 0, 0);">
}
}
}



style="color: rgb(0, 128, 0);">/* This BroadcastReceiver will display discovered Bluetooth devices */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> myOwnBroadcastReceiver extends BroadcastReceiver {
@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> onReceive(Context context, Intent intent) {
String action=intent.getAction();
ToastMaster("style="color: rgb(139, 0, 0);">ACTION:style="color: rgb(0, 0, 0);">" + action);

style="color: rgb(0, 128, 0);">//Notification that BluetoothDevice is FOUNDstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(BluetoothDevice.ACTION_FOUND.equals(action)){
style="color: rgb(0, 128, 0);">//Display the name of the discovered devicestyle="color: rgb(0, 0, 0);">
String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
ToastMaster("style="color: rgb(139, 0, 0);">Discovered: style="color: rgb(0, 0, 0);">" + discoveredDeviceName);

style="color: rgb(0, 128, 0);">//Display more information about the discovered devicestyle="color: rgb(0, 0, 0);">
BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
ToastMaster("style="color: rgb(139, 0, 0);">getAddress() = style="color: rgb(0, 0, 0);">" + discoveredDevice.getAddress());
ToastMaster("style="color: rgb(139, 0, 0);">getName() = style="color: rgb(0, 0, 0);">" + discoveredDevice.getName());

style="color: rgb(0, 0, 255);">intstyle="color: rgb(0, 0, 0);"> bondyState=discoveredDevice.getBondState();
ToastMaster("style="color: rgb(139, 0, 0);">getBondState() = style="color: rgb(0, 0, 0);">" + bondyState);

String mybondState;
style="color: rgb(0, 0, 255);">switchstyle="color: rgb(0, 0, 0);">(bondyState){
style="color: rgb(0, 0, 255);">casestyle="color: rgb(0, 0, 0);"> 10: mybondState="style="color: rgb(139, 0, 0);">BOND_NONEstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
style="color: rgb(0, 0, 255);">casestyle="color: rgb(0, 0, 0);"> 11: mybondState="style="color: rgb(139, 0, 0);">BOND_BONDINGstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
style="color: rgb(0, 0, 255);">casestyle="color: rgb(0, 0, 0);"> 12: mybondState="style="color: rgb(139, 0, 0);">BOND_BONDEDstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
style="color: rgb(0, 0, 255);">defaultstyle="color: rgb(0, 0, 0);">: mybondState="style="color: rgb(139, 0, 0);">INVALID BOND STATEstyle="color: rgb(0, 0, 0);">";
style="color: rgb(0, 0, 255);">breakstyle="color: rgb(0, 0, 0);">;
}
ToastMaster("style="color: rgb(139, 0, 0);">getBondState() = style="color: rgb(0, 0, 0);">" + mybondState);

style="color: rgb(0, 128, 0);">//Change foundDevice to true which will make the screen turn greenstyle="color: rgb(0, 0, 0);">
foundDevice=style="color: rgb(0, 0, 255);">truestyle="color: rgb(0, 0, 0);">;

style="color: rgb(0, 128, 0);">//Connect to the discovered bluetooth device (SeeedBTSlave)style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(discoveredDeviceName.equals("style="color: rgb(139, 0, 0);">SeeedBTSlavestyle="color: rgb(0, 0, 0);">")){
ToastMaster("style="color: rgb(139, 0, 0);">Connecting you Now !!style="color: rgb(0, 0, 0);">");
unregisterReceiver(myDiscoverer);
ConnectToBluetooth connectBT = style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> ConnectToBluetooth(discoveredDevice);
style="color: rgb(0, 128, 0);">//Connect to the the device in a new threadstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">newstyle="color: rgb(0, 0, 0);"> Thread(connectBT).start();
}
}

style="color: rgb(0, 128, 0);">//Notification if bluetooth device is connectedstyle="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">ifstyle="color: rgb(0, 0, 0);">(BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
ToastMaster("style="color: rgb(139, 0, 0);">CONNECTED _ YAYstyle="color: rgb(0, 0, 0);">");
BTisConnected=style="color: rgb(0, 0, 255);">truestyle="color: rgb(0, 0, 0);">; style="color: rgb(0, 128, 0);">//turn screen purplestyle="color: rgb(0, 0, 0);">
}
}
}

style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">classstyle="color: rgb(0, 0, 0);"> ConnectToBluetooth implements Runnable{
style="color: rgb(0, 0, 255);">privatestyle="color: rgb(0, 0, 0);"> BluetoothDevice btShield;
style="color: rgb(0, 0, 255);">privatestyle="color: rgb(0, 0, 0);"> BluetoothSocket mySocket = null;
style="color: rgb(0, 0, 255);">privatestyle="color: rgb(0, 0, 0);"> UUID uuid = UUID.fromString("style="color: rgb(139, 0, 0);">00001101-0000-1000-8000-00805F9B34FBstyle="color: rgb(0, 0, 0);">");

style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> ConnectToBluetooth(BluetoothDevice bluetoothShield) {
btShield = bluetoothShield;
style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);">{
mySocket = btShield.createRfcommSocketToServiceRecord(uuid);
}style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);">(IOException createSocketException){
style="color: rgb(0, 128, 0);">//Problem with creating a socketstyle="color: rgb(0, 0, 0);">
}
}

@Override
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> run() {
style="color: rgb(0, 128, 0);">/* Cancel discovery on Bluetooth Adapter to prevent slow connection */style="color: rgb(0, 0, 0);">
bluetooth.cancelDiscovery();

style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);">{
style="color: rgb(0, 128, 0);">/*Connect to the bluetoothShield through the Socket. This will block
until it succeeds or throws an IOException */style="color: rgb(0, 0, 0);">
mySocket.connect();
} style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);"> (IOException connectException){
style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);">{
mySocket.close(); style="color: rgb(0, 128, 0);">//try to close the socketstyle="color: rgb(0, 0, 0);">
}style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);">(IOException closeException){
}
style="color: rgb(0, 0, 255);">returnstyle="color: rgb(0, 0, 0);">;
}
}

style="color: rgb(0, 128, 0);">/* Will cancel an in-progress connection, and close the socket */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">publicstyle="color: rgb(0, 0, 0);"> style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> cancel() {
style="color: rgb(0, 0, 255);">trystyle="color: rgb(0, 0, 0);"> {
mySocket.close();
} style="color: rgb(0, 0, 255);">catchstyle="color: rgb(0, 0, 0);"> (IOException e){
}
}
}


style="color: rgb(0, 128, 0);">/* My ToastMaster function to display a messageBox on the screen */style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> ToastMaster(String textToDisplay){
Toast myMessage = Toast.makeText(getApplicationContext(),
textToDisplay,
Toast.LENGTH_SHORT);
myMessage.setGravity(Gravity.CENTER, 0, 0);
myMessage.show();
}


The Arduino Sketch

Most of the Android/Processing code used so far has depended on a Bluetooth Device being discoverable. Our ultimate aim it to connect to a Bluetooth Shield on an Arduino UNO or compatible board such as the Freetronics Eleven. The following sketch was essentially taken from one of my previous posts (here), however, I have stripped it down to the bear essentials so that it will only be discoverable, and will not send or receive data. I will provide this functionality later. I just wanted to show you the essential bits to establish the connection to the Shield.

ARDUINO Sketch 1: Bluetooth Pair and Connect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* This project combines the code from a few different sources.
This project was put together by ScottC on the 22/03/2013
http://arduinobasics.blogspot.com/

Bluetooth slave code by Steve Chang - downloaded from :
http://www.seeedstudio.com/wiki/index.php?title=Bluetooth_Shield

This sketch does nothing more than setup bluetooth
connection capabilities. It does not send or receive data.

*/
style="color: rgb(0, 0, 0);">

#include <SoftwareSerial.h> style="color: rgb(0, 128, 0);">//Software Serial Portstyle="color: rgb(0, 0, 0);">

#define RxD 6 style="color: rgb(0, 128, 0);">// This is the pin that the Bluetooth (BT_TX) will transmit to the Arduino (RxD)style="color: rgb(0, 0, 0);">
#define TxD 7 style="color: rgb(0, 128, 0);">// This is the pin that the Bluetooth (BT_RX) will receive from the Arduino (TxD)style="color: rgb(0, 0, 0);">

#define DEBUG_ENABLED 1

SoftwareSerial blueToothSerial(RxD,TxD);

style="color: rgb(0, 128, 0);">/*----------------------SETUP----------------------------*/style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> setup() {
Serial.begin(9600); style="color: rgb(0, 128, 0);">// Allow Serial communication via USB cable to computer (if required)style="color: rgb(0, 0, 0);">
pinMode(RxD, INPUT); style="color: rgb(0, 128, 0);">// Setup the Arduino to receive INPUT from the bluetooth shield on Digital Pin 6style="color: rgb(0, 0, 0);">
pinMode(TxD, OUTPUT); style="color: rgb(0, 128, 0);">// Setup the Arduino to send data (OUTPUT) to the bluetooth shield on Digital Pin 7style="color: rgb(0, 0, 0);">
pinMode(13,OUTPUT); style="color: rgb(0, 128, 0);">// Use onboard LED if required.style="color: rgb(0, 0, 0);">
setupBlueToothConnection(); style="color: rgb(0, 128, 0);">//Used to initialise the Bluetooth shieldstyle="color: rgb(0, 0, 0);">
}

style="color: rgb(0, 128, 0);">/*----------------------LOOP----------------------------*/style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> loop() {
digitalWrite(13,LOW); style="color: rgb(0, 128, 0);">//Turn off the onboard Arduino LEDstyle="color: rgb(0, 0, 0);">
}


style="color: rgb(0, 128, 0);">//The following code is necessary to setup the bluetooth shield ------copy and paste----------------style="color: rgb(0, 0, 0);">
style="color: rgb(0, 0, 255);">voidstyle="color: rgb(0, 0, 0);"> setupBlueToothConnection()
{
blueToothSerial.begin(38400); style="color: rgb(0, 128, 0);">//Set BluetoothBee BaudRate to default baud rate 38400style="color: rgb(0, 0, 0);">
blueToothSerial.print("style="color: rgb(139, 0, 0);">\r\n+STWMOD=0\r\nstyle="color: rgb(0, 0, 0);">"); style="color: rgb(0, 128, 0);">//set the bluetooth work in slave modestyle="color: rgb(0, 0, 0);">
blueToothSerial.print("style="color: rgb(139, 0, 0);">\r\n+STNA=SeeedBTSlave\r\nstyle="color: rgb(0, 0, 0);">"); style="color: rgb(0, 128, 0);">//set the bluetooth name as "SeeedBTSlave"style="color: rgb(0, 0, 0);">
blueToothSerial.print("style="color: rgb(139, 0, 0);">\r\n+STOAUT=1\r\nstyle="color: rgb(0, 0, 0);">"); style="color: rgb(0, 128, 0);">// Permit Paired device to connect mestyle="color: rgb(0, 0, 0);">
blueToothSerial.print("style="color: rgb(139, 0, 0);">\r\n+STAUTO=0\r\nstyle="color: rgb(0, 0, 0);">"); style="color: rgb(0, 128, 0);">// Auto-connection should be forbidden herestyle="color: rgb(0, 0, 0);">
delay(2000); style="color: rgb(0, 128, 0);">// This delay is required.style="color: rgb(0, 0, 0);">
blueToothSerial.print("style="color: rgb(139, 0, 0);">\r\n+INQ=1\r\nstyle="color: rgb(0, 0, 0);">"); style="color: rgb(0, 128, 0);">//make the slave bluetooth inquirable style="color: rgb(0, 0, 0);">
Serial.println("style="color: rgb(139, 0, 0);">The slave bluetooth is inquirable!style="color: rgb(0, 0, 0);">");
delay(2000); style="color: rgb(0, 128, 0);">// This delay is required.style="color: rgb(0, 0, 0);">
blueToothSerial.flush();
}




Please make sure to setup the Bluetooth jumpers as per the picture below, otherwise you will not have much luck with the sketch above.

Jumpers on Shield




Well that brings us to the end of part TWO.

PART THREE
In part three we will attempt to actually send some data from the Android phone to the Arduino via Bluetooth, and vice versa. This will be when the real fun starts.


or GO BACK
Click on the link if you missed PART ONE






No comments:

Post a Comment