Bluetooth Connection
In this tutorial we show how to communicate using Bluetooth, there are simple and inexpensive modules that can be connected to the Arduino / similar.
Will not be required to use any particular library, because these modules operate using a Serial communication
Requirements
If you are using Linux, you must install the following dependencies:
sudo apt-get install bluetooth libbluetooth-dev bluez bluez-tools
Scan bluetooth devices (linux):
hcitool scan
Hardware Setup:
JY-MCU Bluetooth Module. It is a cheap Bluetooth module. (see HC-06 pinout) (I got from here)
The JY-MCU module communicates with the Arduino via a serial connection. It has four pins that we will be using:
- VCC is used to power the module. It needs to be connected to the Arduino 5v pin.
- GND is the ground pin. It needs to be connected to the Arduino Ground pin
- TXD is used to send data from the module to the Arduino. It needs to be connected to the serial receive pin (RX) of the Arduino, which is pin 0 in case of the Uno. If you are using a different Arduino board, check its schematics to make sure you have the right pin.
- RXD is used to receive data from the Arduino. It needs to be connected to the the Arduino serial transmit pin (TX) , which is pin 1 in the case of Arduino Uno.
- We will not be using the “STATE” and the “KEY” pins of the Bluetooth module, so they will not be connected.
If you have hardware like LaunchPad see the session: EnergiaIDE
On this module, as you can see from the photo above, the TX line is rated at 3.3V. This means that even though we can power the module with 5 volts from the Arduino, the communication lines from and to the module are supposed to be at 3.3 volts.
Receiving data from the Arduino is where we need to do more work. The RX line of the Arduino Uno is running at 5V, so we will be sending a higher voltage on the Bluetooth’s module RX line, than suggested by the label. This may damage the Bluetooth module permanently!
I'm not using any voltage divider and is working properly, but if you do not want to risk burning your module, see this:
Other option:
Upload example to Arduino.
Use BluetoothConnection from Examples menu of Arduino IDE.
The example is virtually identical of USB example.
Before uploading to the arduino, disconnect bluetooth module pins 0 (RX) and 1 (TX), as they are the same ones used by the USB communication and you may have problems if you do not use the voltage divider
If you are using Leonardo, use: ODev.begin(Serial1, 9600);
Connection with JAVA.
Pair Devices
Pair the module with your PC (or Android/Raspberry). The defaults for the Bluetooth module are:
Default Name: linvor
Default Pairing code: 1234
On some operating systems when you run the JAVA example, will open a pairing dialog.
If it does not you must add the device manually (run: bluetooth-wizard ) or from networks/bluetooth manager
Auto Paring (linux):
bluetooth-agent 1234 &
Basic Usage
Use the address found and add an output connection:
addOutput(Connections.out.bluetooth("00:13:03:14:19:07"));
Or: connect(Connections.out.bluetooth("00:11:06:14:04:57"));
You can also automatically connect to the first available device:
Note that this will be very slow because it will do the scan and will take the first bluetooth device found (can be up to your phone!)
connect(Connections.out.bluetooth());
public class BlinkDeviceDemo extends SimpleDeviceManager { public static void main(String[] args) throws Exception { new BlinkDeviceDemo(); } public BlinkDeviceDemo() throws Exception { Device led = new Device(1, Device.DIGITAL); connect(Connections.out.bluetooth("00:11:06:14:04:57")); while(true){ led.on(); delay(500); led.off(); delay(500); } } }
Basically what we need to change is the type of connection to bluetooth
Alternative Hardware Setup
If you need to use other Arduino ports, you can use the SoftwareSerial library, is an example:
Use SoftwareSerial with Bluetooth in order to make it easier to program your Arduino and debug your code when using the Bluetooth module.
Bluetooth------------ Arduino GND ------ GND 3.3 do not connect 5.0 ------ 5V RXD ------ D11 TXD ------ D10 KEY do not connect
Software / Firmware
You need use: BluetoothConnectionSoft example.
// You need to make only those changes: #include <SoftwareSerial.h> void setup{ ODev.begin(9600, 10, 11); }
In the Java side, you not need change anything
Energia IDE (LaunchPad Hardware)
The Energy IDE is an adaptation of the Arduino libraries to work with the hardware of the LaunchPad. See the supported platforms at:
http://energia.nu/
https://github.com/energia/Energia
In this example we use: LM4F120
Hardware Setup
Software / Firmware
You need use: EnergiaLaunchPadBasic example.
Change to: Serial1 (Hardware UART on PC4, PC5 pins)
DeviceConnection deviceConnection(Serial1);
In the Java side, you not need change anything
References
- http://mcuoneclipse.com/2013/06/19/using-the-hc-06-bluetooth-module/
- http://www.heatxsink.com/entry/how-to-pair-a-bluetooth-device-from-command-line-on-linux (pair and connect)
- Hardware Manual & AT Commands Reference Manual
- http://wiki.openmoko.org/wiki/Manually_using_Bluetooth