An example of using the Nordic UART service on Bluetooth LE (BLE) from Processing's Android Mode. The Android app acts as a BLE client to a BLE server (peripheral) with the Nordic UART service - for instance, a Bluetooth LE Module or SoC like the ESP-32.
Made with Processing 3.3.6, Android Mode 4.0.1 Tested on a Moto G2 running Android 7.1.2 (custom ROM), with a Sparkfun ESP-32 Thing running a Nordic UART Arduino example.
The example is currently missing code for enabling location and Bluetooth from within the app. This is pending on an open issue.
The BleUART class wraps the Bluetooth LE functionality for scanning, connecting and exchanging data with a BLE server running the Nordic UART service.
- Create and instantiate a BleUART object:
BleUART bleUart = new BleUART(this);
- Before initializing the object (see next step), make sure that
- The device's Bluetooth is on.
- The device's location services are on (battery saving or coarse location should be enough).
- permission for
Access Coarse Location
has been explicitly granted by the user.
- Initialize the BleUART object:
bleUart.init();
- If you have a fixed device you want to connect to, you can call:
bleUart.connectTo("F0:12:34:56:78:9A");
passing the device's MAC address as aString
. In this case, the BleUART will attempt to find (i.e. scan for) and connect to the device automatically. Skip to step 11. - Otherwise, call:
bleUart.startScanning();
You can pass the scan timeout in milliseconds as anint
, otherwise the default timeout of 5000 (5 seconds) is used. - Every time a device is found, BleUART will call the method
void bleUARTDeviceDiscovered(String name, String address);
which you may implement in your sketch. - When scanning is finished, BleUART will call the method
void bleUARTScanningFinished();
which you may implement in your sketch. - You can also explicitly stop scanning by calling
bleUart.stopScanning()
. Scanning will also be stopped if you callbleUart.connectTo(...)
. - You can get an
ArrayList
of devices (name and address) with:ArrayList<BLEDeviceSimple> knownDevices = bleUart.getDeviceList();
You can get the name and address of aBLEDeviceSimple
object usinggetName()
orgetAddress()
. - Connect to a known device by passing the address or the
BLEDeviceSimple
object: bleUart.connectTo( ... ); - On a successful connection, BleUART will call the method
void bleUARTConnected();
which you may implement in your sketch. - If the connection fails (or later, if the device is disconnected) , BleUART will call the method
void bleUARTDisconnected();
which you may implement in your sketch. - To send a message via the UART, use:
bleUart.sendMessage("Hello World");
The BleUART is set-up to allow messages of up to 512 bytes (that's 512 "simple" characters). Everything else will be truncated. - When a message is received, BleUART will call the method
bleUARTMessageReceived(String message);
which you may implement in your sketch. Avoid doing complicated logic here. It's best to store the message in another variable and process it during thedraw()
loop. - When cleaning up - for instance, on
stop()
- dispose of the BleUART by callingbleUart.dispose();