![效果图](https://github.com/Jasonchenlijian/FastBle/raw/master/preview/fastble_poster.png)
Thanks to the logo designed by [anharismail](https://github.com/anharismail)
# FastBle
Android Bluetooth Low Energy
- Filtering, scanning, linking, reading, writing, notification subscription and cancellation in a simple way.
- Supports acquiring signal strength and setting the maximum transmission unit.
- Support custom scan rules
- Support multi device connections
- Support reconnection
- Support configuration timeout for conncet or operation
### Preview
![Preview_1](https://github.com/Jasonchenlijian/FastBle/raw/master/preview/new_1.png)
![Preview_2](https://github.com/Jasonchenlijian/FastBle/raw/master/preview/new_2.png)
![Preview_3](https://github.com/Jasonchenlijian/FastBle/raw/master/preview/new_3.png)
![Preview_4](https://github.com/Jasonchenlijian/FastBle/raw/master/preview/new_4.png)
### APK
If you want to quickly preview all the functions, you can download APK as a test tool directly.
[FastBLE.apk](https://github.com/Jasonchenlijian/FastBle/raw/master/FastBLE.apk)
### Gradle
- Setp1: Add it in your root build.gradle at the end of repositories
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Step2: Add the dependency
dependencies {
implementation 'com.github.Jasonchenlijian:FastBle:2.4.0'
}
### Jar
[FastBLE-2.4.0.jar](https://github.com/Jasonchenlijian/FastBle/raw/master/FastBLE-2.4.0.jar)
## Wiki
[中文文档](https://github.com/Jasonchenlijian/FastBle/wiki)
[Android BLE开发详解和FastBle源码解析](https://www.jianshu.com/p/795bb0a08beb)
## Usage
- #### Init
BleManager.getInstance().init(getApplication());
- #### Determine whether the current Android system supports BLE
boolean isSupportBle()
- #### Open or close Bluetooth
void enableBluetooth()
void disableBluetooth()
- #### Initialization configuration
BleManager.getInstance()
.enableLog(true)
.setReConnectCount(1, 5000)
.setSplitWriteNum(20)
.setConnectOverTime(10000)
.setOperateTimeout(5000);
- #### Configuration scan rules
`void initScanRule(BleScanRuleConfig scanRuleConfig)`
BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
.setServiceUuids(serviceUuids)
.setDeviceName(true, names)
.setDeviceMac(mac)
.setAutoConnect(isAutoConnect)
.setScanTimeOut(10000)
.build();
BleManager.getInstance().initScanRule(scanRuleConfig);
Tips:
- Before scanning the device, scan rules can be configured to filter out the equipment matching the program.
- What is not configured is the default parameter
- #### Scan
`void scan(BleScanCallback callback)`
BleManager.getInstance().scan(new BleScanCallback() {
@Override
public void onScanStarted(boolean success) {
}
@Override
public void onScanning(BleDevice bleDevice) {
}
@Override
public void onScanFinished(List<BleDevice> scanResultList) {
}
});
Tips:
- The scanning and filtering process is carried out in the worker thread, so it will not affect the UI operation of the main thread. Eventually, every callback result will return to the main thread.。
- #### Connect with device
`BluetoothGatt connect(BleDevice bleDevice, BleGattCallback bleGattCallback)`
BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
@Override
public void onStartConnect() {
}
@Override
public void onConnectFail(BleDevice bleDevice, BleException exception) {
}
@Override
public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
}
@Override
public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
}
});
Tips:
- On some types of phones, connectGatt must be effective on the main thread. It is very recommended that the connection process be placed in the main thread.
- After connection failure, reconnect: the framework contains reconnection mechanism after connection failure, which can configure reconnection times and intervals. Of course, you can also call the `connect` method in `onConnectFail` callback automatically.
- The connection is disconnected and reconnected: you can call the `connect` method again in the `onDisConnected` callback method.
- In order to ensure the success rate of reconnection, it is recommended to reconnect after a period of interval.
- When some models fail, they will be unable to scan devices for a short time. They can be connected directly through device objects or devices MAC without scanning.
- #### Connect with Mac
`BluetoothGatt connect(String mac, BleGattCallback bleGattCallback)`
BleManager.getInstance().connect(mac, new BleGattCallback() {
@Override
public void onStartConnect() {
}
@Override
public void onConnectFail(BleDevice bleDevice, BleException exception) {
}
@Override
public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
}
@Override
public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
}
});
Tips:
- This method can attempt to connect directly to the BLE device around the Mac without scanning.
- In many usage scenarios, I suggest that APP save the Mac of the user's customary device, then use this method to connect, which will greatly improve the connection efficiency.
- #### Scan and connect
After scanning the first equipment that meets the scanning rules, it will stop scanning and connect to the device.
`void scanAndConnect(BleScanAndConnectCallback callback)`
BleManager.getInstance().scanAndConnect(new BleScanAndConnectCallback() {
@Override
public void onScanStarted(boolean success) {
}
@Override
public void onScanFinished(BleDevice scanResult) {
}
@Override
public void onStartConnect() {
}
@Override
public void onConnectFail(BleDevice bleDevice,BleException exception) {
}
@Override
public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
}
@Override
public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
}
});
- #### Cancel scan
`void cancelScan()`
BleManager.getInstance().cancelScan();
Tips:
- If this method is called, if it is still in the scan state, it will end immediately, and callback the `onScanFinished` method.
- #### Notify
`void notify(BleDevice bleDevice,
String uuid_service,
String uuid_notify,
BleNotifyCallback callback)`
`void notify(BleDevice bleDevice,
String uuid_service,
String uuid_notify,
boolean useCharacteristicDescriptor,
BleNotifyCallback callback)`
BleManager.getInstance().notify(