4.5 C
New York
Thursday, December 12, 2024

c++ – ANCS ESP32 BLE Arduino Invalid command (0xA1). The command was improperly formatted. When write worth to Management Level Attribute


I am now making an attempt to put in writing to the Management Level Attribute in keeping with the ANCS – Apple Notification Middle Service utilizing an Arduino ESP32.

That is my present definition for the notification attributes command to ship to the Management Level Attribute:

        uint8_t notificationAttributesCommand[7];

        notificationAttributesCommand[0] = 0x00;  // Command ID: Get Notification Attributes
        notificationAttributesCommand[1] = (uint8_t)(notificationUID >> 24);  // Notification UID (Most vital byte)
        notificationAttributesCommand[2] = (uint8_t)(notificationUID >> 16);  // Second byte
        notificationAttributesCommand[3] = (uint8_t)(notificationUID >> 8);   // Third byte
        notificationAttributesCommand[4] = (uint8_t)(notificationUID);        // Least vital byte

        // Attribute IDs and their most size (as per your specification)
        notificationAttributesCommand[5] = 0x01;  // Attribute ID for App Identifier

And that is how I ship it in my loop() operate:

void loop() {
    if (newNotificationFlag) {
        // Ship the command to the Management Level attribute
        if (pControlPointCharacteristic != nullptr) {
            Serial.println("Sending Get Notification Attributes command to Management Level...");
            if (pControlPointCharacteristic->canWrite()) {
                Serial.println("earlier than writing to pControlPointCharacteristic!");
                pControlPointCharacteristic->writeValue(notificationAttributesCommand, sizeof(notificationAttributesCommand), true);
                Serial.println("after writing to pControlPointCharacteristic!");
            } else {
              Serial.println("loop(): Management Level Attribute can't be written.");
            }
        } else {
            Serial.println("Management Level attribute is null");
        }
        // Reset the flag
        newNotificationFlag = false;
    }
}

I’ve modified the code from BLERemoteCharacteristic.cpp to output the log like this:

case ESP_GATTC_WRITE_CHAR_EVT:
    {
      log_d("case ESP_GATTC_WRITE_CHAR_EVT");
      // Decide if this occasion is for us and, if not, cross onwards.
      if (evtParam->write.deal with != getHandle()) {
        break;
      }

      // Test the write standing to substantiate if it was profitable
      // Test the write standing to substantiate if it was profitable or if an error occurred
      if (evtParam->write.standing == ESP_GATT_OK) {
          log_d("Write to Management Level attribute was profitable.");
      } else {
          // Deal with particular ANCS error codes
          swap (evtParam->write.standing) {
              case 0xA0:
                  log_d("Write to Management Level attribute failed. Error: Unknown command (0xA0). The commandID was not acknowledged by the NP.");
                  break;
              case 0xA1:
                  log_d("Write to Management Level attribute failed. Error: Invalid command (0xA1). The command was improperly formatted.");
                  break;
              case 0xA2:
                  log_d("Write to Management Level attribute failed. Error: Invalid parameter (0xA2). One of many parameters (e.g., NotificationUID) doesn't check with an present object on the NP.");
                  break;
              case 0xA3:
                  log_d("Write to Management Level attribute failed. Error: Motion failed (0xA3). The motion was not carried out.");
                  break;
              default:
                  log_d("Write to Management Level attribute failed. Unknown error standing: %d", evtParam->write.standing);
                  break;
          }
      }

      // Error Codes
      // https://developer.apple.com/library/archive/documentation/CoreBluetooth/Reference/AppleNotificationCenterServiceSpecification/Specification/Specification.html#//apple_ref/doc/uid/TP40013460-CH1-SW7
      // When writing to the Management Level attribute, an NC might obtain the next ANCS-specific error codes:

      // There may be nothing additional we have to do right here.  That is merely a sign
      // that the write has accomplished and we will unlock the caller.
      m_semaphoreWriteCharEvt.give();
      log_d("after m_semaphoreWriteCharEvt.give, There may be nothing additional we have to do right here.  That is merely a sign that the write has accomplished and we will unlock the caller.");
      break;
    }  // ESP_GATTC_WRITE_CHAR_EVT

