Last active
August 29, 2019 06:02
-
-
Save KorkyraBoyCRO/c3dd2ec1d8d79189ff31 to your computer and use it in GitHub Desktop.
ancs with display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import libraries (BLEPeripheral depends on SPI) | |
#include <SPI.h> | |
#include <BLEPeripheral.h> | |
#include <Adafruit_SH1106.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
//Uncomment this block to use hardware SPI | |
#define OLED_DC 6 | |
#define OLED_CS 7 | |
#define OLED_RESET 8 | |
Adafruit_SH1106 display(OLED_DC, OLED_RESET, OLED_CS); | |
#include <BLEUtil.h> | |
// define pins (varies per shield/board) | |
#define BLE_REQ 10 | |
#define BLE_RDY 2 | |
#define BLE_RST 9 | |
// create peripheral instance, see pinouts above | |
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST); | |
BLEBondStore bleBondStore; | |
// remote services | |
BLERemoteService ancsService = BLERemoteService("7905f431b5ce4e99a40f4b1e122d00d0"); | |
// remote characteristics | |
BLERemoteCharacteristic ancsNotificationSourceCharacteristic = BLERemoteCharacteristic("9fbf120d630142d98c5825e699a21dbd", BLENotify); | |
//BLERemoteCharacteristic ancsControlPointCharacteristic = BLERemoteCharacteristic("69d1d8f345e149a898219bbdfdaad9d9", BLEWrite); | |
//BLERemoteCharacteristic ancsDataSourceCharacteristic = BLERemoteCharacteristic("22eac6e924d64bb5be44b36ace7c7bfb", BLENotify); | |
void setup() { | |
Serial.begin(9600); | |
display.begin(SH1106_SWITCHCAPVCC); | |
display.clearDisplay(); | |
#if defined (__AVR_ATmega32U4__) | |
while(!Serial); | |
#endif | |
// clears bond data on every boot | |
bleBondStore.clearData(); | |
blePeripheral.setBondStore(bleBondStore); | |
blePeripheral.setServiceSolicitationUuid(ancsService.uuid()); | |
blePeripheral.setLocalName("Jaksa's BLE"); | |
// set device name and appearance | |
blePeripheral.setDeviceName("Arduino ANCS"); | |
blePeripheral.setAppearance(0x0080); | |
blePeripheral.addRemoteAttribute(ancsService); | |
blePeripheral.addRemoteAttribute(ancsNotificationSourceCharacteristic); | |
// blePeripheral.addRemoteAttribute(ancsControlPointCharacteristic); | |
// blePeripheral.addRemoteAttribute(ancsDataSourceCharacteristic); | |
// assign event handlers for connected, disconnected to peripheral | |
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); | |
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); | |
blePeripheral.setEventHandler(BLEBonded, blePeripheralBondedHandler); | |
blePeripheral.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); | |
// assign event handlers for characteristic | |
ancsNotificationSourceCharacteristic.setEventHandler(BLEValueUpdated, ancsNotificationSourceCharacteristicValueUpdated); | |
// ancsDataSourceCharacteristic.setEventHandler(BLEValueUpdated, ancsDataSourceCharacteristicCharacteristicValueUpdated); | |
// begin initialization | |
blePeripheral.begin(); | |
Serial.println(F("BLE Peripheral - ANCS")); | |
} | |
void loop() { | |
blePeripheral.poll(); | |
} | |
void blePeripheralConnectHandler(BLECentral& central) { | |
// central connected event handler | |
Serial.print(F("Connected event, central: ")); | |
Serial.println(central.address()); | |
} | |
void blePeripheralDisconnectHandler(BLECentral& central) { | |
// central disconnected event handler | |
Serial.print(F("Disconnected event, central: ")); | |
Serial.println(central.address()); | |
} | |
void blePeripheralBondedHandler(BLECentral& central) { | |
// central bonded event handler | |
Serial.print(F("Remote bonded event, central: ")); | |
Serial.println(central.address()); | |
if (ancsNotificationSourceCharacteristic.canSubscribe()) { | |
ancsNotificationSourceCharacteristic.subscribe(); | |
} | |
} | |
void blePeripheralRemoteServicesDiscoveredHandler(BLECentral& central) { | |
// central remote services discovered event handler | |
Serial.print(F("Remote services discovered event, central: ")); | |
Serial.println(central.address()); | |
if (ancsNotificationSourceCharacteristic.canSubscribe()) { | |
ancsNotificationSourceCharacteristic.subscribe(); | |
} | |
} | |
enum AncsNotificationEventId { | |
AncsNotificationEventIdAdded = 0, | |
AncsNotificationEventIdModified = 1, | |
AncsNotificationEventIdRemoved = 2 | |
}; | |
enum AncsNotificationEventFlags { | |
AncsNotificationEventFlagsSilent = 1, | |
AncsNotificationEventFlagsImportant = 2, | |
AncsNotificationEventFlagsPositiveAction = 4, | |
AncsNotificationEventFlagsNegativeAction = 8 | |
}; | |
enum AncsNotificationCategoryId { | |
AncsNotificationCategoryIdOther = 0, | |
AncsNotificationCategoryIdIncomingCall = 1, | |
AncsNotificationCategoryIdMissedCall = 2, | |
AncsNotificationCategoryIdVoicemail = 3, | |
AncsNotificationCategoryIdSocial = 4, | |
AncsNotificationCategoryIdSchedule = 5, | |
AncsNotificationCategoryIdEmail = 6, | |
AncsNotificationCategoryIdNews = 7, | |
AncsNotificationCategoryIdHealthAndFitness = 8, | |
AncsNotificationCategoryIdBusinessAndFinance = 9, | |
AncsNotificationCategoryIdLocation = 10, | |
AncsNotificationCategoryIdEntertainment = 11 | |
}; | |
struct AncsNotification { | |
unsigned char eventId; | |
unsigned char eventFlags; | |
unsigned char catergoryId; | |
unsigned char catergoryCount; | |
unsigned long notificationUid; | |
}; | |
void ancsNotificationSourceCharacteristicValueUpdated(BLECentral& central, BLERemoteCharacteristic& characteristic) { | |
Serial.println(F("ANCS Notification Source Value Updated:")); | |
struct AncsNotification notification; | |
memcpy(¬ification, characteristic.value(), sizeof(notification)); | |
display.setTextSize(1); | |
display.setCursor(0,0); | |
Serial.print("\tEvent ID: "); | |
Serial.println(notification.eventId); | |
Serial.print("\tEvent Flags: 0x"); | |
Serial.println(notification.eventFlags, HEX); | |
//Serial.print("\tCategory ID: "); | |
//Serial.println(notification.catergoryId); | |
Serial.print("\tCategory Count: "); | |
Serial.println(notification.catergoryCount); | |
Serial.print("\tNotification UID: "); | |
Serial.println(notification.notificationUid); | |
switch(notification.catergoryId){ | |
case 0: | |
display.println(" Other "); | |
break; | |
case 1: | |
display.println("Incoming call... "); | |
break; | |
case 2: | |
display.println(" ***MissedCall*** "); | |
break; | |
case 3: | |
display.println(" Voicemail "); | |
break; | |
case 4: | |
display.println(" Message "); | |
break; | |
case 5: | |
display.println(" Schedule!!! "); | |
break; | |
case 6: | |
display.println(" Email! "); | |
break; | |
case 7: | |
display.println(" News "); | |
break; | |
case 8: | |
display.println(" Health And Fitness "); | |
break; | |
case 9: | |
display.println(" Business And Finance "); | |
break; | |
case 10: | |
display.println(" Location "); | |
break; | |
case 11: | |
display.println(" Entertainment "); | |
break; | |
} | |
// BLEUtil::printBuffer(characteristic.value(), characteristic.valueLength()); | |
} | |
/* | |
void ancsDataSourceCharacteristicCharacteristicValueUpdated(BLECentral& central, BLERemoteCharacteristic& characteristic) { | |
Serial.print(F("ANCS Data Source Value Updated: ")); | |
BLEUtil::printBuffer(characteristic.value(), characteristic.valueLength()); | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment