In-Depth Analysis of Android BLE Bluetooth

Today’s Technology News

Recently, the Copyright Management Department of the National Copyright Administration held talks with more than twenty domestic and foreign music companies including Universal Music, Warner Music, and Sony Music regarding issues related to online music copyrights. The head of the Copyright Management Department emphasized that they hope all music companies will comply with copyright laws and international treaties, resist various music infringement behaviors, promote comprehensive authorization and wide dissemination of online music, and avoid granting exclusive copyrights to online music service providers; they must adhere to the principles of fairness and reasonableness, and not permit, condone, or provoke online music service providers to inflate authorization prices or engage in vicious competition.

Author Profile

It’s Friday again, the weekend is just around the corner! Wishing everyone a pleasant weekend in advance!

This article comes from Ai Shen Yi Bu Xiao Xin‘s submission, introducing the Bluetooth library he has encapsulated, hoping it will be helpful to everyone!

Ai Shen Yi Bu Xiao Xin‘s blog address:

http://www.jianshu.com/u/f2a928cacb8d

Main Content

Due to the rapid development of the company in the first half of the year, there has been limited free time, and it has been several months since I updated any articles. However, during this time, I have optimized some interfaces in the Bluetooth library, including adding OTA upgrade interfaces, connection timeout reminders, custom annotations to replace previous enumerations (such as device connection status), and fixing the bug of incorrect number of connected devices in the Bluetooth management class, etc. Continuing from the last article on BLE Bluetooth, let’s continue, and for those who haven’t read it, you can refer to it first.

In-Depth Analysis of Android BLE Bluetooth (Part II)

http://www.jianshu.com/p/0c6aedd6640b

In-Depth Analysis of Android BLE Bluetooth

The following mainly analyzes the Bluetooth operations in my Bluetooth library

1.The BluetoothLeService class is the core functionality implementation of the entire Bluetooth operation, while BleManager is the management class that provides all Bluetooth operation interfaces externally. Once BluetoothLeService processes the results, it returns them to BleManager, which then provides interfaces externally through BleManager, connected via a handler, as shown below:

In-Depth Analysis of Android BLE Bluetooth

Once the BluetoothLeService processes the information, it will notify BleManager to handle state changes, as shown below:

In-Depth Analysis of Android BLE Bluetooth

2.The BleListener is a collection of all interfaces, defined here as an abstract class because some callback methods do not need to be implemented (if anyone has better suggestions, please leave a comment, I would greatly appreciate it).

In-Depth Analysis of Android BLE Bluetooth

3.It is important to pay attention to some details, such as most devices will repeatedly scan the same Bluetooth device, so filtering is necessary. When developing applications, product filtering must also be done, such as filtering through the device’s broadcast package or device name, as shown below (note: filtering should be based on the broadcast package provided by your product; the following is our product):

In-Depth Analysis of Android BLE Bluetooth

Another important detail is how to determine whether the current Bluetooth object is the same object when a device is added, removed, or its connection status changes (it sounds a bit convoluted), as shown in the image below:

In-Depth Analysis of Android BLE Bluetooth

As you can see, mBleFactory.create(BleManager.this,(BluetoothDevice) msg.obj) returns a Bluetooth object, and the most basic implementation of this method is as follows:

In-Depth Analysis of Android BLE Bluetooth

Here, it checks whether the currently connected Bluetooth devices collection contains that device. If it does, it returns that object directly; if not, it creates a new Bluetooth object.

4.When a device is successfully connected, it does not mean that data can be sent at that moment, as the connection is successful but the Bluetooth services have not been obtained yet. You must first obtain the services through the gatt object by calling discoverServices(). If you want the app to be able to listen in real-time to data sent from the Bluetooth device, you also need to set a notification (which can be understood as registering a listener) as follows:

In-Depth Analysis of Android BLE Bluetooth

5.This library provides a simple Bluetooth custom object, and the Bluetooth devices used are based on this custom object. If you want to extend more Bluetooth attributes, you can inherit and extend it, so to better handle the types of Bluetooth objects, the library defines generic types.

The newly updated OTA upgrade module interface has not been introduced yet; if anyone is interested, you can download the source code to check it out. OK, the important details have been introduced, and if you are interested, feel free to apply this library to your own projects. The next article is preparing to update an article on Bluetooth 3.0 (based on the A2DP protocol for Bluetooth speakers), so stay tuned. Here is the demo download address:

https://github.com/Alex-Jerry/BleDemo

More

After a tiring day of learning, let’s relax with some funny jokes.Follow the most entertaining public account for a good mood every day.

In-Depth Analysis of Android BLE Bluetooth

If you have good technical articles you want to share with everyone, feel free to submit them to my public account. For submission details, please click the “Submit” menu on the public account homepage.

Welcome to long press the image below -> recognize the QR code or scan to follow my public account:

In-Depth Analysis of Android BLE Bluetooth

Leave a Comment