Bluetooth Stuff

Bluetooth Suff

OK, so I have a project where I want to have a mobile robot I want to connect with. So I figure I had better get to grips with Bluetooth!

Bluetooth seems like a good choice for this project because I may ultimately want to use my android phone as a controller. The Android SDK allows use of the phone's Bluetooth hardware so I should be able to lash up something at that end - so that just leaves the mbed end of things.

Bluetooth hello world

Luckily for me Chris has written a Cookbook page on using the RN-42 Bluetooth module.

So I get myself a module a RN-42 module from Cool Components which arrives after a few days and 'dead bug' it upside down to a bit of strip board. I modify Chris's design slightly and add in two status LEDs so I can see what the module is doing. I also add a Molex header so I can quickly disconnect the module. After I'm finish it looks like this...

Bluetooth Module

Hello World!

So After loading the Hello World program from the Cookbook page onto the mbed I set about powering up the RN-42 module. It is connected exactly the same as shown on the cookbook page. So I plug it all in and the red LED starts flashing - what does that mean? Well looking at the datasheet doesn't help at all! It just says that the Status LED blinks at various speed to indicate status. OK so flashing red means... something?

So next I had better plug my Bluetooth dongle into the laptop. Through Windows I 'add a device' from the Bluetooth menu and the PC scans for devices - and finds an device named RN42-095F! Awesome. I connect to the device and it asks for a pairing code. I remember form the Cookbook page that the pairing code is 1234, so I enter that and Windows connects and installs a driver. So far so good.

Windows then tells me that my device is ready to use. Clicking on the installation I now see that I have two new 'Standard Serial over Bluetooth links' set as COM31 and COM32. I fire up Tera Term and connect over Serial to COM31, and then notice that the red LED on the RN-42 booard has gone off and the green LED is on. That has to be a good sign right?

I then set the baud rate to 115200, and start typing. I see the characters echoed back to the terminal and LED1 on the mbed flashing away merrily. Cool that was quite painless now for something else...

Hello Android

So I can communicate with a PC but can I communicate with a mobile phone?

I turn on Bluetooth on my Sony X10i and scan for devices - once again the RN-42 is picked up. Again when I try to pair with it it asks for a pair code. I enter 1234 and the phone has successfully paired with the RN-42 module. So far so good.

But now it is showing up as paired but not connected. So how do I connect to it? I decide to see what apps are availible from the Android market. I find one called BlueTerm which is a terminal that operates over Bluetooth - and it's free, perfect!

I download BlueTerm and start it up. Click the 'Menu' key and select 'Connect Device' scan for devices and the RN-42 if found. I try to connect to it... And it shows as connected, so maybe I'll type something? - It's echoed back! and the mbed's LED1 is flashing (bit odd that I didn't need to set up the baud rate but who cares?)

So it looks like mbed over Bluetooth is definitely possible - which opens up a load of cool stuff I can now do...

Hello World Code

Here is the code I used to send characters to and from the PC and android phone.


9 comments on Bluetooth Stuff:

02 Apr 2011

When you find out how to make Android Apps, especially with Bluetooth, please tell me, and the rest of the MBED community. Cheers Ceri.

02 Apr 2011

Wow that was quick - I just posted this page! I'm learning Android by basically reading a book and picking up code from the web! I'll do a write up on it in due course but am really just stating out at the moment.

02 Apr 2011

The baud rate only matters between the RN-42 and the mbed to match their bit rates, so the TTL serial communication can happen.

The baud rate doesn't matter on the Android(BlueTerm)/Bluetooth side, because that's strictly Bluetooth communication that has its own speed completely independent of the serial speed.

03 Apr 2011

Dave Van Wagner wrote:

The baud rate only matters between the RN-42 and the mbed to match their bit rates, so the TTL serial communication can happen.

The baud rate doesn't matter on the Android(BlueTerm)/Bluetooth side, because that's strictly Bluetooth communication that has its own speed completely independent of the serial speed.

Oh of course - sorry this was me being dumb!

09 Feb 2012

Hello Martin,

I have a problem to get the data from the mobile phone(BLUETERM) into the mbed. Basically I need to compare some characteres when I press them on the mobile app BLUETERM.

I am using this piece of code, which is simple and should works, but actually there is something wrong.

  1. include "mbed.h"

Serial pc(USBTX, USBRX); Serial rn41(p9, p10); DigitalOut myled(LED1);

int main() { rn41.baud(9600); while(1) { if( rn41.readable() ){ char c = rn41.getc(); pc.printf("%c",c); print the code of the letter typed on the phone myled = !myled; } wait (0.2); } }

As you are more confident, I really appreciate if you can give me a hand.

Many thanks

09 Feb 2012

Hi Marcio,

How far did you get with the connection process? Did you find the RN42? Did you pair with it OK?

Your code (below) points to the fact you are using a RN-41 module not an RN-42 module. I don't know if this could be the problem?

include "mbed.h"

Serial pc(USBTX, USBRX); 
Serial rn41(p9, p10); 
DigitalOut myled(LED1);

int main() { 
    rn41.baud(9600); 
    while(1) { 
        if( rn41.readable() ){ 
            char c = rn41.getc(); 
            pc.printf("%c",c); 
            // Print the code of the letter typed on the phone 
            myled = !myled; 
        } 
        wait (0.2); 
    } 
}

I have published the program I used for the RN-42 on this page. Hopefully that may help a little as well.

Martin

10 Feb 2012

Hi Martin,

The connection on the phone using BlueTerm works perfectly, but on the android system on the Settings menu I just can pair with the RN41 module (the phone says: paired but not connected). But on the BlueTerm the phone connects well without any problem at all.

I will have a look to your code if it can helps me. By the way, have you try the RN-42 module with mobile phone?

Thanks

03 Jun 2015

no problem while connecting to rn42 through android using blue term but can we make rn42 as master and connect phone or other pc through rn42 terminal.i tried to phone through rn42 using tera term as phone is paired. giving the mac address of phone to the rn42 it show trying but later gives connect failed please help me out /media/uploads/alpino/teraout.png

21 Nov 2017

Guys, i cant find this module on cool components anywhere. I dont know if they took it of the market or what. Does anyone know any other place to order the module from? That ships to Croatia. Thanks

Please log in to post comments.