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

Exposed GAP.setAdvertisedServiceData to BLELocalDevice #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions examples/Peripheral/ClimateSensor/ClimateSensor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Climate Sensor

This example creates a BLE peripheral which emulates a Xiami Mijia climate
sensor. This device encodes the temperature and humidity into the service
data, so BLE apps can detect the current readings without connecting. For
more information see here:
https://github.com/mspider65/Xiaomi-Mijia-Bluetooth-Temperature-and-Humidity-Sensor

The circuit:
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

This example code is in the public domain.
*/

#include <ArduinoBLE.h>


void setup() {
Serial.begin(9600); // initialize serial communication
while (!Serial);

pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");

while (1);
}

/* Set a local name for the BLE device
This name will appear in advertising packets
and can be used by remote devices to identify this BLE device
The name can be changed but maybe be truncated based on space left in advertisement packet
*/

// read all the sensor values if you have something like a HTS sensor
float temperature = 28.5; // HTS.readTemperature();
float humidity = 42.0; // HTS.readHumidity();

// convert the temperatures to a 16-bit byte format
uint16_t tempBytes = temperature * 10.0;
uint16_t humidityBytes = humidity * 10.0;

// prepare the service data payload, this emulates a Xiamo Mijia BLE Climate Sensor
uint16_t uuid = 0xFE95;
uint8_t serviceDataLength = 18;
uint8_t serviceData[] = {0x50, 0x20, 0xAA, 0x01, 0x17, 0xEE, 0x7F, 0xB5, 0x91, 0xFB, 0xE8, 0x0D, 0x10, 0x04, tempBytes & 0xFF, tempBytes>>8, humidityBytes & 0xFF, humidityBytes>>8};

// Set the service data
BLE.setAdvertisedServiceData(uuid, serviceData, serviceDataLength);

/* Start advertising BLE. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection */

// start advertising
BLE.advertise();

Serial.println("Bluetooth device active and advertising...");
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);

digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
5 changes: 5 additions & 0 deletions src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ void BLELocalDevice::setAdvertisedService(const BLEService& service)
setAdvertisedServiceUuid(service.uuid());
}

void BLELocalDevice::setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length)
{
GAP.setAdvertisedServiceData(uuid, data, length);
}

void BLELocalDevice::setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength)
{
GAP.setManufacturerData(manufacturerData, manufacturerDataLength);
Expand Down
1 change: 1 addition & 0 deletions src/local/BLELocalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class BLELocalDevice {

void setAdvertisedServiceUuid(const char* advertisedServiceUuid);
void setAdvertisedService(const BLEService& service);
void setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);
void setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength);
void setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength);
void setLocalName(const char *localName);
Expand Down
2 changes: 1 addition & 1 deletion src/utility/GAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GAPClass {
virtual ~GAPClass();

void setAdvertisedServiceUuid(const char* advertisedServiceUuid);
void setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);
void setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength);
void setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength);
void setLocalName(const char *localName);
Expand All @@ -53,7 +54,6 @@ class GAPClass {
protected:
friend class BLELocalCharacteristic;

void setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);

protected:
friend class HCIClass;
Expand Down