2. About BLE,BT Explanation
BLE, it is important to understand that all the profile and
service support is entirely in the application space.
This means that with BLE, there isn't really a need for a
serial port service to transmit custom data, since you can
easily make your custom service, that is specially tailored
for the data your application needs to transfer.
3. About BLE,BT Explanation
This avoids the need for cramming all kinds of data into
serial packages, and most often this leads to much cleaner
data handling strategies and hence applications.
4. About BLE,BT create custom profile
To create a custom service, you should try to follow the
general flow of the existing services, and mor or less
copy them. The battery service (ble_bas.c/ble_bas.h) is
probably the simplest one, and should provde a good
starting point.
If you look at it, you can see that the init function first
adds a service, using the sd_ble_gatts_service_add()
function, then calls an internal function to add a
characteristic. This internal functions sets up a lot of
parameter structures, that in different ways affect the
characteristic added.
5. About BLE,BT Explanation
Most services also needs to know what happens on run-
time, so you need to create a event handler to handle
events coming from the stack. This should be named
service_name_on_ble_evt(), and be called from the
applications main.c-file's ble_evt_dispatch() method.
Finally, most services also need some API function to
actually do something useful, for example the battery
service's method to update the measured battery value.
7. Builder custom profile
Client v.s Server
Client is no have data(information).
Client can send Request, Command and Confirmation
Server is have data .(information,so the device usually is
server)
Server can send Response, Notification and Indication
25. Builder custom profile
1.main loop( have to sure the order function caller)
int main(void)
{
ble_stack_init(); //Encodes the required advertising data and passes it to the stack
bond_manager_init();//if it have .
gap_params_init();
//GAP initialization, setup all the necessary GAP,set device name
//permissions and appearance,reference appearance device
advertising_init(); //adverrising the service (profile),advertising data to stack
services_init(); //** advertising data and passes it to the stack
conn_params_init();//Connection Parameters module
sec_params_init();//security parameters
radio_notification_init();//Radio Notification event handler
// Start execution
application_timers_start();
advertising_start();
}
26. Builder custom profile
Static void services_init(void)
init function first adds a service, using the
sd_ble_gatts_service_add() function, then calls an internal
function to add a characteristic. This internal functions sets
up a lot of parameter structures, that in different ways affect
the characteristic added.
what happens on run-time, so you need to create a event
handler to handle events coming from the stack. This should
be named service_name_on_ble_evt(), and be called from
the applications main.c-file's ble_evt_dispatch() method.
1.caller sd_ble_gatts_service_add() then add char,last
register the event handler
34. /**@brief Battery Service event type. */
typedef enum
{
BLE_BAS_EVT_NOTIFICATION_ENABLED,
value notification enabled event. */
BLE_BAS_EVT_NOTIFICATION_DISABLED
value notification disabled event. */
} ble_bas_evt_type_t;
/**< Battery
/**< Battery
/**@brief Battery Service event. */
typedef struct
{
ble_bas_evt_type_t evt_type;
} ble_bas_evt_t;
/**< Type of event. */
35.
/**@brief Battery Service init structure. This contains all options and data needed for
*
initialization of the service.*/
typedef struct
{
ble_bas_evt_handler_t
evt_handler;
/**< Event handler to be called for
handling events in the Battery Service. */
bool
support_notification;
/**< TRUE if notification of Battery
Level measurement is supported. */
ble_srv_report_ref_t *
p_report_ref;
/**< If not NULL, a Report Reference
descriptor with the specified value will be added to the Battery Level characteristic */
uint8_t
initial_batt_level;
/**< Initial battery level */
ble_srv_cccd_security_mode_t battery_level_char_attr_md; /**< Initial security level for
battery characteristics attribute */
ble_gap_conn_sec_mode_t
battery_level_report_read_perm; /**< Initial security level
for battery report read attribute */
} ble_bas_init_t;
//for pc
36.
/**@brief Battery Service structure. This contains various status information for the service. */
typedef struct ble_bas_s
{
ble_bas_evt_handler_t
evt_handler;
/**< Event handler to be called for
handling events in the Battery Service. */
uint16_t
service_handle;
/**< Handle of Battery Service (as
provided by the BLE stack). */
ble_gatts_char_handles_t
battery_level_handles;
/**< Handles related to the
Battery Level characteristic. */
uint16_t
report_ref_handle;
/**< Handle of the Report Reference
descriptor. */
uint8_t
battery_level_last;
/**< Last Battery Level measurement
passed to the Battery Service. */
uint16_t
conn_handle;
/**< Handle of the current connection (as
provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
bool
is_notification_supported;
/**< TRUE if notification of Battery
Level is supported. */
} ble_bas_t;
37.
// Initialize Battery Service
memset(&bas_init, 0, sizeof(bas_init));
// Here the sec level for the Battery Service can be changed/increased.
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.cccd_write_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init.battery_level_char_attr_md.write_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_report_read_perm);
bas_init.evt_handler
= NULL;
bas_init.support_notification = true;
bas_init.p_report_ref
= NULL;
bas_init.initial_batt_level = 100;
err_code = ble_bas_init(&m_bas, &bas_init);
APP_ERROR_CHECK(err_code);
41. 以battery service 中,由上可知有一個characteristics/及
一個CCCD .而每一個characteristics由上到下:
1. ble_gatts_char_md_t char_md;//GATT Characteristic metadata
Characteristic Properties, user description descriptor, Pointer to a
presentation format structure and Attribute metadata for the
Client(server) Characteristic Configuration Descriptor.
ble_gatts_attr_md_t attr_md;//Attribute metadata
A.
A.
Read(write) permissions, Variable length, Value location and
Read(write) Authorization on every read(write) operation
ble_gatts_attr_md_t cccd_md //Attribute metadata
B.
A.
Read(write) permissions, Variable length, Value location and
Read(write) Authorization
42. 2.
ble_gatts_attr_t
attr_char_value;//GATT Attribute
Pointer to the attribute UUID, Pointer to the attribute metadata
structure, attribute value length in bytes and Pointer to the attribute
data if the @ref BLE_GATTS_VLOC_USER value location is
selected in the attribute metadata, this will have to point to a buffer
43.
/**@brief GATT Characteristic metadata. */
typedef struct
{
ble_gatt_char_props_t
char_props;
/**< Characteristic Properties. */
ble_gatt_char_ext_props_t char_ext_props;
/**< Characteristic Extended Properties. */
uint8_t*
p_char_user_desc;
/**< Pointer to a UTF-8, NULL if the descriptor is not
required. */
uint16_t
char_user_desc_max_size; /**< The maximum size in bytes of the user description
descriptor. */
uint16_t
char_user_desc_size;
/**< The size of the user description, must be smaller or
equal to char_user_desc_max_size. */
ble_gatts_char_pf_t*
p_char_pf;
/**< Pointer to a presentation format structure or NULL if
the descriptor is not required. */
ble_gatts_attr_md_t*
p_user_desc_md;
/**< Attribute metadata for the User Description
descriptor, or NULL for default values. */
ble_gatts_attr_md_t*
p_cccd_md;
/**< Attribute metadata for the Client Characteristic
Configuration Descriptor, or NULL for default values. */
ble_gatts_attr_md_t*
p_sccd_md;
/**< Attribute metadata for the Server Characteristic
Configuration Descriptor, or NULL for default values. */
} ble_gatts_char_md_t;
44. /**@brief GATT Characteristic Properties. */
typedef struct
{
/* Standard properties */
uint8_t broadcast
:1; /**< Broadcasting of value permitted. */
uint8_t read
:1; /**< Reading value permitted. */
uint8_t write_wo_resp :1; /**< Writing value with Write Command permitted. */
uint8_t write
:1; /**< Writing value with Write Request permitted. */
uint8_t notify
:1; /**< Notications of value permitted. */
uint8_t indicate
:1; /**< Indications of value permitted. */
uint8_t auth_signed_wr :1; /**< Writing value with Signed Write Command
permitted. */
} ble_gatt_char_props_t;
45. /**@brief Attribute metadata. */
typedef struct
{
ble_gap_conn_sec_mode_t read_perm;
/**< Read permissions. */
ble_gap_conn_sec_mode_t write_perm;
/**< Write permissions. */
uint8_t
vlen
:1; /**< Variable length attribute. */
uint8_t
vloc
:2; /**< Value location, see @ref
BLE_GATTS_VLOCS.*/
uint8_t
rd_auth :1; /**< Read Authorization and value will
be requested from the application on every read operation. */
uint8_t
wr_auth :1; /**< Write Authorization will be
requested from the application on every Write Request operation (but not
Write Command). */
} ble_gatts_attr_md_t;
46.
/**@brief GATT Attribute. */
typedef struct
{
ble_uuid_t*
p_uuid;
/**< Pointer to the attribute UUID. */
ble_gatts_attr_md_t* p_attr_md;
/**< Pointer to the attribute metadata structure. */
uint16_t
init_len;
/**< Initial attribute value length in bytes. */
uint16_t
init_offs;
/**< Initial attribute value offset in bytes. If different from zero, the first
init_offs bytes of the attribute value will be left uninitialized. */
uint16_t
max_len;
/**< Maximum attribute value length in bytes, see @ref
BLE_GATTS_ATTR_LENS_MAX for maximum values. */
uint8_t*
p_value;
/**< Pointer to the attribute data. Please note that if the @ref
BLE_GATTS_VLOC_USER value location is selected in the attribute metadata, this will have to point to a
buffer
that remains valid through the lifetime of the attribute. This excludes usage
of automatic variables that may go out of scope or any other temporary location.
The stack may access that memory directly without the application's
knowledge. */
} ble_gatts_attr_t;
61. Nordic FrameWork support service
Alert Notification Service Client,ble_ans_c
Energon bank Service module, ble_band
Battery Service module,ble_bas
Blood Pressure Service module. ble_bps
Cycling Speed and Cadence Service, ble_cscs
Device Information Service, ble_dis
Glucose Service module. ble_gls
Glucose Database Service. ble_gls_db
Human Interface Device Service, ble_hids
62. Nordic FrameWork support service
Heart Rate Service ,ble_hrs
Health Thermometer Service ,ble_hts
Immediate Alert Service,ble_ias
Immediate Alert Service Client,ble_ias_c
Link Loss Service,ble_lls
Running Speed and Cadence Service,ble_rscs
TX Power Service,ble_tps
63. Reference
Battery Service (BAS)
http://developer.bluetooth.org/TechnologyOverview/Pages/B
AS.aspx
The external appearance of this
https://developer.bluetooth.org/gatt/characteristics/Pa
ges/CharacteristicViewer.aspx?u=org.bluetooth.characte
ristic.gap.appearance.xml