Hi Everyone,
I am trying to develop a simple application with the XIAO BLE Sense device, the idea is to send a BLE message when something hits the device. To achieve this, I wanted to configure the BLE to notify the event after connecting with the master. I problem I have is very simple but I cant figure out how to solve it, the problem is that I always have to Enable notifications on my phone in order to receive the notification, How can I change that to receive the message without the need to enable anything on the phone?? Thanks!!
First of all, let me summarize what I am using and what program I have uploaded to the device:
- Hardware:
- Xiao BLE Sense.
- Software:
- Visual Studio Code.
- nRF Connect for VS Code Extension Pack (latest version).
- nRF Connect SDK Toolchain v2.5.2
- nRF Connect SDK v2.5.2
- Zephyr OS.
I have used the Zephyr Example for BLE and I have the following piece of code:
/* Accel readings Service Declaration and Registration */
BT_GATT_SERVICE_DEFINE(accel_service,
BT_GATT_PRIMARY_SERVICE(BT_UUID_ACCELSERVICE),
BT_GATT_CHARACTERISTIC(BT_UUID_ACCELSERVICE_RX,
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
NULL, on_receive, NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_ACCELSERVICE_TX,
BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_READ ,//BT_GATT_PERM_READ,
NULL, NULL, NULL),
BT_GATT_CCC(on_cccd_changed,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
);
int bt_ready(void) {
return perform_bt_ready();
}
int my_service_init(void)
{
int err = 0;
memset(&data_rx, 0, MAX_TRANSMIT_SIZE);
memset(&data_tx, 0, MAX_TRANSMIT_SIZE);
return err;
}
void my_service_send(const uint8_t *data, uint16_t len)
{
/*
The attribute for the TX characteristic is used with bt_gatt_is_subscribed
to check whether notification has been enabled by the peer or not.
Attribute table: 0 = Service, 1 = Primary service, 2 = RX, 3 = TX, 4 = CCC.
*/
const struct bt_gatt_attr *attr = &accel_service.attrs[3];
struct bt_gatt_notify_params params =
{
.uuid = BT_UUID_ACCELSERVICE_TX,
.attr = attr,
.data = data,
.len = len,
.func = on_sent
};
// Check whether notifications are enabled or not
if(bt_gatt_is_subscribed(my_connection, attr, BT_GATT_CCC_NOTIFY))
{
// Send the notification
if(bt_gatt_notify_cb(my_connection, ¶ms))
{
printk("Error, unable to send notification\n");
}
}
else
{
printk("Warning, notification not enabled on the selected attribute\n");
}
}
Thanks beforehand, sorry for the inconveniences and looking forward to hearing from you.
Kind regards.