Upon deployment and testing, I get this log:

15:35:25.617 -> [577839][D][BLEDevice.cpp:138] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... ESP_GATTC_NOTIFY_EVT
15:35:25.617 -> [577852][D][BLEClient.cpp:185] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... ESP_GATTC_NOTIFY_EVT
15:35:25.661 -> [577863][D][BLERemoteCharacteristic.cpp:154] gattClientEventHandler(): case ESP_GATTC_NOTIFY_EVT
15:35:25.661 -> [577871][D][BLERemoteCharacteristic.cpp:154] gattClientEventHandler(): case ESP_GATTC_NOTIFY_EVT
15:35:25.661 -> [577880][D][BLERemoteCharacteristic.cpp:159] gattClientEventHandler(): @Invoking callback for notification on attribute Attribute: uuid: 9fbf120d-6301-42d9-8c58-25e699a21dbd, deal with: 40 0x0028, props: broadcast: 0, learn: 0, write_nr: 0, write: 0, notify: 1, point out: 0, auth: 0
15:35:25.681 -> 0x00 0x10 0x00 0x0C 0x0D 0x00 0x00 0x00 
15:35:25.681 -> New notification!
15:35:25.681 -> Notification UID: 218103808
15:35:25.681 ->   Class: Different
15:35:25.681 -> Sending Get Notification Attr[577906][D][BLERemoteCharacteristic.cpp:154] gattClientEventHandler(): case ESP_GATTC_NOTIFY_EVT
15:35:25.713 -> ibutes command to Management Level...
15:35:25.713 -> earlier than writing to pControlPointCharacteristic!
15:35:25.713 -> [577925][D][BLERemoteCharacteristic.cpp:598] writeValue(): >> writeValue(), size: 7
15:35:25.713 -> [577941][D][BLERemoteCharacteristic.cpp:606] writeValue(): 1. Earlier than m_semaphoreWriteCharEvt.take
15:35:25.713 -> [577949][D][BLERemoteCharacteristic.cpp:610] writeValue(): 2. After m_semaphoreWriteCharEvt.take
15:35:25.746 -> [577958][D][BLERemoteCharacteristic.cpp:617] writeValue(): 3. Earlier than errRc != ESP_OK
15:35:25.746 -> [577966][D][BLERemoteCharacteristic.cpp:624] writeValue(): 4. Earlier than m_semaphoreWriteCharEvt.wait
15:35:25.811 -> [578051][D][BLEDevice.cpp:138] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... ESP_GATTC_WRITE_CHAR_EVT
15:35:25.843 -> [578062][D][BLEClient.cpp:185] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... ESP_GATTC_WRITE_CHAR_EVT
15:35:25.843 -> [578073][D][BLERemoteCharacteristic.cpp:241] gattClientEventHandler(): case ESP_GATTC_WRITE_CHAR_EVT
15:35:25.843 -> [578082][D][BLERemoteCharacteristic.cpp:258] gattClientEventHandler(): Write to Management Level attribute failed. Error: Invalid command (0xA1). The command was improperly formatted.
15:35:25.875 -> [578098][D][BLERemoteCharacteristic.cpp:626] writeValue():[ 557.8 0A9f8t]e[rD ]m[_BsLeEmRaepmhootreeCWhrairtaecCthearriEsvtti.cw.ait:279] gattClientEventHandler(): after m_semaphoreWriteCharEvt.give, There may be nothing additional we have to do right here.  That is merely a sign that the write has accomplished and we will unlock the caller.
15:35:25.907 -> after writing to pControlPointCharacteristic!
15:35:25.907 -> [578127][D][BLERemoteCharacteristic.cpp:241] gattClientEventHandler(): case ESP_GATTC_WRITE_CHAR_EVT
15:35:25.907 -> [578140][D][BLERemoteCharacteristic.cpp:241] gattClientEventHandler(): case ESP_GATTC_WRITE_CHAR_EVT

I am specializing in this specific line:

Write to Management Level attribute failed. Error: Invalid command (0xA1). The command was improperly formatted.

Can somebody inform me what’s unsuitable with my get command definition? Thanks

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles