@@ -21,7 +21,7 @@ static NimBLEUUID uuidCharaHidInformation("2a4a");
2121static NimBLEUUID uuidCharaPeripheralAppearance (" 2a01" );
2222static NimBLEUUID uuidCharaPeripheralControlParameters (" 2a04" );
2323
24- static NimBLEAdvertisedDevice* advDevice;
24+ static const NimBLEAdvertisedDevice* advDevice;
2525static NimBLEClient* pConnectedClient = nullptr ;
2626
2727static const uint16_t controllerAppearance = 964 ;
@@ -42,67 +42,60 @@ class ClientCallbacks : public NimBLEClientCallbacks {
4242 this ->pConnectionState = pConnectionState;
4343 }
4444
45- void onConnect (NimBLEClient* pClient) {
45+ void onConnect (NimBLEClient* pClient) override {
4646#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
4747 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (" Connected" );
4848#endif
4949 *pConnectionState = ConnectionState::WaitingForFirstNotification;
5050 // pClient->updateConnParams(120,120,0,60);
51- };
51+ }
5252
53- void onDisconnect (NimBLEClient* pClient) {
53+ void onDisconnect (NimBLEClient* pClient, int reason) override {
5454#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
5555 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.print (
5656 pClient->getPeerAddress ().toString ().c_str ());
5757 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (" Disconnected" );
5858#endif
5959 *pConnectionState = ConnectionState::Scanning;
6060 pConnectedClient = nullptr ;
61- };
61+ }
6262
63- /* ******************** Security handled here **********************
64- ****** Note: these are the same return values as defaults ********/
65- uint32_t onPassKeyRequest () {
66- // Serial.println("Client Passkey Request");
67- /* * return the passkey to send to the server */
68- return 0 ;
69- };
63+ void onPassKeyEntry (NimBLEConnInfo& connInfo) override {
64+ NimBLEDevice::injectPassKey (connInfo, 0 );
65+ }
7066
71- bool onConfirmPIN ( uint32_t pass_key) {
67+ void onConfirmPasskey (NimBLEConnInfo& connInfo, uint32_t pass_key) override {
7268 // Serial.print("The passkey YES/NO number: ");
7369 // Serial.println(pass_key);
74- /* * Return false if passkeys don't match. */
75- return true ;
76- };
77-
78- /* * Pairing process complete, we can check the results in ble_gap_conn_desc */
79- void onAuthenticationComplete (ble_gap_conn_desc* desc) {
80- // Serial.println("onAuthenticationComplete");
81- if (!desc->sec_state .encrypted ) {
82- // Serial.println("Encrypt connection failed - disconnecting");
83- /* * Find the client with the connection handle provided in desc */
84- NimBLEDevice::getClientByID (desc->conn_handle )->disconnect ();
70+ NimBLEDevice::injectConfirmPasskey (connInfo, true );
71+ }
72+
73+ void onAuthenticationComplete (NimBLEConnInfo& connInfo) override {
74+ if (!connInfo.isEncrypted ()) {
75+ // Serial.printf("Encrypt connection failed - disconnecting\n");
76+ /* * Find the client with the connection handle provided in connInfo */
77+ NimBLEDevice::getClientByHandle (connInfo.getConnHandle ())->disconnect ();
8578 return ;
8679 }
87- };
80+ }
8881};
8982
9083/* * Define a class to handle the callbacks when advertisments are received */
91- class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks {
84+ class ScanCallbacks : public NimBLEScanCallbacks {
9285 public:
93- AdvertisedDeviceCallbacks (String strTargetDeviceAddress,
94- ConnectionState* pConnectionState) {
86+ ScanCallbacks (String strTargetDeviceAddress,
87+ ConnectionState* pConnectionState) {
9588 if (strTargetDeviceAddress != " " ) {
9689 this ->targetDeviceAddress =
97- new NimBLEAddress (strTargetDeviceAddress.c_str ());
90+ new NimBLEAddress (strTargetDeviceAddress.c_str (), 0 );
9891 }
9992 this ->pConnectionState = pConnectionState;
10093 }
10194
10295 private:
10396 NimBLEAddress* targetDeviceAddress = nullptr ;
10497 ConnectionState* pConnectionState;
105- void onResult (NimBLEAdvertisedDevice* advertisedDevice) {
98+ void onResult (const NimBLEAdvertisedDevice* advertisedDevice) override {
10699#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
107100 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.print (" Advertised Device found: " );
108101 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (
@@ -116,15 +109,16 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks {
116109 ? advertisedDevice->getServiceUUID ().toString ().c_str ()
117110 : " none" );
118111#endif
119- char * pHex = NimBLEUtils::buildHexData (
120- nullptr , (uint8_t *)advertisedDevice->getManufacturerData ().data (),
112+ auto pHex = NimBLEUtils::dataToHexString (
113+ (uint8_t *)advertisedDevice->getManufacturerData ().data (),
121114 advertisedDevice->getManufacturerData ().length ());
122115 if ((targetDeviceAddress != nullptr &&
123116 advertisedDevice->getAddress ().equals (*targetDeviceAddress)) ||
124117 (targetDeviceAddress == nullptr &&
125118 advertisedDevice->getAppearance () == controllerAppearance &&
126- (strcmp (pHex, controllerManufacturerDataNormal.c_str ()) == 0 ||
127- strcmp (pHex, controllerManufacturerDataSearching.c_str ()) == 0 ) &&
119+ (strcmp (pHex.c_str (), controllerManufacturerDataNormal.c_str ()) == 0 ||
120+ strcmp (pHex.c_str (), controllerManufacturerDataSearching.c_str ()) ==
121+ 0 ) &&
128122 advertisedDevice->getServiceUUID ().equals (uuidServiceHid)))
129123 // if (advertisedDevice->isAdvertisingService(uuidServiceHid))
130124 {
@@ -137,26 +131,24 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks {
137131 *pConnectionState = ConnectionState::Found;
138132 advDevice = advertisedDevice;
139133 }
140- };
134+ }
141135};
142136
143137class Core {
144138 public:
145139 Core (String targetDeviceAddress = " " ) {
146- this ->advDeviceCBs =
147- new AdvertisedDeviceCallbacks (targetDeviceAddress, &connectionState);
140+ this ->scanCBs = new ScanCallbacks (targetDeviceAddress, &connectionState);
148141 this ->clientCBs = new ClientCallbacks (&connectionState);
149142 }
150143
151- AdvertisedDeviceCallbacks* advDeviceCBs ;
144+ ScanCallbacks* scanCBs ;
152145 ClientCallbacks* clientCBs;
153146 uint8_t battery = 0 ;
154147 static const int deviceAddressLen = 6 ;
155148 uint8_t deviceAddressArr[deviceAddressLen];
156149
157150 void begin () {
158- NimBLEDevice::setScanFilterMode (CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE);
159- // NimBLEDevice::setScanDuplicateCacheSize(200);
151+ // NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE);
160152 NimBLEDevice::init (" " );
161153 NimBLEDevice::setOwnAddrType (BLE_OWN_ADDR_PUBLIC);
162154 NimBLEDevice::setSecurityAuth (true , false , false );
@@ -175,7 +167,7 @@ class Core {
175167#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
176168 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (pService->toString ().c_str ());
177169#endif
178- for (auto pChara : * pService->getCharacteristics ()) {
170+ for (auto pChara : pService->getCharacteristics ()) {
179171 if (pChara->canWrite ()) {
180172#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
181173 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (
@@ -237,7 +229,7 @@ class Core {
237229 // pScan->clearResults();
238230 // pScan->clearDuplicateCache();
239231 pScan->setDuplicateFilter (false );
240- pScan->setAdvertisedDeviceCallbacks (advDeviceCBs );
232+ pScan->setScanCallbacks (scanCBs );
241233 // pScan->setActiveScan(true);
242234 pScan->setInterval (97 );
243235 pScan->setWindow (97 );
@@ -306,11 +298,11 @@ class Core {
306298
307299 /* * Handles the provisioning of clients and connects / interfaces with the
308300 * server */
309- bool connectToServer (NimBLEAdvertisedDevice* advDevice) {
301+ bool connectToServer (const NimBLEAdvertisedDevice* advDevice) {
310302 NimBLEClient* pClient = nullptr ;
311303
312304 /* * Check if we have a client we should reuse first **/
313- if (NimBLEDevice::getClientListSize ()) {
305+ if (NimBLEDevice::getCreatedClientCount ()) {
314306 pClient = NimBLEDevice::getClientByPeerAddress (advDevice->getAddress ());
315307 if (pClient) {
316308 pClient->connect ();
@@ -319,7 +311,7 @@ class Core {
319311
320312 /* * No client to reuse? Create a new one. */
321313 if (!pClient) {
322- if (NimBLEDevice::getClientListSize () >= NIMBLE_MAX_CONNECTIONS) {
314+ if (NimBLEDevice::getCreatedClientCount () >= NIMBLE_MAX_CONNECTIONS) {
323315#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
324316 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (
325317 " Max clients reached - no more connections available" );
@@ -387,9 +379,9 @@ class Core {
387379 }
388380
389381 bool afterConnect (NimBLEClient* pClient) {
390- memcpy (deviceAddressArr, pClient->getPeerAddress ().getNative (),
382+ memcpy (deviceAddressArr, pClient->getPeerAddress ().getBase (),
391383 deviceAddressLen);
392- for (auto pService : * pClient->getServices (true )) {
384+ for (auto pService : pClient->getServices (true )) {
393385 auto sUuid = pService->getUUID ();
394386 if (!sUuid .equals (uuidServiceHid) && !sUuid .equals (uuidServiceBattery)) {
395387 continue ; // skip
@@ -398,7 +390,7 @@ class Core {
398390 XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println (
399391 pService->toString ().c_str ());
400392#endif
401- for (auto pChara : * pService->getCharacteristics (true )) {
393+ for (auto pChara : pService->getCharacteristics (true )) {
402394 charaHandle (pChara);
403395 charaSubscribeNotification (pChara);
404396 }
0 commit comments