Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Manufacturer specific data into Advertising data #26

Closed
FTaluk opened this issue May 3, 2019 · 5 comments
Closed

Adding Manufacturer specific data into Advertising data #26

FTaluk opened this issue May 3, 2019 · 5 comments

Comments

@FTaluk
Copy link

FTaluk commented May 3, 2019

Hi,
I am kind of new to C++ and Bluetooth. I have just started using your server. I have successfully implemented a basic NUS based chat application by modifying your server code and standalone app code. Thanks for all your effort.

But I need to add manufacturer specific data into the advertising data. I could not find any easy way until now to do this. With my limited understanding, I realized that I have to change things much deep into your codes (e.g. using add Advertising command of management-api instead of set advertising command). But the problem is that I am afraid of chaning things without having a complete grasp (which I don't have at the moment).

p.s. I found a python based example where they provide a seperate method just to set this data. But I need a C++ based solution.
Could you please help? Thanks in advance.

@FTaluk
Copy link
Author

FTaluk commented May 7, 2019

Hi,
I figured it out. Actually, I created a simple work-around. The setAdvertising() method Mgmt.cpp file has been modified. This method now disables the "setAdvertising" command and then uses "addAdvertising Command" to make use of a custom AD-Payload. I used only one instance, zero for all time related parameters, zero for all flags. I didn't use the scan response field. The whole raw-advertising data is provied as adv_data. This worked for me.

@FTaluk FTaluk closed this as completed May 7, 2019
@stez-mind
Copy link

stez-mind commented May 24, 2019

For those interested, I also did something similar as the FTaluk solution.

I'll have to think more how to include this in a way that's generic enough for making a PR but in the meanwhile, here's my implementation in Mgmt.cpp that calls addAdvertising adding one 128-bit service UUID and a 8 chars short name into the advertisement packet:

// Start advertising with custom data
// Advertisement packet will contain: flags, shortName, uuid
bool Mgmt::addAdvertising(std::string shortName, const uint8_t* uuid)
{
    constexpr size_t ADVERTISING_SHORTNAME_MAX_LEN = 8;
    constexpr size_t ADVERTISING_UUID_LEN = 16;
    constexpr size_t ADVERTISING_MAX_DATALEN = 2 + ADVERTISING_SHORTNAME_MAX_LEN + 2 + ADVERTISING_UUID_LEN;

    struct SRequest : HciAdapter::HciHeader
    {
        uint8_t  instance;
        uint32_t flags;
        uint16_t duration;
        uint16_t timeout;
        uint8_t  advDataLen;
        uint8_t  scanRspLen;
        uint8_t  data[ADVERTISING_MAX_DATALEN];
    } __attribute__((packed));

    SRequest request;
    request.code = Mgmt::EAddAdvertisingCommand;
    request.controllerId = controllerIndex;
    request.dataSize = sizeof(SRequest) - sizeof(HciAdapter::HciHeader);

    request.instance = 1u;
    request.flags = 3u; // Connectable && Discoverable, see Bluez/lib/mgmt.h
    request.duration = 0;
    request.timeout = 0;

    size_t shortNameEffectiveLen = std::min(ADVERTISING_SHORTNAME_MAX_LEN, shortName.length());
    request.advDataLen = ADVERTISING_MAX_DATALEN - (ADVERTISING_SHORTNAME_MAX_LEN - shortNameEffectiveLen);
    request.scanRspLen = 0;

    request.data[0] = static_cast<uint8_t>(ADVERTISING_UUID_LEN + 1);
    request.data[1] = 0x07; // Incomplete UUID list
    memcpy(&request.data[2], uuid, ADVERTISING_UUID_LEN);

    request.data[2+ADVERTISING_UUID_LEN] = static_cast<uint8_t>(1 + shortNameEffectiveLen);
    request.data[3+ADVERTISING_UUID_LEN] = 0x08;  // Set short name
    memcpy(&request.data[4+ADVERTISING_UUID_LEN], shortName.c_str(), shortNameEffectiveLen);

    if (!HciAdapter::getInstance().sendCommand(request))
    {
	Logger::warn(SSTR << "  + Failed to start advertising with UUID");
	return false;
    }

    return true;
}

@xuio
Copy link

xuio commented Oct 2, 2020

@stez-mind can you give more detail about the usage of Mgmt::addAdvertising?
It is not working for me. You have to do Mgmt::setAdvertising(0) after, right?

@stez-mind
Copy link

Hi @xuio,
sorry it has been quite a long time since I worked on this.

I just checked my implementation and I was not calling explicitly Mgmt::setAdvertising(0) directly from the C++ sources but I remember that I was doing a few tasks separately with a script calling hcitool.

@xuio
Copy link

xuio commented Oct 13, 2020

@stez-mind Thanks for answering!

Can you share the context in which you call Mgmt::addAdvertising(...). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants