Recent reports indicate that Samsung is set to launch the Galaxy Note 8 at the end of August, several months ahead of iOS phones entering the market. Rumors about the Note 8 surfaced back in April, suggesting that the phone will have a narrower bezel, a larger screen, and dual cameras. Traditionally, after releasing the Note smartphone, Samsung typically launches a variant of the Note device. This new device belongs to the Note series and features a larger screen, an integrated stylus, and combines smartphone and tablet functionalities.
Over the weekend, several cities across the country experienced heavy rainfall, which was the largest downpour I’ve encountered this year. I’m curious if it rained in your city as well? Today, we return to our work routine, and I hope everyone has a vibrant start to the new week!
This article is contributed by Phantom Universe, sharing a detailed guide on the use of the Android BLE basic framework, hoping to assist everyone.
Phantom Universe‘s blog address:
http://my.csdn.net/xiaoyaoyou1212
After researching Bluetooth usage for a while, I found that the related information online is quite scarce, unlike other Android-related documentation which is comprehensive; most of the information can only be found on the official website provided by the Bluetooth Special Interest Group (SIG).
https://www.bluetooth.com/zh-cn/specifications
There is no stable and easy-to-use basic operational framework available, and the various issues encountered during development can be very frustrating. In this context, this framework was born, containing the basic operational functionalities for Bluetooth devices, with simple calls, validated through practical projects, and equipped with a user-friendly demo. The following text mainly discusses how to use this framework. If you are not very familiar with Android BLE, you can first take a look at my blog post Android BLE Learning Notes, which provides a detailed explanation of the basic knowledge of Bluetooth.
The Android BLE basic operational framework is callback-based and easy to operate. It includes functionalities such as scanning, connecting, parsing broadcast packets, reading and writing services, and notifications.
-
Project address: https://github.com/xiaoyaoyou1212/BLE
-
Project introduction: compile ‘com.vise.xiaoyaoyou:baseble:1.0.5’
Device Scanning
-
Usage Overview
Scanning includes three methods. The first method is to scan all devices directly. You can set up a loop scan or a timeout; the scanned devices can be added to the BluetoothLeDeviceStore for unified processing. The usage is as follows:
The second method is to scan devices with a specified MAC address, usually requiring a timeout setting. Scanning will stop once the specified device is found. The usage is as follows:
The third method is to scan devices with a specified broadcast name, similar to the second method, also requiring a timeout setting. Scanning will stop once the specified device is found. The usage is as follows:
The information of the scanned devices is uniformly placed in BluetoothLeDevice , which contains all the information about the device. The following will detail what information is included.
-
Sample Image
Device Connection
-
Usage Overview
Connecting also has three methods. The first method is to use the device information obtained from scanning BluetoothLeDevice, with a configurable connection timeout, defaulting to 10 seconds. The usage is as follows:
The second method is to connect to a device with a specified MAC address. This method does not require prior scanning; it combines scanning and connecting. Once the specified device is found, it connects automatically. The usage is as follows:
The third method is to connect to a device with a specified name, similar to the second method. The usage is as follows:
Once connected successfully, you can perform related operations. Callbacks have been handled with thread switching at the lower level, allowing direct manipulation of the view. If you know the UUID of the device service, you can directly call ViseBluetooth.getInstance().withUUIDString(serviceUUID, characteristicUUID, descriptorUUID); In this case, you do not need to pass the characteristic (BluetoothGattCharacteristic) and descriptor (BluetoothGattDescriptor) parameters when operating the device. If you do not set the UUID after a successful connection, you will need to pass these parameters during operations. This content will be explained in detail in the device operation section below, so I won’t elaborate here.
-
Sample Image
Device Details
-
Usage Overview
DEVICE INFO (Device Information)
-
Get Device Name (Device Name): bluetoothLeDevice.getName();
-
Get Device Address (Device Address): bluetoothLeDevice.getAddress();
-
Get Device Class (Device Class): bluetoothLeDevice.getBluetoothDeviceClassName();
-
Get Major Device Class (Major Class): bluetoothLeDevice.getBluetoothDeviceMajorClassName();
-
Get Service Class (Service Class): bluetoothLeDevice.getBluetoothDeviceKnownSupportedServices();
-
Get Bonding State (Bonding State): bluetoothLeDevice.getBluetoothDeviceBondState();
RSSI INFO (Signal Information)
-
Get First Timestamp (First Timestamp): bluetoothLeDevice.getFirstTimestamp();
-
Get First RSSI (First RSSI): bluetoothLeDevice.getFirstRssi();
-
Get Last Timestamp (Last Timestamp): bluetoothLeDevice.getTimestamp();
-
Get Last RSSI (Last RSSI): bluetoothLeDevice.getRssi();
-
Get Running Average RSSI (Running Average RSSI): bluetoothLeDevice.getRunningAverageRssi();
SCAN RECORD INFO (Broadcast Information)
Based on the scanned broadcast packets AdRecordStore, get the type number of a broadcast data unit using record.getType(), then obtain the type description of the broadcast data unit using record.getHumanReadableType() along with its length and data content. Finally, convert the data content into a specific string using AdRecordUtil.getRecordDataAsString(record).
-
Sample Image
Device Operations
-
Usage Overview
Before operating the device, ensure that it is successfully connected. Once you obtain the BluetoothGatt after a successful connection, you can directly process the service’s characteristic UUID, which can be readable, writable, notify, or indicate. The process is as follows:
After obtaining the BluetoothGattCharacteristic, the following operations can be performed:
Set Notification Service
The last parameter sets whether the notification is in indicator mode, which is a reliable notification method with acknowledgment. If you already know the UUIDs that can notify and have set them successfully upon connection, you can also set them as follows:
Read Information
Similarly, if a readable UUID has been set, you can read information as follows:
Write Data
Likewise, if a writable UUID was set during the successful connection, you can write data as follows:
The data new byte[]{0x00,0x01,0x02} is simulated data; replace it with actual data during use. Remember that the data sent must not exceed 20 bytes; if it exceeds, you can use a packet mechanism to process it.
-
Sample Image
From the above description, it can be seen that all device-related operations are uniformly handled by ViseBluetooth, which is a singleton class, making management very convenient. Before use, you must call ViseBluetooth.getInstance().init(this); in the Application for initialization. Upon successful device connection, a BluetoothGatt is automatically obtained, and when disconnected, this BluetoothGatt will be closed. The upper layer does not need to worry about the limitation of a maximum of 6 connections; simply call ViseBluetooth.getInstance().clear(); when resources need to be released. This simplicity and ease of use is the project’s purpose.
I would like to thank the two authors for providing the open-source libraries
https://github.com/litesuits/android-lite-bluetoothLE
https://github.com/alt236/Bluetooth-LE-Library—Android
These two open-source libraries have greatly aided the completion of this project.
After a long day of learning, relax with some funny jokes. Follow the most entertaining public account for a daily dose of good humor.
If you have good technical articles to share with everyone, feel free to submit to my public account. Please check the submission details by clicking on the “Submission” menu on the public account homepage.
Welcome to long press the image below -> recognize the QR code in the image or scan to follow my public account: