How do I advertise both manufacturer and custom service?

I want to advertise a custom service so my central can filter for it and have a device name and manufacturer. I have the following code but can only seem to do either the manufacturer or the custom service but not at the same time.

static const struct bt_data ad[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
	//BT_DATA(BT_DATA_MANUFACTURER_DATA, (unsigned char *)&adv_mfg_data, sizeof(adv_mfg_data)),
    BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_CUSTOM_SERVICE_VAL),
};

Parents
  • Hi Leo, sorry for the delay.

    You can advertise both manufacturer data and custom service data at the same time by including both in your `bt_data` array. However, you need to be aware of the maximum advertisement data size. The total size of advertisement data cannot exceed 31 bytes due to the Bluetooth specification. If you have more data, you may need to use the scan response to fit the additional data.

    static const struct bt_data ad[] = {
    
        BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
    
        BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
    
        BT_DATA_BYTES(BT_DATA_SVC_DATA16,
    
                  0xaa, 0xfe, /* Eddystone UUID */
    
                  0x10, /* Eddystone-URL frame type */
    
                  0x00, /* Calibrated Tx power at 0m */
    
                  0x00, /* URL Scheme Prefix http://www. */
    
                  'z', 'e', 'p', 'h', 'y', 'r',
    
                  'p', 'r', 'o', 'j', 'e', 'c', 't',
    
                  0x08) /* .org */
    
    };
    
    /* Set Scan Response data */
    
    static const struct bt_data sd[] = {
    
        BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
    
                      sd, ARRAY_SIZE(sd));
    
        if (err) {
    
            printk("Advertising failed to start (err %d)\n", err);
    
            return;
    
        }
    
    
    

    In this example, ad is the advertising data and sd is the scan response data. bt_le_adv_start starts advertising with both the advertising data and the scan response data.

Reply
  • Hi Leo, sorry for the delay.

    You can advertise both manufacturer data and custom service data at the same time by including both in your `bt_data` array. However, you need to be aware of the maximum advertisement data size. The total size of advertisement data cannot exceed 31 bytes due to the Bluetooth specification. If you have more data, you may need to use the scan response to fit the additional data.

    static const struct bt_data ad[] = {
    
        BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
    
        BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
    
        BT_DATA_BYTES(BT_DATA_SVC_DATA16,
    
                  0xaa, 0xfe, /* Eddystone UUID */
    
                  0x10, /* Eddystone-URL frame type */
    
                  0x00, /* Calibrated Tx power at 0m */
    
                  0x00, /* URL Scheme Prefix http://www. */
    
                  'z', 'e', 'p', 'h', 'y', 'r',
    
                  'p', 'r', 'o', 'j', 'e', 'c', 't',
    
                  0x08) /* .org */
    
    };
    
    /* Set Scan Response data */
    
    static const struct bt_data sd[] = {
    
        BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
    
                      sd, ARRAY_SIZE(sd));
    
        if (err) {
    
            printk("Advertising failed to start (err %d)\n", err);
    
            return;
    
        }
    
    
    

    In this example, ad is the advertising data and sd is the scan response data. bt_le_adv_start starts advertising with both the advertising data and the scan response data.

Children
No Data
Related