I'm trying to write some BlueZ code using the Management API but I can't seem to get BlueZ to advertise arbitrary data on the advertisement system
I have 2 tests one in C++ and one using the btmgmt
tool that BlueZ has released
When I run this command it fails this way
btmgmt add-adv -d 8C33 1
Add Advertising failed with status 0x0d (Invalid Parameters)
The equivalent C++ code also fails identically
#include <iostream>
extern "C" {
#include "external/bluetooth/lib/bluetooth.h"
#include "external/bluetooth/lib/mgmt.h"
#include "external/bluetooth/lib/hci.h"
#include "external/bluetooth/lib/hci_lib.h"
#include "external/bluetooth/src/shared/mgmt.h"
}
static void setup_dv(uint8_t status, uint16_t length,
const void *param, void *user_data)
{
std::cout << "good" << std::endl;
}
struct CPadvertising {
uint8_t instance;
uint32_t flags;
uint16_t duration;
uint16_t timeout;
uint8_t adv_data_len;
uint8_t scan_rsp_len;
uint8_t data[4];
} __packed;
int main()
{
mgmt* sock = mgmt_new_default();
CPadvertising* data = new CPadvertising();
data->instance = 1;
data->duration = 2;
data->timeout = 100;
data->adv_data_len = 4;
data->scan_rsp_len = 0;
data->flags = 0;
data->data[0] = 8;
data->data[1] = 12;
data->data[2] = 3;
data->data[3] = 3;
mgmt_send(sock, MGMT_OP_ADD_ADVERTISING, index,
sizeof(CPadvertising), data, setup_dv, nullptr, nullptr);
auto loop = g_main_loop_new(nullptr, FALSE);
g_main_loop_run(loop);
}
has this output in btmon
@ MGMT Command: Add Advertising (0x003e) plen 13 {0x0003} [hci0] 98.205359
Instance: 1
Flags: 0x00000000
Duration: 2
Timeout: 100
Advertising data length: 4
8 c 3 3 .3
Scan response length: 0
@ MGMT Event: Command Status (0x0002) plen 3 {0x0003} [hci0] 98.205376
Add Advertising (0x003e)
Status: Invalid Parameters (0x0d)
Referring to these docs, I should not get invalid parameter for any of this but I do
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt
However, I know certain types of data does indeed work
btmgmt add-adv -d 080954657374204C45
works even in an even weirder way prompting this output on btmon
@ MGMT Command: Add Advertising (0x003e) plen 20 {0x0002} [hci0] 143.671485
Instance: 1
Flags: 0x00000000
Duration: 0
Timeout: 0
Advertising data length: 9
Name (complete): Test LL <--- What is this
Scan response length: 0
Looking at the source for btmgmt, these are the options available:
static void add_adv_usage(void)
{
bt_shell_usage();
print("Options:\n"
"\t -u, --uuid <uuid> Service UUID\n"
"\t -d, --adv-data <data> Advertising Data bytes\n"
"\t -s, --scan-rsp <data> Scan Response Data bytes\n"
"\t -t, --timeout <timeout> Timeout in seconds\n"
"\t -D, --duration <duration> Duration in seconds\n"
"\t -P, --phy <phy> Phy type, Specify 1M/2M/CODED\n"
"\t -c, --connectable \"connectable\" flag\n"
"\t -g, --general-discov \"general-discoverable\" flag\n"
"\t -l, --limited-discov \"limited-discoverable\" flag\n"
"\t -n, --scan-rsp-local-name \"local-name\" flag\n"
"\t -a, --scan-rsp-appearance \"appearance\" flag\n"
"\t -m, --managed-flags \"managed-flags\" flag\n"
"\t -p, --tx-power \"tx-power\" flag\n"
"e.g.:\n"
"\tadd-adv -u 180d -u 180f -d 080954657374204C45 1");
}
Looking at the advert that worked, the first number is the length of the data, the second is the data_type, and the rest is the local name
string. So:
0x08 = Length of datatype is 8
0x09 = Data type is <Complete Local Name>
0x54 = T
0x65 = e
0x73 = s
0x74 = t
0x20 = ' '
0x4C = L
0x45 = E
Not sure why that has turned up as
Test LL
in the btmon output. I'll assume it was a slightly different run that you recorded.
Looking at your data of 8c33
; the 8c does not represent the length and 33 is not a valid data type.
I suspect what you want to do is send manufacturer data. Assuming you don't have a company identifier, then you can use 0xffff
for testing purposes.
This would be a command of:
btmgmt add-adv -d 05ffffff8C33 -g 1
Which gave the following in btmon
@ MGMT Command: Add Adver.. (0x003e) plen 17 {0x0002} [hci0] 341.378134
Instance: 1
Flags: 0x00000002
Advertise as Discoverable
Duration: 0
Timeout: 0
Advertising data length: 6
Company: internal use (65535)
Data: 8c33
Scan response length: 0
User contributions licensed under CC BY-SA 3.0