mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-11 00:40:00 +00:00
draft of le peripheral arduino example
This commit is contained in:
parent
69308c9e60
commit
bed744a565
@ -4,5 +4,5 @@ pages:
|
|||||||
- [index.md, Welcome]
|
- [index.md, Welcome]
|
||||||
- [tutorial.md, Tutorial]
|
- [tutorial.md, Tutorial]
|
||||||
- [examples/generated.md, Examples]
|
- [examples/generated.md, Examples]
|
||||||
- [appendix/apis.md, API]
|
#- [appendix/apis.md, API]
|
||||||
theme: readthedocs
|
theme: readthedocs
|
||||||
|
@ -4,8 +4,20 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* EXAMPLE_START(LEPeripheral): LE Peripheral
|
* EXAMPLE_START(LEPeripheral): LE Peripheral
|
||||||
|
*
|
||||||
|
* @text BTstack allows to setup a GATT Services and Characteristics directly
|
||||||
|
* from the setup function without using other tools outside of the Arduino IDE.
|
||||||
|
*
|
||||||
|
* @section Setup
|
||||||
|
*
|
||||||
|
* @text First, a number of callbacks are set. Then, a Service with a Read-only
|
||||||
|
* Characteristic and a dynamic Characteristic is added to the GATT database.
|
||||||
|
* In BTstack, a dynamic Characteristic is a Characteristic where reads and writes
|
||||||
|
* are forwarded to the Sketch. In this example, the dynamic Characteristic is
|
||||||
|
* provided by the single byte variable characteristic_data.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* LISTING_START(LEPeripheralSetup): Setup */
|
||||||
static char characteristic_data = 'H';
|
static char characteristic_data = 'H';
|
||||||
|
|
||||||
void setup(void){
|
void setup(void){
|
||||||
@ -32,12 +44,18 @@ void setup(void){
|
|||||||
BTstack.startAdvertising();
|
BTstack.startAdvertising();
|
||||||
// BTstack.enablePacketLogger();
|
// BTstack.enablePacketLogger();
|
||||||
}
|
}
|
||||||
|
/* LISTING_END(LEPeripheralSetup): Setup */
|
||||||
|
|
||||||
void loop(void){
|
void loop(void){
|
||||||
BTstack.loop();
|
BTstack.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @section Device Connected Callback
|
||||||
|
*
|
||||||
|
* @text When a remove device connects, device connected callback is callec.
|
||||||
|
*/
|
||||||
|
/* LISTING_START(LEPeripheralDeviceConnectedCallback): Device Connected Callback */
|
||||||
void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
|
void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
|
||||||
switch (status){
|
switch (status){
|
||||||
case BLE_STATUS_OK:
|
case BLE_STATUS_OK:
|
||||||
@ -47,17 +65,31 @@ void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* LISTING_END(LEPeripheralDeviceConnectedCallback): Device Connected Callback */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @section Device Disconnected Callback
|
||||||
|
*
|
||||||
|
* @text If the connection to a device breaks, the device disconnected callback
|
||||||
|
* is called.
|
||||||
|
*/
|
||||||
|
/* LISTING_START(LEPeripheralDeviceDisconnectedCallback): Device Disconnected Callback */
|
||||||
void deviceDisconnectedCallback(BLEDevice * device){
|
void deviceDisconnectedCallback(BLEDevice * device){
|
||||||
Serial.println("Disconnected.");
|
Serial.println("Disconnected.");
|
||||||
}
|
}
|
||||||
|
/* LISTING_END(LEPeripheralDeviceDisconnectedCallback): Device Disconnected Callback */
|
||||||
|
|
||||||
// ATT Client Read Callback for Dynamic Data
|
/*
|
||||||
// - if buffer == NULL, don't copy data, just return size of value
|
* @section Read Callback
|
||||||
// - if buffer != NULL, copy data and return number bytes copied
|
*
|
||||||
// @param characteristic_id to be read
|
* @text In BTstack, the Read Callback is first called to query the size of the
|
||||||
// @param buffer
|
* Charcteristic Value, before it is called to provide the data.
|
||||||
// @param buffer_size
|
* Both times, the size has to be returned. The data is only stored in the provided
|
||||||
|
* buffer, if the buffer argeument is not NULL.
|
||||||
|
* If more than one dynamic Characteristics is used, the value handle is used
|
||||||
|
* to distinguish them.
|
||||||
|
*/
|
||||||
|
/* LISTING_START(LEPeripheralReadCallback): Read Callback */
|
||||||
uint16_t gattReadCallback(uint16_t value_handle, uint8_t * buffer, uint16_t buffer_size){
|
uint16_t gattReadCallback(uint16_t value_handle, uint8_t * buffer, uint16_t buffer_size){
|
||||||
if (buffer){
|
if (buffer){
|
||||||
Serial.print("gattReadCallback, value: ");
|
Serial.print("gattReadCallback, value: ");
|
||||||
@ -66,16 +98,22 @@ uint16_t gattReadCallback(uint16_t value_handle, uint8_t * buffer, uint16_t buff
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
/* LISTING_END(LEPeripheralDeviceDisconnectedCallback): Read Callback */
|
||||||
|
|
||||||
// ATT Client Write Callback for Dynamic Data
|
/*
|
||||||
// @param characteristic_id to be read
|
* @section Write Callback
|
||||||
// @param buffer
|
*
|
||||||
// @param buffer_size
|
* @text When the remove device writes a Characteristic Value, the Write callback
|
||||||
// @returns 0 if write was ok, ATT_ERROR_INVALID_OFFSET if offset is larger than max buffer
|
* is called. The buffer arguments points to the data of size size/
|
||||||
int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t buffer_size){
|
* If more than one dynamic Characteristics is used, the value handle is used
|
||||||
|
* to distinguish them.
|
||||||
|
*/
|
||||||
|
/* LISTING_START(LEPeripheralWriteCallback): Write Callback */
|
||||||
|
int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t size){
|
||||||
characteristic_data = buffer[0];
|
characteristic_data = buffer[0];
|
||||||
Serial.print("gattWriteCallback , value ");
|
Serial.print("gattWriteCallback , value ");
|
||||||
Serial.println(characteristic_data, HEX);
|
Serial.println(characteristic_data, HEX);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
/* LISTING_END(LEPeripheralWriteCallback): Write Callback */
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user