A comprehensive Java library for interfacing with Arduino boards through serial, Bluetooth, and Ethernet connections
Android Bluetooth communication enabling Arduino control from mobile applications. The Bluetooth module provides seamless Arduino communication over Bluetooth connections with automatic device pairing, connection management, and the same reliable messaging protocol used in serial communication.
Main class for Bluetooth communication with Arduino from Android applications, implementing the observer pattern for asynchronous message handling.
class AndroidBluetooth4JArduino implements JArduinoClientObserver, JArduinoSubject, Runnable {
// Message framing constants (same as serial)
static final byte START_BYTE = 0x12;
static final byte STOP_BYTE = 0x13;
static final byte ESCAPE_BYTE = 0x7D;
// Constructors
AndroidBluetooth4JArduino(AndroidBluetoothConfiguration myConf);
AndroidBluetooth4JArduino(ProtocolConfiguration myConf);
// Configuration methods
void setAndroidBluetoothSocket(BluetoothSocket socket);
// Communication methods
void receiveMsg(byte[] msg);
void register(JArduinoObserver observer);
void unregister(JArduinoObserver observer);
void run();
void close();
}Configuration class for Android Bluetooth connection setup.
class AndroidBluetoothConfiguration extends ProtocolConfiguration {
AndroidBluetoothConfiguration(BluetoothSocket socket);
BluetoothSocket getmSocket();
}
class ProtocolConfiguration {
// Base configuration class
}import org.sintef.jarduino.comm.AndroidBluetooth4JArduino;
import org.sintef.jarduino.comm.AndroidBluetoothConfiguration;
import org.sintef.jarduino.observer.JArduinoObserver;
import org.sintef.jarduino.*;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
public class AndroidBluetoothExample extends Activity {
private AndroidBluetooth4JArduino arduino;
private BluetoothSocket socket;
private void connectToArduino() {
try {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = adapter.getRemoteDevice("00:11:22:33:44:55"); // Arduino Bluetooth MAC
// Create Bluetooth socket
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // Serial Port Profile
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
// Create configuration and Arduino connection
AndroidBluetoothConfiguration config = new AndroidBluetoothConfiguration(socket);
arduino = new AndroidBluetooth4JArduino(config);
// Register observer for Arduino responses
arduino.register(new JArduinoObserver() {
@Override
public void receiveMsg(byte[] msg) {
FixedSizePacket packet = JArduinoProtocol.createMessageFromPacket(msg);
Log.d("Arduino", "Response: " + packet.toString());
// Handle specific message types
if (packet instanceof AnalogReadResultMsg) {
AnalogReadResultMsg result = (AnalogReadResultMsg) packet;
// Update UI with sensor value
}
}
});
// Start communication thread
Thread bluetoothThread = new Thread(arduino);
bluetoothThread.start();
} catch (IOException e) {
Log.e("Bluetooth", "Connection failed", e);
}
}
private void controlArduino() {
// Set pin mode
FixedSizePacket pinMode = JArduinoProtocol.createPinMode(DigitalPin.PIN_13, PinMode.OUTPUT);
arduino.receiveMsg(pinMode.getPacket());
// Blink LED
FixedSizePacket on = JArduinoProtocol.createDigitalWrite(DigitalPin.PIN_13, DigitalState.HIGH);
arduino.receiveMsg(on.getPacket());
// Read sensor
FixedSizePacket analogRead = JArduinoProtocol.createAnalogRead(AnalogPin.A_0);
arduino.receiveMsg(analogRead.getPacket());
}
@Override
protected void onDestroy() {
super.onDestroy();
if (arduino != null) {
arduino.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
Log.e("Bluetooth", "Error closing socket", e);
}
}
}
}import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class BluetoothDeviceDiscovery {
private BluetoothAdapter bluetoothAdapter;
private List<BluetoothDevice> discoveredDevices = new ArrayList<>();
public void startDiscovery() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Register broadcast receiver for device discovery
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(deviceDiscoveryReceiver, filter);
// Start discovery
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver deviceDiscoveryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Look for Arduino devices (typically named "HC-05", "HC-06", etc.)
String deviceName = device.getName();
if (deviceName != null && (deviceName.contains("HC-") || deviceName.contains("Arduino"))) {
discoveredDevices.add(device);
// Update UI with found Arduino device
}
}
}
};
}// If you already have a BluetoothSocket from elsewhere
BluetoothSocket existingSocket = // ... obtained from your Bluetooth setup
// Create Arduino communication directly
AndroidBluetooth4JArduino arduino = new AndroidBluetooth4JArduino(
new AndroidBluetoothConfiguration(existingSocket)
);
// Or set socket after creation
arduino.setAndroidBluetoothSocket(existingSocket);Add these permissions to your Android manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />The JArduino project includes complete Android sample applications:
These samples demonstrate:
Bluetooth communication uses the same reliable binary framing protocol as serial communication:
This ensures consistent message handling across all communication transports.
The AndroidBluetooth4JArduino class implements Runnable for background communication:
receiveMsg() methodCommon Bluetooth issues handled by the implementation:
org.sintef.jarduino.core for protocol and message classesandroid.bluetooth.* for Bluetooth socket communicationorg.kevoree.extra.osgi.rxtx for additional serial port utilitiesInstall with Tessl CLI
npx tessl i tessl/maven-org-sintef-jarduino--org-sintef-jarduino