Advertisement
Krzyspx

iTAG press button detection 2

May 14th, 2018
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. /**
  2.   iTAG service - press button detection by notify
  3.   http://100-x-arduino.blogspot.com/
  4.   https://pastebin.com/FBERqycw
  5. */
  6.  
  7. #include "BLEDevice.h"
  8.  
  9. static BLEUUID serviceUUID("0000ffe0-0000-1000-8000-00805f9b34fb"); // The remote service we wish to connect to.
  10. static BLEUUID charUUID("0000ffe1-0000-1000-8000-00805f9b34fb"); // The characteristic of the remote service we are interested in.
  11.  
  12. static BLEAddress *pServerAddress;
  13. static boolean doConnect = false;
  14. static boolean connected = false;
  15. static BLERemoteCharacteristic* pRemoteCharacteristic;
  16. static  BLEClient*  pClient;
  17. bool deviceBleConnected;
  18.  
  19. class MyClientCallbacks: public BLEClientCallbacks {
  20.     void onConnect(BLEClient *pClient) {
  21.       deviceBleConnected = true;                                                  // set ble connected flag
  22.       Serial.println("connected");
  23.     };
  24.  
  25.     void onDisconnect(BLEClient *pClient) {
  26.       pClient->disconnect();
  27.       deviceBleConnected = false;                                                 // clear ble connected flag
  28.       Serial.println("disconnected");
  29.     }
  30. };
  31.  
  32. static void notifyCallback(
  33.   BLERemoteCharacteristic* pBLERemoteCharacteristic,
  34.   uint8_t* pData,
  35.   size_t length,
  36.   bool isNotify) {
  37.   Serial.println(); Serial.print("Notify from  ");   Serial.println("black iTAG"); // Serial.print(" length ");  Serial.println(length);
  38.  
  39. }
  40.  
  41. bool connectToServer(BLEAddress pAddress) {
  42.  
  43.   // BLEClient*  pClient  = BLEDevice::createClient();  Serial.println(" Created client");
  44.   pClient  = BLEDevice::createClient();  Serial.println(" Created client");
  45.  
  46.   pClient->setClientCallbacks(new MyClientCallbacks());
  47.  
  48.   pClient->connect(pAddress);  Serial.println(" Connected to server");
  49.  
  50.   BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  51.   if (pRemoteService == nullptr) {
  52.     Serial.print("Failed to find our service UUID: ");    return false;
  53.   }
  54.   Serial.println("Found service " + String(pRemoteService->toString().c_str())); //  Serial.println(pRemoteService->toString().c_str());
  55.  
  56.   pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  57.   if (pRemoteCharacteristic == nullptr) {
  58.     Serial.print("Failed to find our characteristic UUID: ");    return false;
  59.   }
  60.   Serial.println("Found characteristic" + String(pRemoteCharacteristic->toString().c_str()));
  61.  
  62.   std::string value = pRemoteCharacteristic->readValue();  // Read the value of the characteristic.
  63.   Serial.print("The characteristic value was: ");  Serial.println(value.c_str());
  64.  
  65.  
  66.   const uint8_t bothOff[]        = {0x0, 0x0};
  67.   const uint8_t notificationOn[] = {0x1, 0x0};
  68.   const uint8_t indicationOn[]   = {0x2, 0x0};
  69.   const uint8_t bothOn[]         = {0x3, 0x0};
  70.  
  71.   pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true); //notification ON
  72.  
  73.   pRemoteCharacteristic->registerForNotify(notifyCallback); // Callback function when notification
  74. }
  75.  
  76. void setup() {
  77.   Serial.begin(115200);
  78.  
  79.   BLEDevice::init(""); Serial.println("Starting Arduino BLE Client application...");
  80.   //  pServerAddress = new BLEAddress("ff:ff:c2:0e:e0:e5"); //iTAG green
  81.   pServerAddress = new BLEAddress("fc:58:fa:05:a4:62"); //iTAG black
  82.   //pServerAddress = new BLEAddress("fc:58:fa:04:cb:03"); //iTAG pink
  83.   Serial.print("my adrress  ");   Serial.println(pServerAddress->toString().c_str());
  84. } // End of setup.
  85.  
  86.  
  87. void loop() {
  88.  
  89.   if (connected == false) {
  90.     if (connectToServer(*pServerAddress)) {
  91.       connected = true; Serial.println("Connected to the BLE Server.");
  92.     } else   Serial.println("No connection ..");
  93.   }
  94.  
  95.  // int ifconn = pClient->isConnected();
  96.  // Serial.println("connection ?.." + String(ifconn));
  97.  
  98.   if (connected) {
  99.     Serial.print(".");
  100.     String newValue = "T";
  101.     pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());     // Set the characteristic's value to be the array of bytes that is actually a string.
  102.   }
  103.   delay(1000);
  104. } // End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement