In this example 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 that can be purchased from many retailers online, or from eBay. . (see HC-06 pinout) (I got mine from here)
The JY-MCU module communicates with the Arduino via a serial connection. It has four pins that we will be using:
...
Info |
---|
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
Info |
---|
If you are using Leonardo, use: ODev.begin(Serial1, 9600); |
Connection with JAVA.
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
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
Tip |
---|
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());
Code Block | ||||
---|---|---|---|---|
| ||||
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.
Code Block |
---|
Bluetooth------------ Arduino GND ------ GND 3.3 do not connect 5.0 ------ 5V RXD ------ D11 TXD ------ D10 KEY do not connect |
...
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
...
Code Block | ||||
---|---|---|---|---|
| ||||
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