pub struct FakeBle {
pub show_pa_level: bool,
pub mac_address: [u8; 6],
/* private fields */
}Expand description
A struct that implements BLE functionality.
This implementation is subject to Limitations.
Use ble_config() to properly configure the radio for BLE compatibility.
use rf24::radio::{prelude::*, RF24};
use rf24ble::{ble_config, radio::FakeBle};
let mut radio = RF24::new(ce_pin, spi_device, delay_impl);
radio.init()?;
radio.withConfig(&ble_config())?;
let mut ble = FakeBle::new();
radio.print_details()?;Fields§
§show_pa_level: boolEnable or disable the inclusion of the radio’s PA level in advertisements.
Enabling this feature occupies 3 bytes of the 18 available bytes in advertised payloads.
mac_address: [u8; 6]Set or get the BLE device’s MAC address.
A MAC address is required by BLE specifications. Use this attribute to uniquely identify the BLE device.
Implementations§
Source§impl FakeBle
impl FakeBle
Sourcepub fn new() -> Self
pub fn new() -> Self
Instantiate a BLE device using a given instance of RF24.
The radio object is consumed because altering the radio’s setting will
instigate unexpected behavior.
Sourcepub fn set_name(&mut self, name: &str)
pub fn set_name(&mut self, name: &str)
Set the BLE device’s name for inclusion in advertisements.
Setting a BLE device name will occupy more bytes from the
18 available bytes in advertisements. The exact number of bytes occupied
is the length of the given name buffer plus 2.
The maximum supported name length is 10 bytes. So, up to 12 bytes (10 + 2) will be used in the advertising payload.
Sourcepub fn get_name(&self, name: &mut [u8]) -> u8
pub fn get_name(&self, name: &mut [u8]) -> u8
Get the current BLE device name included in advertisements.
If no name is set (with FakeBle::set_name()), then this function
does nothing.
If a BLE device name has been set, then this function
stores the bytes of the name in the given name buffer.
This function returns the number of bytes changed in the given name buffer.
Sourcepub fn len_available(&self, hypothetical: &[u8]) -> i8
pub fn len_available(&self, hypothetical: &[u8]) -> i8
How many bytes are available in an advertisement payload?
The hypothetical parameter shall be the same buf value passed to FakeBle::send().
In addition to the given hypothetical payload length, this function also
accounts for the current state of FakeBle::get_name() and
FakeBle::show_pa_level.
If the returned value is less than 0, then the hypothetical payload will not
be broadcasted.
Sourcepub fn hop_channel<SPI, DO, DELAY>(
&self,
radio: &mut RF24<SPI, DO, DELAY>,
) -> Result<(), Nrf24Error<SpiError, OutputPinError>>
pub fn hop_channel<SPI, DO, DELAY>( &self, radio: &mut RF24<SPI, DO, DELAY>, ) -> Result<(), Nrf24Error<SpiError, OutputPinError>>
Hop the radio’s current channel to the next BLE compliant frequency.
Use this function after FakeBle::send() to comply with BLE specifications.
This is not required, but it is recommended to avoid bandwidth pollution.
Sourcepub fn make_payload(
&self,
buf: &[u8],
pa_level: Option<PaLevel>,
channel: u8,
) -> Option<[u8; 32]>
pub fn make_payload( &self, buf: &[u8], pa_level: Option<PaLevel>, channel: u8, ) -> Option<[u8; 32]>
Create a buffer to be used as a BLE advertisement payload.
This is a helper method to FakeBle::send(), but it is publicly exposed for
advanced usage only (eg. FFI binding).
If the resulting payload length is larger than 32 bytes, then None is returned.
Sourcepub fn send<SPI, DO, DELAY>(
&self,
radio: &mut RF24<SPI, DO, DELAY>,
buf: &[u8],
) -> Result<bool, Nrf24Error<SpiError, OutputPinError>>
pub fn send<SPI, DO, DELAY>( &self, radio: &mut RF24<SPI, DO, DELAY>, buf: &[u8], ) -> Result<bool, Nrf24Error<SpiError, OutputPinError>>
Send a BLE advertisement
The buf parameter takes a buffer that has been already formatted for
BLE specifications.
See our convenient API to
- advertise a Battery’s remaining change level:
BatteryService - advertise a Temperature measurement:
TemperatureService - advertise a URL:
UrlService
For a custom/proprietary BLE service, the given buf must adopt compliance with BLE specifications.
For example, a buffer of n bytes shall be formed as follows:
| index | value |
|---|---|
0 | n - 1 |
1 | 0xFF |
2 ... n - 1 | custom data |
Sourcepub fn read<SPI, DO, DELAY>(
&self,
radio: &mut RF24<SPI, DO, DELAY>,
) -> Result<Option<BlePayload>, Nrf24Error<SpiError, OutputPinError>>
pub fn read<SPI, DO, DELAY>( &self, radio: &mut RF24<SPI, DO, DELAY>, ) -> Result<Option<BlePayload>, Nrf24Error<SpiError, OutputPinError>>
Read the first available payload from the radio’s RX FIFO
and decode it into a BlePayload.
The payload must be decoded while the radio is on the same channel that it received the data. Otherwise, the decoding process will fail.
Use RF24::available to
check if there is data in the radio’s RX FIFO.
If the payload was somehow malformed or incomplete,
then this function returns a None value.