Skip to content

Instantly share code, notes, and snippets.

@sowbug
Created September 1, 2018 18:09
Show Gist options
  • Save sowbug/46475937eeb1ec7be3573bcccef84da8 to your computer and use it in GitHub Desktop.
Save sowbug/46475937eeb1ec7be3573bcccef84da8 to your computer and use it in GitHub Desktop.
#include <BLEAddress.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <U8x8lib.h>
int scanTime = 15;
// https://thejeshgn.com/2017/06/20/reverse-engineering-itag-bluetooth-low-energy-button/
// https://medium.com/@monkeytypewritr/2-bluetooth-tags-and-tangible-uis-for-iot-47599869a7fb
static BLEUUID serviceUUID("0000ffe0-0000-1000-8000-00805f9b34fb");
static BLEUUID characteristicUUID("0000ffe1-0000-1000-8000-00805f9b34fb");
bool found = false;
BLEAddress *foundAddress;
BLERemoteCharacteristic* pRemoteCharacteristic;
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(15, 4, 16);
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
u8x8.print("-> ");
u8x8.print(advertisedDevice.getAddress().toString().c_str());
if (advertisedDevice.getName() == "iTAG ") {
u8x8.print("YES\r\n");
found = true;
foundAddress = new BLEAddress(advertisedDevice.getAddress());
}
}
};
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
u8x8.clear();
u8x8.print(millis());
u8x8.print(": ");
u8x8.print(pData[0]);
}
void setup() {
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.setCursor(0, 0);
u8x8.print("Scanning...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
BLEScanResults foundDevices = pBLEScan->start(scanTime);
u8x8.clear();
u8x8.print("Found: ");
u8x8.print(foundDevices.getCount());
u8x8.print("\r\n");
if (found) {
u8x8.print("iTAG OK\r\n");
BLEClient* pClient = BLEDevice::createClient();
pClient->connect(*foundAddress);
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
u8x8.print("No service UUID\r\n");
return;
}
u8x8.print("S OK\r\n");
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(characteristicUUID);
if (pRemoteCharacteristic == nullptr) {
u8x8.print("No characteristic UUID\r\n");
return;
}
u8x8.print("C OK\r\n");
pRemoteCharacteristic->registerForNotify(notifyCallback);
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment