Bluetooth between Android and HC-05

In a previous post, https://astrospanner.wordpress.com/2016/04/13/bluetooth-enabled-lcd-driver/ I talked about the hardware side and the code on the PIC.  This post is about the android software. I wasn’t able to find a good reference on how to do this, so there was some experimentation, hence the need for a clear post like this.

Introduction

Android uses, not surprisingly, a “client” and “server” model for Bluetooth communications. This is outlined in their developer guides (http://developer.android.com/guide/topics/connectivity/bluetooth.html), but it assumes you are connecting two android devices, and so can decide which is the client and which is the server. This isn’t the case here, adn the HC-05 documentation doesn’t specify whether its a client or server. It talks about “slave” and “master”, but that doesn’t really help…

An Aside

I am not a particularly skilled Android developer. When I see examples like this

CaseInsensitiveString cis = new CaseInsensitiveString("Polish");

my brain struggles; why am I having to type “CaseInsensitiveString” twice? Isn’t the compiler meant to be sorting out type dependences like this for me?

After a while I realise that this is

  1. defining a new object called “cis” of type “CaseInsensitiveString”
  2. Assigning it to a value of “polish”

The Android developer guide has the following example

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

It’s good that we can all type “Adapter” correctly after these examples…

What I found

The HC-05, when configured as a slave (as by default),  works as a “server” (in the Bluetooth Socket sense).

The process that Android code has to do is then straightforward

  1. Make sure Bluetooth is on
  2. Setup to talk to remote device by MAC address
  3. Create an RF Comm socket
  4. Connect the socket
  5. Get the output stream for the scoket
  6. Write bytes into the outputstream

At Point 2, you need the MAC address of the HC-05. This is discoverable by code, (I used what is reported by BT Term for this, which is a cheat), but I’ve not yet implement it.

At Point 3, you need to set a UUID. I initially thought this was just a random number, but you need to be specific; its this UUID that tells the HC-05 that you are connecting via SSP (Simple Serial Protocol). The magic ID is

00001101-0000-1000-8000-00805F9B34FB

Searching for this ID specificially shows that its in the Adnroid Bluetooth guide, which shows I didn’t read it properly…

 

Code fragment

The resultant code (with try/catch blocks removed for readability) is then. This is single threaded, blocking, and probably gives real Android developers a hernia. Hopefully its useful for someone.

String address = "98:D3:31:FD:18:B8"; // todo remove hardcoding

UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

BluetoothDevice device = BA.getRemoteDevice(address);

btSocket = device.createRfcommSocketToServiceRecord(SPP_UUID);

// connect.
while (btSocket.isConnected() == false) {
    try {
        btSocket.connect();
    } catch (IOException e) {
    }
}

// write a message
outStream = btSocket.getOutputStream();
String message = "M07Conn...";
byte[] msgBuffer = message.getBytes();

outStream.write(msgBuffer);

outStream.flush();

Conclusion

The code works, and I have a specific app on my home which will connect to the HC-05, and push things onto the screen. I then started noticing timing issues, which will have to be dealt with. But it’s now time to finalize the hardware and package it properly.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s