How to write data from central to peripheral?


#1

Hi,

I have 2 BLE Nano devices. One is for central and the other is for peripheral device.
The reference code for central device is from “BLE central test example”.
And the reference code for peripheral device is from “Simple Chat”.

Part of main() function in my central code is as follows:

ble.init();
ble.onConnection(connectionCallBack);
ble.onDisconnection(disconnectionCallBack);
ble.gattClient().onServiceDiscoveryTermination(discoveryTermination);
ble.gattClient().onHVX(hvxCallBack);    
ble.gattClient().onDataRead(onDataRead);
ble.startScan(scanCallBack);

The peripheral can successfully update its value to central via hvxCallBack callback function.
But how to write data from central to peripheral ?
Can you help please?
Thank you !!


Write from Central to Peripheral
#2

Hi frankc,
You can use

ble.gattClient().write(GattClient::GATT_OP_WRITE_CMD,    // op write_cmd
                               chars->getConnHandle(),   // connect handle
                               chars->getValueHandle(),  // value handle
                               2,                        // len    
                               (uint8_t *)&value);       // data

BLE Nano Nordic UART Central Write data to peripheral
#3

Hi jixing,
It works now.
Thank you !


#4

Hi,

I have another question.
Is there only one service with one characteristic that the central can launch ?

The peripheral has one service with two characteristics.
The central can discover the first one characteristic but can’t discover the second one at the same time.

In the connectionCallBack callback function described as above,ble can launch the service with RX characteristic and it works well but it can’t launch the service with TX characteristic
Please help.
Thank you !

My code is as follows:

_#define TXRX_BUF_LEN                      30_
uint8_t tx_value[TXRX_BUF_LEN] = {0,};
uint8_t rx_value[TXRX_BUF_LEN] = {0,};
GattCharacteristic characteristic1(UARTServiceTXCharacteristicUUID, tx_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
GattCharacteristic characteristic2(UARTServiceRXCharacteristicUUID, rx_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic *uartChars[] = {&characteristic1, &characteristic2};
GattService     uartService(UARTServiceUUID, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));

void connectionCallBack( const Gap::ConnectionCallbackParams_t *params )
{
ble.gattClient().launchServiceDiscovery(params->handle, ServiceCallBack, CharacteristicCallBack, UARTServiceUUID, UARTServiceRXCharacteristicUUID);

ble.gattClient().launchServiceDiscovery(params->handle, ServiceCallBack_new, CharacteristicCallBack_new, UARTServiceUUID, UARTServiceTXCharacteristicUUID);
}

#5

Hi frankc,
I will test you problem later. Thanks for you point.


#6

Hi frankc,
Board package V106 has been released.This version solve you question, you could try this.


#7

Hi Jinxing,

This board package V106 solves my problem.
Thanks a lot !

I am trying to build a central device to receive notification from two peripheral devices.
After scanning,connecting then discovering service and characteristics, connect the found devices.
I find out the central can only get the first peripheral notification.
And it can’t get the second one’s notification.

For a central device, how to receive notification from two peripheral devices ?

Part of my code is as follows.
Please help !
Thank you !

static void discoveredDescTerminationCallBack(const CharacteristicDescriptorDiscovery::TerminationCallbackParams_t *params) {
  Serial.println("\r\n----discovery descriptor Termination");
  //if(descriptor_is_found) 
  {
    Serial.println("Open Device notify");
    uint16_t value = 0x0001;

    if(descriptor_is_found_1)
    {
      Serial.println("---check ONE ----");
    ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ, chars_rx.getConnectionHandle(), desc_of_chars_rx.getAttributeHandle(), 2, (uint8_t *)&value);
    }
    if(descriptor_is_found_2)
    {
      Serial.println("---check TWO ----");
    ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ, chars_rx_2.getConnectionHandle(), desc_of_chars_rx_2.getAttributeHandle(), 2, (uint8_t *)&value);
    }
  }
}

#8

Hi frankc,

You can’t open notify twice at the same time.
When you open notify:

if(descriptor_is_found_1)
{
    Serial.println("---check ONE ----");
    ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ,chars_rx.getConnectionHandle(), desc_of_chars_rx.getAttributeHandle(), 2, (uint8_t *)&value);
}

After this operation finish, would call the function:

void onDataWriteCallBack(const GattWriteCallbackParams *params) 

You should open the second notify in this function.


#9

Hi jinxing,

I put the second connection in the onDataWriteCallBack() function and it works.
Now the central can receive the notification from two peripheral devices at the same time.
Thanks a lot !!

I wonder how many peripherals the central can connect with at the same time?


#10

Hi frankc,

Using S130, you could connect to three peripherals(you best use chip with 32KB RAM).


#11

Hey all!

Thanks for your answers, However, I am still unable to write from central to peripheral.

Can someone give their code, both for central and peripheral for a simple write from central?

Thanks