Bluetooth LE (BLE)
BleAddress
Each Bluetooth device has an address, which is encapsulated in the BleAddress object. The address is 6 octets, and the BleAddress object has two additional bytes of overhead. The BleAddress object can be trivially copied.
copy (BleAddress)
You can copy and existing BleAddress.
// PROTOTYPE
BleAddress& operator=(hal_ble_addr_t addr)
BleAddress& operator=(const uint8_t addr[BLE_SIG_ADDR_LEN])
address byte (BleAddress)
The BleAddress is 6 octets long (constant: BLE_SIG_ADDR_LEN
). You can access individual bytes using array syntax:
// PROTOTYPE
uint8_t operator[](uint8_t i) const;
// EXAMPLE - Device OS 3.0 and later
Log.info("rssi=%d address=%02X:%02X:%02X:%02X:%02X:%02X ",
scanResults[ii].rssi(),
scanResults[ii].address()[5], scanResults[ii].address()[4], scanResults[ii].address()[3],
scanResults[ii].address()[2], scanResults[ii].address()[1], scanResults[ii].address()[0]);
// EXAMPLE - Device OS 2.x and earlier
Log.info("rssi=%d address=%02X:%02X:%02X:%02X:%02X:%02X ",
scanResults[ii].rssi,
scanResults[ii].address[5], scanResults[ii].address[4], scanResults[ii].address[3],
scanResults[ii].address[2], scanResults[ii].address[1], scanResults[ii].address[0]);
See also toString() for an easier way to print address string.
toString (BleAddress)
Since 1.3.1:
// PROTOTYPE
String toString(bool stripped = false) const;
// EXAMPLE
Log.info("rssi=%d address=%%s", scanResults[ii].rssi(), scanResults[ii].address().toString().c_str());
Converts a BleAddress to a String
. The default value of stripped
is false, which results in the
standard colon-separated format. If stripped
is true, then the raw 12 hex bytes with no separators
is returned.
equality (BleAddress)
You can test two BleAddress objects for equality (same address).
// PROTOTYPE
bool operator==(const BleAddress& addr) const
valid (BleAddress)
// PROTOTYPE
bool valid() const;
Since 3.0.0:
You can test if a BLEAddress object is valid using the valid()
method in Device OS 3.0.0 and later.
Getters
Often you will get the value of a BleAddress for debugging purposes.
// PROTOTYPE
BleAddressType type() const;
void octets(uint8_t addr[BLE_SIG_ADDR_LEN]) const;
String toString(bool stripped = false) const;
size_t toString(char* buf, size_t len, bool stripped = false) const;
hal_ble_addr_t halAddress() const;
uint8_t operator[](uint8_t i) const;
// EXAMPLE 1
Log.info("found device %s", scanResult->address.toString().c_str());
// EXAMPLE 2
Log.info("found device %02X:%02X:%02X:%02X:%02X:%02X",
scanResult->address[5], scanResult->address[4], scanResult->address[3],
scanResult->address[2], scanResult->address[1], scanResult->address[0]);
Constructor
Normally you won't construct a BleAddress as you get one back from scanning using BLE.scan()
. However there are numerous options:
// PROTOTYPE
BleAddress(const hal_ble_addr_t& addr);
BleAddress(const uint8_t addr[BLE_SIG_ADDR_LEN], BleAddressType type = BleAddressType::PUBLIC);
BleAddress(const char* address, BleAddressType type = BleAddressType::PUBLIC);
BleAddress(const String& address, BleAddressType type = BleAddressType::PUBLIC);
Setters
You will not normally need to set the value of a BleAddress, but there are methods to do so:
// PROTOTYPE
int type(BleAddressType type);
int set(const uint8_t addr[BLE_SIG_ADDR_LEN], BleAddressType type = BleAddressType::PUBLIC);
int set(const char* address, BleAddressType type = BleAddressType::PUBLIC);
int set(const String& address, BleAddressType type = BleAddressType::PUBLIC);