SHARE
TWEET

Untitled

kubbur Aug 16th, 2018 259 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4. #include <BLE2902.h>
  5. #include <string>
  6.  
  7. // See the following for generating UUIDs:
  8. // https://www.uuidgenerator.net/
  9.  
  10. bool connected = false;
  11.   BLEService *pService;
  12.   BLEService *pService1;
  13.   BLECharacteristic *reportChar1;
  14.   BLECharacteristic *reportChar2;
  15.   BLECharacteristic *reportChar3;
  16.   class MyCallbacks : public BLEServerCallbacks {
  17.     void onConnect(BLEServer* pServer){
  18.       connected=true;
  19.     }
  20.    
  21.     void onDisconnect(BLEServer* pServer){
  22.       connected=false;
  23.     }
  24.   };
  25.  
  26. void setup() {
  27.   Serial.begin(115200);
  28.   Serial.println("Starting BLE work!");
  29.  
  30.   BLEDevice::init("ESP32");
  31.   BLEServer *pServer = BLEDevice::createServer();
  32.   pServer->setCallbacks(new MyCallbacks());
  33.   pService = pServer->createService((uint16_t)0x180a);
  34.   pService1 = pServer->createService((uint16_t)0x1812, 30);
  35.   setupCharacteristics();
  36.  
  37.   pService->start();
  38.   pService1->start();
  39.   BLEAdvertising *pAdvertising = pServer->getAdvertising();
  40.   pAdvertising->setAppearance(961);
  41.   pAdvertising->addServiceUUID((uint16_t)0x1812);
  42.   pAdvertising->start();
  43.  
  44.   Serial.println("Characteristic defined! Now you can read it in your phone!");
  45. }
  46. uint8_t v[] = {0x0, 0x0, 0x0, 0x0,0x0,0x0,0x0,0x0};
  47.  
  48. void loop() {
  49.   // put your main code here, to run repeatedly:
  50.   delay(1000);
  51.   if(connected){
  52.     uint8_t a[] = {0x0, 0x0, random(0x04,0x26), 0x0,0x0,0x0,0x0,0x0};
  53.     reportChar1->setValue(a,sizeof(a));
  54.     reportChar1->notify();    
  55.  
  56.     reportChar1->setValue(v, sizeof(v));
  57.     reportChar1->notify();
  58.   }
  59. }
  60.  
  61. void setupCharacteristics() {
  62.  
  63.   BLECharacteristic *manufacturer = pService->createCharacteristic(
  64.                                        (uint16_t)0x2a29,
  65.                                        BLECharacteristic::PROPERTY_READ
  66.                                      );
  67.   std::string name = "espressif";
  68.   manufacturer->setValue(name);
  69.                                      
  70.   BLECharacteristic *pnpIDChar = pService->createCharacteristic(
  71.                                        (uint16_t)0x2a50,
  72.                                        BLECharacteristic::PROPERTY_READ
  73.                                      );
  74.   const uint8_t pnp[] = {0x01,0xe5,0x02,0xcd,0xab,0x01,0x00};
  75.   pnpIDChar->setValue((uint8_t*)pnp, sizeof(pnp));
  76.  
  77.   BLECharacteristic *hidInfoChar = pService1->createCharacteristic(
  78.                                        (uint16_t)0x2a4a,
  79.                                        BLECharacteristic::PROPERTY_READ
  80.                                      );
  81.   const uint8_t val1[] = {0x00,0x01,0x00,0x02};
  82.   hidInfoChar->setValue((uint8_t*)val1, 4);
  83.  
  84.   BLECharacteristic *reportMapChar = pService1->createCharacteristic(
  85.                                        (uint16_t)0x2a4b,
  86.                                        BLECharacteristic::PROPERTY_READ
  87.                                      );
  88.   const uint8_t val2[] = {
  89.     0x05,0x01,0x09,0x06,0xA1,0x01,0x05,0x07,
  90.     0x19,0xE0,0x29,0xE7,0x15,0x00,0x25,0x01,
  91.     0x75,0x01,0x95,0x08,0x81,0x02,0x95,0x01,
  92.     0x75,0x08,0x81,0x01,0x95,0x05,0x75,0x01,
  93.     0x05,0x08,0x19,0x01,0x29,0x05,0x91,0x02,
  94.     0x95,0x01,0x75,0x03,0x91,0x01,0x95,0x06,
  95.     0x75,0x08,0x15,0x00,0x25,0x65,0x05,0x07,
  96.     0x19,0x00,0x29,0x65,0x81,0x00,0xC0}; //TODO
  97.   reportMapChar->setValue((uint8_t*)val2, sizeof(val2));
  98.  
  99.   BLECharacteristic *hidControlChar = pService1->createCharacteristic(
  100.                                        (uint16_t)0x2a4c,
  101.                                        BLECharacteristic::PROPERTY_WRITE_NR
  102.                                      );
  103.  
  104.   reportChar1 = pService1->createCharacteristic(
  105.                                        (uint16_t)0x2a4d,
  106.                                        BLECharacteristic::PROPERTY_READ   |
  107.                                        BLECharacteristic::PROPERTY_NOTIFY                                      
  108.                                      );
  109.   BLEDescriptor *desc1 = new BLEDescriptor(BLEUUID((uint16_t)0x2908));
  110.   const uint8_t desc1_val[] = {0x01, 0};
  111.   desc1->setValue((uint8_t*)desc1_val, 1);
  112.   reportChar1->addDescriptor(desc1);
  113.   reportChar1->addDescriptor(new BLE2902());
  114.  
  115.   reportChar2 = pService1->createCharacteristic(
  116.                                        (uint16_t)0x2a4d,
  117.                                        BLECharacteristic::PROPERTY_READ   |
  118.                                        BLECharacteristic::PROPERTY_WRITE                                      
  119.                                      );
  120.   BLEDescriptor *desc2 = new BLEDescriptor(BLEUUID((uint16_t)0x2908));
  121.   const uint8_t desc2_val[] = {0x02, 0};
  122.   desc2->setValue((uint8_t*)desc2_val, 1);
  123.   reportChar2->addDescriptor(desc2);
  124.  
  125.   reportChar3 = pService1->createCharacteristic(
  126.                                        (uint16_t)0x2a4d,
  127.                                        BLECharacteristic::PROPERTY_READ     |
  128.                                        BLECharacteristic::PROPERTY_WRITE    |
  129.                                        BLECharacteristic::PROPERTY_WRITE_NR
  130.                                      );
  131.   BLEDescriptor *desc3 = new BLEDescriptor(BLEUUID((uint16_t)0x2908));
  132.   const uint8_t desc3_val[] = {0x03, 0};
  133.   desc3->setValue((uint8_t*)desc3_val, 1);
  134.   reportChar3->addDescriptor(desc3);
  135.  
  136.   BLECharacteristic *protocolModeChar = pService1->createCharacteristic(
  137.                                        (uint16_t)0x2a4e,
  138.                                        BLECharacteristic::PROPERTY_WRITE_NR
  139.                                      );
  140.  
  141.   BLECharacteristic *bootInputChar = pService1->createCharacteristic(
  142.                                        (uint16_t)0x2a22,
  143.                                        BLECharacteristic::PROPERTY_NOTIFY
  144.                                      );
  145.   bootInputChar->addDescriptor(new BLE2902());
  146.  
  147.  
  148.   BLECharacteristic *bootOutputChar = pService1->createCharacteristic(
  149.                                        (uint16_t)0x2a32,
  150.                                        BLECharacteristic::PROPERTY_READ     |
  151.                                        BLECharacteristic::PROPERTY_WRITE    |
  152.                                        BLECharacteristic::PROPERTY_WRITE_NR
  153.                                      );
  154.  
  155.  
  156.  
  157. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top