Always On, Always Connected

Building Mobile Accessories with Arduino and BLE

A variety of BLE modules that either work with Arduino (such as a shield) or are Arduino's themselves have begun to come on the market. This makes it easy for us to start prototyping phone connected hardware or accessories.

Arduino 101

The Arduino 101 comes with on-board BLE capabilities and is a great one for getting started with. Of course, a wide variety of other Arduino BLE devices such as RedBearLab's BLE Nano (which we have available for checkout) are available as well.

Arduino BLEPeripheral Library

The Arduino BLEPeripheral Library is the standard for BLE on Arduino is very full featured and supports the BLE Nano among others. The Arduino 101 has it's own BLE library that is "99% compatible" with the BLEPeripheral Library called "CurieBLE".

The ButtonLED or LED Switch Example which comes with the CurieBLE library or can be found in Tom Igoe's BLE Examples for other boards shows the basics of working with BLE on the Arduino and is a great starting point for a variety of apps.

Here is an example page for working with it on the Cordova side. Don't forget to install Don Coleman's BLE Central Plugin

<!DOCTYPE html>
<html>
    <head>
 <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript">

			var led = {
				service: "19b10010-e8f2-537e-4f6c-d104768a1214",
				data: "19b10011-e8f2-537e-4f6c-d104768a1214"
			};
			
			var peripheralId = null;

			function onPageLoad() {
				document.addEventListener("deviceready", onDeviceReady, false);
			}

			function onDeviceReady() {
				ble.scan([led.service], 10, scanSuccess, scanFailure);
			}

			function scanSuccess(peripheral) {
				// if you get more than one, you can decide based on name						
				// name will be cached on iOS from the original, on ble nano that is "Arduino"
				// you can reset on the device with setDeviceName("something"); but it won't update the iOS cache
				if (peripheral.name == "ButtonLED") {
					ble.stopScan(stopScanSuccess, stopScanFailure);
					console.log(peripheral);
					console.log("Connecting");
					ble.connect(peripheral.id, connectSuccess, connectFailure);
				}
			}

			function scanFailure(err) {
				console.log(err);
			}
			
			function stopScanSuccess() {
				console.log("Stop Scan Success");
			}
			
			function stopScanFailure() {
				console.log("Stop Scan Failure");
			}
			
			function connectSuccess(peripheral) {
				console.log("Connected");
				console.log(peripheral);
				
				peripheralId = peripheral.id;
				//console.log(foundPeripheral);

				// Do a read
				ble.read(peripheral.id, led.service, led.data, readSuccess, readFailure);
			}
			
			function connectFailure() {
				console.log("Connection Failed");
			}
			
			
			function readSuccess(data) {
				console.log(data)
				document.getElementById('data').innerHTML = data;
			}
		
			
			function readFailure(err) {
				console.log(err);
			}

			function writeData(data) {
				var configData = new Uint8Array(1);
				configData[0] = data;

				ble.write(peripheralId, led.service, led.data, configData.buffer, 
				function() { console.log("Wrote: " + data); },function() { console.log("Error");});
			}
                
        </script>
    </head>
    <body onload="onPageLoad()">
	<div id="data"></div>
		<button onclick="writeData(1);">Write 1</button>
		<button onclick="writeData(0);">Write 0</button>
    </body>
</html>