Build World Class Health Tech Products with Bluetooth - Part 1.
What is Bluetooth(BLE) Custom protocol?
Short Intro about Bluetooth?
There are two major technologies within the bluetooth core specification
Bluetooth Classic and
Bluetooth Smart(Bluetooth Low Energy).
Bluetooth Low Energy (BLE) is a wireless communication protocol designed for low power consumption.
BLE Specs:
Power Consumption.
Application with Periodic transfer of data.
Simultaneous connections upto 20.
Protocols & Layers
BLE has 3 layers:
Application.
Host.
Controller.
Advertising & Scanning
BLE has two types of packets (Advertising and data packets). Advertising packet:
Discover slaves & connect
Broadcast data for Application
Advertising packets can carry up to 31 Bytes of data payload along with the header information with the advertising interval from 20ms to 10.24s.
The shorter advertising interval increase the frequency at which advertising packets are broadcast.
Advertising uses a maximum of 3 frequency channels (37, 38 & 39).
GAP(Generic Access Profile)
GAP is a mechanism by which two devices get connected. E.g: Mobile Phone and Blood Glucose Monitor (device)
GAP specifies 4 roles:
Broadcaster - transmit only application.
Observer - receive only applications.
Central - multiple connections e.g smartphone (GATT Client).
Peripheral - use advertising packet to allow central to find it
To build Bluetooth Blood Glucose Monitor we need Peripheral type.
GATT(Generic Attribute Profile)
GATT is the way that connected devices exchange data. GAP enables the connection and GATT to exchange the data.
The device which wants to read or write the data is called the GATT client. E.g Cell Phone. All BLE peripherals are required to implement a GAP and a GATT service. GAP is a protocol that defines how the device connects. We can configure the advertising parameters for the device in GAP.
E.g Device Name, Device address OR UUID, Local Name, Service UUID, Service Data, etc.
BLE Custom Profile
A custom protocol in BLE refers to a user-defined protocol that is based on the BLE protocol, but has been customized to meet the specific needs of a particular application or use case. Custom protocols can be used to add additional functionality or features to BLE, or to optimize the protocol for a particular type of device or network. BLE custom protocols are often used in IoT (Internet of Things) applications, where they can help to improve the performance, reliability, and security of the network. Credit: #ChatGPT.
Services & Characteristics
Service is a thing that the device can do, & the characteristics are the information that can be shared. Both the service and characteristics are specified by Bluetooth. We can create our service and characteristics.
Blood Glucose Custom Profile
Service UUID: 0xDTS11
Characteristics:
UUID:
0xDTSC1
Localname:DT-MEASURE2
.UUID:
0xDTSC3
Localname:DT-Control Point
React Native BLE Manager
Let's suppose you have all details about the sensor you need to connect your mobile to fetch data from, we need the Library to connect from Mobile App, bets way is to go with React Native.
Installing ble manager in React Native:
npm i --save react-native-ble-manager
For Android, we need to add the following permissions in AndroidManifest.xml file
<uses-permissionandroid:name="android.permission.BLUETOOTH"/>
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" />
Usage
- Initialize the BLE Manager for this First we need to import BleManager in our File.
import BleManager from'react-native-ble-manager';
BleManager.start({ showAlert: false });
- Add listener to handle the stopScan, discoverDevices and disconnect devices & notify.
BleManager.scan([], 5, true).then(() =>
{
//Scan BLE Devices for 5 Seconds
console.log('Scan started'); // Success code});
}
HandleDiscoverPeripheral gets the devices list & update on the UI.
Connection and Disconnection
BleManager.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX') .then(() => { console.log('Connected'); }). catch((error) => { console.log(error); }); }); BleManager.disconnect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'). then(() => { console.log('Connected');}). catch((error) => { console.log(error); });
Read Data from BLE Device
To read the current value of specified characteristics ID we need to call read method with deviceId, ServiceUUID & CharacteristicsUUID.
Before calling read we need to call retrieve services to get services and characteristics of BLE Device.
BleManager.retrieveServices(deviceId).then((peripheralInfo) => {BleManager.read(deviceId, ServiceUUID, CharacteristicsUUID).then((readData) => { console.log('Read: '+ readData); }). catch((error) => { console.log(error); }); });
Write Data to BLE Device
For writing data to BLE Device we need to first call retrieve services & then We need to call write function with deviceId, ServiceUUID & Characteristics UUID.
For write function data must be in byte format
BleManager.retrieveServices(deviceId).then((peripheralInfo) => {BleManager.write(deviceId, ServiceUUID, CharacteristicsUUID, data). then(() => { console.log('Write: '+ data); }) // Data Must be in Byte Format. catch((error) => { console.log(error); }); });
Notify Service
For Notify also we need to call retrieve services first & then we need to call startNotification() deviceId, ServiceUUID & CharacteristicsUUID.Whenever the device sends data notify service gets called and we can read data through notify.
BleManager.retrieveServices(deviceId). then((peripheralInfo) => { BleManager.startNotification(deviceId, ServiceUUID, CharacteristicsUUID). then(() => { console.log('Notification started'); }). catch((error) => { console.log(error); }); });
Custom Commands in BLE
We can write custom commands to BLE device using write functionality.
We can use
BleManager.write()
to write our custom command.
We need to pass the command data in the byte format.
And we will get response in notify service.
E.g. '@110101020000000*'
This is the input format for write custom command.
11 is Command Type,
01 -> Data Type,
01 -> Data Type Size &
02 -> No. of Data in Packet
@ & * are the start and end indicator for data & remaining 0’s for completing 17 bytes in complete command.
About Delighteck
Delighteck is your digital innovation partner. We help organisations to build human-centric technology solutions through cutting-edge services.
Delighteck helps with strategies and provides product development services for Health Tech. Do talk to us! contact@delighteck.com | +65 83854477