Understanding the GATT Protocol in BLE

Recently, I began to study the development of a device based on mircocontroller, which will have a BLE module. The device should send the analog reading received from the sensor to the Android application that I am going to develop.
What I learned about how GATT works:

  • The microcontroller-based device will be a GATT server.
  • The Android app will be a GATT client.
  • As can be seen from the point of view of communication, the device based on the micro-controller is subordinate, and the Android application is the master.

Questions:

  • How do I determine the number of attributes that I need to determine in order to receive a command from the GATT client and send a response (which will be a float value)? Do I need to have two different attributes: one for Android for sending commands and one for a device based on a microcontroller for sending data to Android? Or can I use one attribute?
  • GATT seems to be event driven.
    2.1. What events will be generated when android sends a command to a device based on a microcontroller: (Client to Server)?
    2.2: Will an event be generated when the data is written in the attribute that will be read by the Android application: (from server to client)?
  • The android application (GATT client) must use read / write commands to communicate with the microcontroller-based device (GATT Server). In addition, the GATT server must use Notify / Indicate to transmit data to the GATT client. Do I understand correctly?

I am using this BlueGiga BLE112 module for development.

The gatt.xml file I have written so far:

<?xml version="1.0" encoding="UTF-8" ?> <configuration> <!-- 1800: org.bluetooth.service.generic_access --> <service uuid="1800" id="generic_access"> <description>Generic Access</description> <!-- 2A00: org.bluetooth.characteristic.gap.device_name --> <characteristic uuid="2A00" id="c_device_name"> <description>Device Name</description> <properties read="true" const="true" /> <value>MyBLEDev</value> </characteristic> <!-- 2A01: org.bluetooth.characteristic.gap.appearance --> <characteristic uuid="2A01" id="c_appearance"> <description>Appearance</description> <properties read="true" const="true" /> <value type="hex">0300</value> </characteristic> </service> <!-- custom service --> <service uuid="624e957f-cb42-4cd6-bacc-84aeb898f69b" advertise="true"> <description>Custom Device Service</description> <!-- custom write-only characteristic for Client to send commands to fetch reading --> <characteristic uuid="a57892fe-4f58-97d4-a5245-78a4125d3e6" id="c_cmd_TxReading"> <description>Request for Reading</description> <properties write="true" /> <value length="4" /> </characteristic> <characteristic uuid="8fde302a-56ac-b289-65ed-a577ed66b89c" id="c_reading"> <description>Measurement</description> <properties read="true" write="true" /> <value length="4" type="float32" /> </characteristic> </service> 

+6
source share
1 answer

I see the GATT server as a piece of memory on another machine. You can request specific pieces with pens and get various information. You can force another machine to do different things or react differently by writing values ​​to these descriptors. The difference from the amount of memory is that each descriptor can contain different sizes of information, and each one has a UUID that identifies how to interpret the data that you find there. In a normal memory space, each “descriptor” will be an address, each fragment will be one byte, and there is no way to understand how to interpret this data without any other information.

So ... questions:

  • Like most questions here, the answer is "it depends." If you just want to get the value, you only have one attribute containing the data that the client can retrieve. If you also want to configure it so that the GATT server sends notifications when this value changes, you also need to add a client characteristic configuration descriptor to this attribute. (For example, I have one accelerometer that has 3 attributes for X, Y, and Z values ​​and another device that reports all 3 values ​​as one attribute. Since this is a type of value that has not been standardized, it can do this by defining them your own custom UUID. If you are measuring something that already has a standard layout, then you should probably use this instead)

  • GATT has some event-related aspects and other aspects that are executed sequentially. For example, you can only negotiate one connection request at a time. However, you can receive notifications in any order from any number of attributes at any time.

    • You cannot define your own commands using GATT. You are limited to things like “read from descriptor” or “write to process”, like manipulating a block of memory. The main implementation may depend on the hardware, but usually you can trigger some kind of event when manipulating the handle.

    • You can request events by subscribing to notifications or directions for a specific attribute.

  • Yes this is correct.

+8
source

Source: https://habr.com/ru/post/969515/


All Articles