SoFunction
Updated on 2025-03-11

Basic introduction to Android Bluetooth library FastBle

Preface

Recently, I am working on IoT courses and need to use Android's Bluetooth API during the process, but it is a bit troublesome to use the native Bluetooth API. So I searched the Internet to see if there is any useful Android Bluetooth library, and then found this baby and share it with you.

FastBle VS Native Android Bluetooth API

It is a bit troublesome to use the Bluetooth API of native Android. You have to first obtain the Bluetooth adapter of the device, and then register the broadcast to accept the Bluetooth device information. After using it, you still need to log out the broadcast, which is relatively troublesome.

It is not easy to encapsulate, and it can be said that the most painful thing about native Android is because the code of native Android is not very independent and is mixed with Activity, broadcasting, etc. There are very few Bluetooth libraries on the market. I first looked at BleLib, but I felt that it was better to change the soup and not the medicine, and it was not simple at all to use.

But FastLib encapsulation is very skillful, which can basically control the granularity of an operation within one line. In addition, the code does not need to deal with threads, notifications, etc. The library has helped us complete these complex things.

The Github project address of FastBle is here, you can take a look at: [FastBle - GitHub](/Jasonchenlijian/FastBle  (Local download

Its documentation is also relatively complete, you can check the official documentation to use it:FastBle - Document

FastBle usage

0x00 Declare permission

As long as Bluetooth is used, it is essential to declare permissions. FastBle requires permissions as follows:

<uses-permission android:name="" />
<uses-permission android:name=".BLUETOOTH_ADMIN" />
<uses-permission android:name=".ACCESS_COARSE_LOCATION" />
<uses-permission android:name=".ACCESS_FINE_LOCATION" />

One thing to note here, If the Android version is higher than 6.0, the user also needs to turn on the location information (not only need location permissions, but also location information) before scanning through Bluetooth.

0x01 Initialization and global configuration

Initialization needs to be executed before any function in the library is called. Since the library uses singleton mode, it only needs to be initialized once and can be used anywhere. It is recommended to execute the initialization code in onCreate:

().init(getApplication());

Global configuration can be executed immediately after initialization. Of course, if no configuration is performed, there is no relationship. Each option has a default value:

()
 .enableLog(true)
 .setReConnectCount(1, 5000)
 .setSplitWriteNum(20)
 .setConnectOverTime(10000)
 .setOperateTimeout(5000);

You can find detailed information on each item in the official documentation

0x02 Turn on Bluetooth

There are many ways to turn on Bluetooth using the BleManager class in FastBle. Here we recommend the following method, which will cause the thread to be blocked. If the user does not choose whether to turn on Bluetooth, the thread will pause execution:

().enableBluetooth();

0x03 Scan the device

After turning on Bluetooth, you can scan the device. Before the official scan, you can customize the scanning rules, like this:

BleScanRuleConfig scanRuleConfig = new ()
 .setServiceUuids(serviceUuids) // Only scan the specified service devices, optional .setDeviceName(true, names)  // Only scan devices with specified broadcast name, optional .setDeviceMac(mac)   // Only scan the specified mac devices, optional .setAutoConnect(isAutoConnect) // autoConnect parameter when connecting, optional, default false .setScanTimeOut(10000)  // Scan timeout time, optional, default 10 seconds; less than or equal to 0 means no limit on scanning time .build();

().initScanRule(scanRuleConfig);

After setting the rules, you can start scanning, like this

().scan(new BleScanCallBack() {
 @Override
 public void onScanStarted(boolean success) {
 // Callback to start scanning }

 @Override
 public void onScanning(BleDevice bleDevice) {
 // Scan to a callback for a device that has not been scanned before }

 @Override
 public void onScanFinished(List&lt;BleDevice&gt; scanResultList) {
 // If the scanned callback will not be repeated in the list. }
});

These callbacks are safe and will automatically return to the main thread, so you can use it with confidence.

Of course, anywhere, at any time, you can use the Cancel Scan function directly to stop scanning:

().cancelScan();

0x04 Connect to the device

After scanning, you have obtained one or more BleDevice objects, which you can use directly to initiate connections to the target device, like this:

().connect(bleDevice, new BleGattCallback() {
 @Override
 public void onStartConnect() {
 // Start the connection }

 @Override
 public void onConnectFail(BleDevice bleDevice, BleException exception) {
  // Connection failed }

 @Override
 public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
  // If the connection is successful, BleDevice is the connected BLE device }

 @Override
 public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
  // Connection interruption, isActiveDisConnected indicates whether the disconnect method is actively called. }
});

Of course, there are many detailed instructions in the official documentation. Here I just briefly introduce the basic use of FastBle. Please refer to the official documentation for details.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.