What are the current options for supporting Bluetooth Low Energy (BLE)?

It does work! That’s an Electron (2G) on an AssetTracker, connected to an Adafruit Bluefruit LE SPI. I’m not sure what the use case for this contraption is, but it works. It’s the standard AssetTracker library with my port of the BLE library (Adafruit_BLE in the community libraries, or https://github.com/rickkas7/Adafruit_BLE). I used the Adafruit Bluefruit iOS phone app in UART mode and it prints the location from the asset tracker to the phone via Bluetooth. Neat!


#include "Particle.h"

#include "AssetTracker/AssetTracker.h"

#include "Adafruit_BLE/Adafruit_BLE.h"
#include "Adafruit_BLE/Adafruit_BluefruitLE_SPI.h"

#define BUFSIZE                        128   // Size of the read buffer for incoming data
#define VERBOSE_MODE                   true  // If set to 'true' enables debug output

#define BLUEFRUIT_SPI_CS               D4  	// Note: LIS3DH on the AssetTracker uses A2, don't use that!
#define BLUEFRUIT_SPI_IRQ              D3
#define BLUEFRUIT_SPI_RST              D2    // Optional but recommended, set to -1 if unused
// SPI: SCK=A3, MISO=A4, MOSI=A5
Adafruit_BluefruitLE_SPI ble(&SPI, BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

enum BluetoothState { BLUETOOTH_CONNECT_WAIT, BLUETOOTH_CONNECTED };
BluetoothState bluetoothState = BLUETOOTH_CONNECT_WAIT;

// Creating an AssetTracker named 't' for us to reference
AssetTracker assetTracker = AssetTracker();

const unsigned long CHECK_PERIOD_MS = 10000;

unsigned long lastCheck = 0;


void setup() {
	Serial.begin(9600);

	// Enable Asset Tracker
    assetTracker.begin();
    assetTracker.gpsOn();


    if (ble.begin(VERBOSE_MODE)) {
    	ble.factoryReset();
    	ble.echo(false);

    	// Optional, print some information
    	ble.info();
    }
    else {
    	Serial.println("BLE initialization failed");
    }
}

void loop() {
    // You'll need to run this every loop to capture the GPS output
    assetTracker.updateGPS();

    switch(bluetoothState) {
    case BLUETOOTH_CONNECT_WAIT:
    	if (ble.isConnected()) {
    		Serial.println("BLE connected, switching to data mode");
    		ble.setMode(BLUEFRUIT_MODE_DATA);

    		bluetoothState = BLUETOOTH_CONNECTED;
    	}
    	break;

    case BLUETOOTH_CONNECTED:
    	if (!ble.isConnected()) {
    		Serial.println("BLE disconnected");
    		bluetoothState = BLUETOOTH_CONNECT_WAIT;
    		break;
    	}

    	break;

    }

    if (millis() - lastCheck >= CHECK_PERIOD_MS) {
    	lastCheck = millis();

    	String latLon = assetTracker.readLatLon();
    	Serial.println(latLon);

    	if (bluetoothState == BLUETOOTH_CONNECTED) {
    		ble.println(latLon.c_str());
    	}
    }

}

4 Likes