Samo Penic
2022-05-20 3e4d4c2df4f4be8447ce268f4de0688610c0bbda
commit | author | age
e0a3ca 1
SP 2 /** NimBLE_Server Demo:
3  *
4  *  Demonstrates many of the available features of the NimBLE server library.
5  *  
6  *  Created: on March 22 2020
7  *      Author: H2zero
8  * 
9 */
10
11 #include <NimBLEDevice.h>
12
13 static NimBLEServer* pServer;
14 NimBLECharacteristic* pBeefCharacteristic;
15 int counter;
16
17 /** Handler class for characteristic actions */
18 class CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
19     void onRead(NimBLECharacteristic* pCharacteristic){
20         counter++;
21         pCharacteristic->setValue(counter);
22         Serial.print(pCharacteristic->getUUID().toString().c_str());
23         Serial.print(": onRead(), value: ");
24         Serial.println(pCharacteristic->getValue().c_str());
25         
26     };
27
28
29 };
30     
31
32 static CharacteristicCallbacks chrCallbacks;
33
34
35 void setup() {
36     Serial.begin(115200);
37     Serial.println("Starting NimBLE Server");
38     counter = 0;
39     /** sets device name */
40     NimBLEDevice::init("Acell_Miha_1");
41
42     /** Optional: set the transmit power, default is 3db */
43     //NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
44     
45     /** Set the IO capabilities of the device, each option will trigger a different pairing method.
46      *  BLE_HS_IO_DISPLAY_ONLY    - Passkey pairing
47      *  BLE_HS_IO_DISPLAY_YESNO   - Numeric comparison pairing
48      *  BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing
49      */
50     //NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); // use passkey
51     //NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_YESNO); //use numeric comparison
52
53     /** 2 different ways to set security - both calls achieve the same result.
54      *  no bonding, no man in the middle protection, secure connections.
55      *   
56      *  These are the default values, only shown here for demonstration.   
57      */ 
58     //NimBLEDevice::setSecurityAuth(false, false, true); 
59     NimBLEDevice::setSecurityAuth(/*BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM |*/ BLE_SM_PAIR_AUTHREQ_SC);
60
61     pServer = NimBLEDevice::createServer();
62 //    pServer->setCallbacks(new ServerCallbacks());
63
64     NimBLEService* pDeadService = pServer->createService("DEAD");
65     pBeefCharacteristic = pDeadService->createCharacteristic(
66                                                "BEEF",
67                                                NIMBLE_PROPERTY::READ |
68                                                //NIMBLE_PROPERTY::WRITE |
69                                /** Require a secure connection for read and write access */
70                                                NIMBLE_PROPERTY::READ_ENC   // only allow reading if paired / encrypted
71                                                //NIMBLE_PROPERTY::WRITE_ENC   // only allow writing if paired / encrypted
72                                               );
73   
74     pBeefCharacteristic->setValue(counter);
75     pBeefCharacteristic->setCallbacks(&chrCallbacks);
76
77     /** 2904 descriptors are a special case, when createDescriptor is called with
78      *  0x2904 a NimBLE2904 class is created with the correct properties and sizes.
79      *  However we must cast the returned reference to the correct type as the method
80      *  only returns a pointer to the base NimBLEDescriptor class.
81      */
82 //    NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor("2904"); 
83 //    pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
84
85    /** Note a 0x2902 descriptor MUST NOT be created as NimBLE will create one automatically
86      *  if notification or indication properties are assigned to a characteristic.
87      */
88
89     /** Custom descriptor: Arguments are UUID, Properties, max length in bytes of the value */
90   NimBLEDescriptor* pBeef2904 = pBeefCharacteristic->createDescriptor(
91                                                "C01D",
92                                                NIMBLE_PROPERTY::READ | 
93                                                NIMBLE_PROPERTY::WRITE|
94                                                NIMBLE_PROPERTY::WRITE_ENC, // only allow writing if paired / encrypted
95                                                20
96                                               );
97
98     /** Start the services when finished creating all Characteristics and Descriptors */  
99     pDeadService->start();
100
101
102     NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
103     /** Add the services to the advertisment data **/
104     pAdvertising->addServiceUUID(pDeadService->getUUID());
105  
106     /** If your device is battery powered you may consider setting scan response
107      *  to false as it will extend battery life at the expense of less data sent.
108      */
109     pAdvertising->setScanResponse(true);
110     pAdvertising->start();
111
112     Serial.println("Advertising Started");
113 }
114
115
116 void loop() {
117 }