This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE notification and write value not working

I have followed the well written blog post on how to create custom service on this site. I downloaded the sdk 13 code for the same and i can send and receive values on the Nordic android app.

Blog: devzone.nordicsemi.com/.../

SDK 13 code: devzone.nordicsemi.com/.../15bc9d099b645556b335757c662d3d54

Link: devzone.nordicsemi.com/.../

For me three things are not working as expected:

  1. printf commands are working but NRF_LOG_INFO is not working

  2. iPad is sending cccd value as 0x20002128 instead of 0x0001 to enable notification hence it in not going in the loop as shown below to print "Char cccd Notification enabled"

cccd handle is 0x0011

Putty Text:

Value received on handle 0x0011: 0x20002128

else if(p_ble_evt->evt.gatts_evt.params.write.handle == p_our_service->char_handles.cccd_handle)
    {
        // Get data
        sd_ble_gatts_value_get(p_our_service->conn_handle, p_our_service->char_handles.cccd_handle, &rx_data);
        // Print handle and value 
        printf("\r\nValue received on char cccd handle %#06x: %#06x\r\n", p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer);
        if(data_buffer == 0x0001)
        {
            printf("\r\nChar cccd Notification enabled\r\n");
        }
        else if(data_buffer == 0x0000)
        {
            printf("\r\nChar cccd Notification disabled\r\n");
        }
    }
  1. I can not write the value from iPad. I modified the code here a little bit so when we receive "28\0" it will enable the gpio pin and enable the led. (external not oboard)

    uint8_t ledValue = atoi(rx_data.p_value); if(ledValue >= 28 || ledValue <= 31){ only_enable_led(ledValue); }

When i send "28\0" from Nordic app it works.

But does not work from ipad swift 3 code is below:

  sendValueToBLE("28\0")

  func sendValueToBLE(value: String) {
            let data = value.data(using: String.Encoding.utf8)
            for blePeripheral in devicePeripherals{
                for characteristic in blePeripheral.services?.first?.characteristics as [CBCharacteristic]!
                {
                    let c = characteristic.uuid.uuidString
                    if(characteristic.uuid.uuidString.starts(with: "0000BEE2"))
                    {
                        blePeripheral.writeValue(data!, for: characteristic, type: CBCharacteristicWriteType.withoutResponse)
                    }
                }
            }
        }

i can see in debugger it executes this line:

blePeripheral.writeValue(data!, for: characteristic, type: CBCharacteristicWriteType.withoutResponse)

But it shows this on output window:

WARNING: Characteristic <CBCharacteristic: 0x1c40a58e0, UUID = 0000BEE2-1212-EFDE-1523-785FEF13D123, properties = 0x1A, value = (null), notifying = YES> does not specify the "Write Without Response" property - ignoring response-less write

Also putty does not show any message received. My be this command is failing to send any data to NRF52-DK.

    1. In my case it seems to be the other way around. Have you enabled logging in sdk_config.h? Search for NRF_LOG_ENABLED and set it to 1. I think my plan was to change all printf statements to NRF_LOG_INFO().
    2. That looks like a variable's address in RAM rather than the actual variable value. It seems to work fine when I try the code in your link with an Android phone. There seems to be a bug in the on_ble_write() function too. Try to use if((data_buffer & 0x0000FFFF) == .....) instead of if(data_buffer == .....) otherwise the statements will never be true.
  • May I ask what app (what open source code repository) you might be using for Swift on iOS? (Because I am starting a similar project as yours.)

    Random thoughts: Is "28\0" correct? If that is a C string type, it has a null terminator without you needing to specify one. I suppose the Swift String type wants you to specify a null terminator? Because you say it works on Android, but not in Swift iOS, is the bug on the central side?

  • Also, is the string representation of a UUID like "0xFF" instead of "FF"? Again, just random thought, not knowing where the Swift code came from.

  • @ Rahul: To answer your third question: In the tutorial, the write without response property for the characteristic is not set. That is probably why you get the "Write Without Response property "-warning. I'm not familiar with Swift, but maybe there is an API call looking something like this: CBCharacteristicWriteType.withResponse (Notice this is with response and not without). You can also try to set the read without response property on the peripheral side when you initiate the characteristic.

Related