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.

RedBearLab's BLE Nano

The BLE Nano is an Arduino compatible board that can run Arduino sketches and has BLE capabilities. We have them available for checkout in the ER. Get the ones that include the USB programmer board (itself an Arduino).

It requires that you have the Arduino Library for nRF51822 setup.

Arduino BLEPeripheral Library

The Arduino BLEPeripheral Library is very full featured and supports the BLE Nano so installing that is the next step.
cd ~/Documents/Arduino/libraries/
git clone https://github.com/sandeepmistry/arduino-BLEPeripheral BLEPeripheral		
		

BLE Nano and BLEPeripheral Library

Next, you'll run this example sketch to test. Use your phone with a BLE Scanner and the Arduino Serial monitor to see if it works.

If that works, we can move on to a more full featured example. Maria Paula and Tom Igoe put together a few examples online.

The LED Switch is a great starting point for a variety of apps. You may want to change the name blePeripheral.setDeviceName("something") to make it easier to find and change the LED pin to 13 and give it a shot.

Here is an example page for working with it on the PhoneGap 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 == "something") {
					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>