This article is a summary of the implementation of the Bluetooth anti-loss device project, the principle of Bluetooth anti-loss device, the implementation of Bluetooth BLE interface with the Android client programming. Here is the focus on how to use the BLE interface to carry out the project, for BLE protocol, the BLE interface involved in the internal source code implementation, the author later detailed analysis. However, readers are required to have a certain understanding of BLE protocol, such as Gap, gattprofile in ble role and role, how to use service, characteristic and so on.
First, the principle of Bluetooth anti-loss device and product requirements
The core principle of the Bluetooth anti-drop device is to estimate the distance based on the wireless signal Strength (RSSI) received from the Bluetooth devices. The calculation formula is:
D is the computational distance, RSSI is the signal strength, a is the signal strength at 1 meters apart from the transmitter and receiver, and N is the environmental attenuation factor. For different Bluetooth devices This value is not the same, the same equipment in different transmitting power in the case of its signal strength is not the same, and for the same is 1 meters, the environment for the signal strength is also affected. n is the environmental attenuation factor, which is naturally related to the environment. Therefore, in the case of exact transmitting power, A and n are also an empirical value for the same device.
In the actual anti-loss device products, the following functions are generally:
1. When the mobile phone (receiver) detects that the transmitter device distance exceeds a certain distance, the alarm prompts, the device according to the alarm level of the corresponding instructions, such as the issuance of different frequencies of audio or flashing lights.
2. It automatically emits some form of warning when the transmitting device is found to be disconnected from the phone's established link (meaning that the distance has exceeded the connection range).
Second, Bluetooth anti-loss profile
The author of the industry is currently the lowest power consumption of Bluetooth single chip (dialog company's DA14580) to illustrate. For Da14580,dialog Company has provided development SDK (later will be analysis of the SDK Framework to guide development), there is the implementation of anti-loss profile, named proximity. The profile provides the following characteristic for the above anti-loss function:
1. TXP (txpower) characteristic, the device side needs to use the host control Interface HCI to obtain the transmit power parameters, and the Read property is provided to master.
2. IAS (immediate alter service), the Write property, for master to write the alarm level. When Master writes a new value, the device side receives a write callback, which is alerted according to the alarm level.
3. LLS (link loss service), Write/read property, for master to set the default alarm level for link disconnection.
Rssi is obtained through the interface of the receiving end and does not require a service on the device side.
The above characteristic through the GATT profile to provide services, in the Bluetooth communication protocol, each characteristic will correspond to a UUID.
Third, Android Bluetooth BLE interface programming
The Androidble interface is provided above the android4.3 version.
1. Determine if the current system supports BLE
Getpackagemanager (). Hassystemfeature (Packagemanager. Feature_bluetooth_le)
Returns true representation support.
2. Get the Bluetooth adapter class
Users use the BLE API through a unified Bluetooth adapter class Bluetoothadapter.
Get the Bluetooth manager first:
Bluetoothmanagerbluetoothmanager = Getsystemservice (Context. Bluetooth_service);
Then get the Bluetooth adapter instance (Monomer object):
Bluetoothadaptermbluetoothadapter = Bluetoothmanager.getadapter ();
3. Activate the Bluetooth hardware function (equivalent to turn on the Bluetooth function in the settings screen)
Mbluetoothadapter.enable ();
4. Start scanning
Bluetoothadapter.startlescan (Android.bluetooth.BluetoothAdapter.LeScanCallbackcallback)
Callback is the callback interface when scanning to a Bluetooth device. Implement the Onlescan interface in callback:
@Override
Public void Onlescan (finalbluetoothdevice device, int rssi, byte[] scanrecord)
Where device represents the scanned devices, can obtain their MAC address, device name and so on; Rssi is the signal strength, which is the method of acquiring Rssi when not connected; Scanrecord represents the response parameters of the scanning device, and ibeacon the broadcast content by this parameter.
Assuming string bluetoothaddress = Device.getaddress (), get the Bluetooth 48-bit MAC address
5. Connect to GATT, get the UUID service on the device side, and interact with the data communication
Get the Bluetooth device class on behalf of the device by MAC address
Bluetoothdevicedevice = Mbluetoothadapter.getremotedevice (bluetoothaddress);
Connecting GATT
Bluetoothgatt Mbluetoothgatt = Device.connectgatt (Android.content.Context Context, Booleanautoconnect, Android.bluetooth.BluetoothGattCallback callback);
Callback is the callback entry for all data interactions after the GATT is connected. Each includes:
1) Equipment Service discovery
@Override
publicvoid onservicesdiscovered (bluetoothgatt GATT, int status)
Mbluetoothgatt.getservices () represents a collection of device services,
for (Bluetoothgattservice gattService:mBluetoothGatt.getServices ())
For each service serving, uuid,getcharacteristics (), which is available with Getuuid (), represents the characteristic collection of the service.
for (Bluetoothgattcharacteristic gattcharacteristic:gattcharacteristics)
For each characteristic,getuuid () get Uuid,getpermissions () Get property permission, GetValue () Gets the property value.
In this callback we only extract the UUID of the three characteristic of interest, for other UUID such as battery, device service and so on.
gattcharacteristic_char5_txp=gattcharacteristic;
2) Connection Status change
@Override
Public voidOnconnectionstatechange (bluetoothgatt GATT, int status,intnewstate)
There are two kinds of states, bluetoothprofile. state_connected represents the connection, Bluetoothprofile. The state_disconnected represents a disconnect.
3) Read callback
@Override
Public voidOncharacteristicread (Bluetoothgatt GATT, bluetoothgattcharacteristiccharacteristic, int status)
The callback when the data on the device side is received when a read request is made on the phone side. Such as
Mbluetoothgatt.readcharacteristic (gattcharacteristic_char5_txp)
4) device-side data change callback
The characteristic property of the corresponding device here is notify or indication, that is, the equivalent of the mobile phone to subscribe to this characteristic value change service, when the device side of the characteristic changed, The device side will actively notify the mobile phone side.
@Override
Public voidoncharacteristicchanged (Bluetoothgatt GATT,
bluetoothgattcharacteristiccharacteristic)
Get the new value Characteristic.getvalue () in the callback.
5) Get a callback to the RSSI value
Rssi can be obtained by scanning the callback interface when scanning, but it must be used continuously after the connection
Mbluetoothgatt.readremoterssi () sends a read RSSI request to the underlying driver, and the following callback is made when the underlying acquisition of the new RSSI:
@Override
Public voidOnreadremoterssi (bluetoothgatt GATT, int rssi, int status)
RSSI is the new signal strength value.
After the connection, because the mobile phone and the device side distance is changing, so to constantly read Rssi, real-time calculation of the distance between the two to ensure the realization of the anti-loss function.
[Yueqian_scut] Bluetooth anti-loss principle, implementation and Android BLE interface programming