Arduino Bluetooth control with Android 12

Introduction

Did you ever think about controlling a device from your phone with Bluetooth using an app made by you? Me too. I don't even know what I'm going to control yet (I have some plans though which I will disclose in due time - I know this is exactly the kind of comment that will make you bookmark my blog and keep coming back).

Anyway, at this point I will be ecstatic if I can receive data from a device on my phone. I don't even need to send anything out yet as I know that if I can receive I can probably send at a later stage as all the harder work such as setting permissions on android has been done. Going from receiving to also sending data over Bluetooth is a rather trivial upgrade.

Most of what you will see here comes from this work. I added only the changes to make it all operational on Android 12. More breathtaking things will be built on top of this, you will see.

Hardware

On the android side of the equation, I will be using a 1-year-old Samsung S21 5G, model number SM-G9960. It's my personal phone. I don't think anything I will attempt can ever break this thing, but I have said that before. I declare the risk to be null, lets proceed.

My S21 is running Android 12 which I have come to learn has some changes in regards to Bluetooth permissions compared to previous versions. In fact, this is the main change to the template android code I used and partially the reason why I'm going through the pleasure of write this document. You see, there are quite a few articles describing the process of getting an Arduino to work with Android over Bluetooth but there isn't much information on accomplishing that on android 12. As always, I'm here to help.

On the Arduino side, I'm using a very old and now discontinued board with a BT module built-in. Here it is in all it's vintage glory.

Nowadays It's more common to have a separate BT module like this one . I don't have one of those to test but something higher (some call it intuition) tells me that all I'm going to look at is valid for both options.

The modules connect to pins 0 and 1 (AKA Rx and Tx). That means that conventional serial communications with other peripherals cannot be done through those pins but not to worry, the SoftwareSerial Library is here for you in case you still need serial communications which you can do with other digital inputs using that library. 

With the connection to pins 0 and 1, the Arduino sketch also needs to be loaded through Bluetooth. This is useful if you can't reach your device (it's inside a sealed enclosure, for instance) but it's a pain if you do can reach your device but you're paired to your phone at that moment. So near yet so far. As you test your program multiple pairings and unpairings must be made: pair your computer, load the sketch and disconnect. Pair your phone and test. Test will fail. Repeat.

Arduino code

The Arduino code is the slimpest I ever wrote in my life and it goes like this:

void setup() {
//Initialize serial comms at 115200
Serial.begin(115200);
}
void loop() {
//Just send out the value of millis() every 500ms
Serial.println(millis());
delay(500);
}

This does exactly what it says. It send out through the serial connection the current value of millis() every half second. The goal is now to get that updating value on the phone.

Android code

I cannot really put the complete android code here. It's available on GitHub on the link down below.

I based it all off this 4-year old supository. I don't want to repeat the great work of the gentleman that originally made it so I invite you to take a look at the project page. Even the file names are the same to make it easier for you to follow along, dear reader. If I understood it enough to implement his ideas on my project, you will too.

I removed some of the things I didn't need this time, mainly he's controlling two items, I removed that feature. I basically only kept the text area where the Arduino messages are displayed. The button you see on the screenshot is there for the next step which is sending information to the Arduino from the phone, something I can also get from that 4-year-old project.

As I mentioned previously, that original code will not work on modern Android versions mainly due to the permissions request to use BT. These now need to be explicitly asked from the user. If you require "android.permission.BLUETOOTH_CONNECT" on your application, you need this change done.

GitHub code is here. I could give you the apk too but the MAC address is hardcoded in so it wouldn't be of much use to you unless you somehow obtained my Arduino board.

Notes on some notes of note

  • On the phone you must first pair the Arduino (the device I'm using has a password of 12345) using the regular way, not through the app. The app doesn't have that feature. Go to the Bluetooth setup area, scan for devices and select to pair it from there. Somewhere along this process you will see something like this:

  • Remember to set the MAC address of your device on the Android code, change this line to the correct address on the Ardcon.java file.
public final static String MODULE_MAC = "00:07:80:42:05:8F"; //your dream address here

  • How do you know which MAC address to use, I hear you ask? On windows go to "Device manager"

Conclusion and final remarks

Bye bye.

Comments