Thanks to the logo designed by anharismail
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
If you want to quickly preview all the functions, you can download APK as a test tool directly.
<dependency>
<groupId>com.clj.fastble</groupId>
<artifactId>FastBleLib</artifactId>
<version>2.3.2</version>
<type>pom</type>
</dependency>
compile 'com.clj.fastble:FastBleLib:2.3.2'
-
BleManager.getInstance().init(getApplication());
-
boolean isSupportBle()
-
void enableBluetooth() void disableBluetooth()
-
BleManager.getInstance() .enableLog(true) .setReConnectCount(1, 5000) .setSplitWriteNum(20) .setConnectOverTime(10000) .setOperateTimeout(5000);
-
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
-
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.。
-
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 inonConnectFail
callback automatically. - The connection is disconnected and reconnected: you can call the
connect
method again in theonDisConnected
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.
-
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.
-
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) { } });
-
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.
- If this method is called, if it is still in the scan state, it will end immediately, and callback the
-
void notify(BleDevice bleDevice, String uuid_service, String uuid_notify, BleNotifyCallback callback)
BleManager.getInstance().notify( bleDevice, uuid_service, uuid_characteristic_notify, new BleNotifyCallback() { @Override public void onNotifySuccess() { } @Override public void onNotifyFailure(BleException exception) { } @Override public void onCharacteristicChanged(byte[] data) { } });
-
boolean stopNotify(BleDevice bleDevice, String uuid_service, String uuid_notify)
BleManager.getInstance().stopNotify(uuid_service, uuid_characteristic_notify);
-
void indicate(BleDevice bleDevice, String uuid_service, String uuid_indicate, BleIndicateCallback callback)
BleManager.getInstance().indicate( bleDevice, uuid_service, uuid_characteristic_indicate, new BleIndicateCallback() { @Override public void onIndicateSuccess() { } @Override public void onIndicateFailure(BleException exception) { } @Override public void onCharacteristicChanged(byte[] data) { } });
-
boolean stopIndicate(BleDevice bleDevice, String uuid_service, String uuid_indicate)
BleManager.getInstance().stopIndicate(uuid_service, uuid_characteristic_indicate);
-
void write(BleDevice bleDevice, String uuid_service, String uuid_write, byte[] data, BleWriteCallback callback)
void write(BleDevice bleDevice, String uuid_service, String uuid_write, byte[] data, boolean split, BleWriteCallback callback)
BleManager.getInstance().write( bleDevice, uuid_service, uuid_characteristic_write, data, new BleWriteCallback() { @Override public void onWriteSuccess(int current, int total, byte[] justWrite) { } @Override public void onWriteFailure(BleException exception) { } });
Tips:
- Without expanding MTU and expanding MTU's ineffectiveness, subcontracting is required when long data with more than 20 bytes are to be sent. The parameter
boolean split
indicates whether to use packet delivery; thewrite
method without theboolean split
parameter is subcontracted to the data by more than 20 bytes by default. - On the
onWriteSuccess
callback method:current
represents the number of packets that are currently sent, andtotal
represents the total packet data this time, andjustWrite
represents the successful packet that has just been sent.
- Without expanding MTU and expanding MTU's ineffectiveness, subcontracting is required when long data with more than 20 bytes are to be sent. The parameter
-
void read(BleDevice bleDevice, String uuid_service, String uuid_read, BleReadCallback callback)
BleManager.getInstance().read( bleDevice, uuid_service, uuid_characteristic_read, new BleReadCallback() { @Override public void onReadSuccess(byte[] data) { } @Override public void onReadFailure(BleException exception) { } });
-
void readRssi(BleDevice bleDevice, BleRssiCallback callback)
BleManager.getInstance().readRssi( bleDevice, new BleRssiCallback() { @Override public void onRssiFailure(BleException exception) { } @Override public void onRssiSuccess(int rssi) { } });
Tips:
- Obtaining the signal strength of the device must be carried out after the device is connected.
- Some devices may not be able to read Rssi, do not callback onRssiSuccess (), and callback onRssiFailure () because of timeout.
-
void setMtu(BleDevice bleDevice, int mtu, BleMtuChangedCallback callback)
BleManager.getInstance().setMtu(bleDevice, mtu, new BleMtuChangedCallback() { @Override public void onSetMTUFailure(BleException exception) { } @Override public void onMtuChanged(int mtu) { } });
Tips:
- Setting up MTU requires operation after the device is connected.
- There is no such restriction in the Android Version (API-17 to API-20). Therefore, only the equipment above API21 will expand the demand for MTU.
- The parameter MTU of the method is set to 23, and the maximum setting is 512.
- Not every device supports the expansion of MTU, which requires both sides of the communication, that is to say, the need for the device hardware also supports the expansion of the MTU method. After calling this method, you can see through onMtuChanged (int MTU) how much the maximum transmission unit of the device is expanded to after the final setup. If the device does not support, no matter how many settings, the final MTU will be 23.
-
BleDevice convertBleDevice(BluetoothDevice bluetoothDevice)
通过BluetoothDevice对象构建BleDevice convertBleDevice(ScanResult scanResult)
通过ScanResult对象构建Tips:
- The completed BleDevice object is still unconnected, if necessary, advanced connection.
-
List<BleDevice> getAllConnectedDevice()
BleManager.getInstance().getAllConnectedDevice();
-
BluetoothGatt getBluetoothGatt(BleDevice bleDevice)
-
List<BluetoothGattService> getBluetoothGattServices(BleDevice bleDevice)
-
List<BluetoothGattCharacteristic> getBluetoothGattCharacteristics(BluetoothGattService service)
-
boolean isConnected(BleDevice bleDevice)
BleManager.getInstance().isConnected(bleDevice);
boolean isConnected(String mac)
BleManager.getInstance().isConnected(mac);
-
int getConnectState(BleDevice bleDevice)
BleManager.getInstance().getConnectState(bleDevice);
-
void disconnect(BleDevice bleDevice)
BleManager.getInstance().disconnect(bleDevice);
-
void disconnectAllDevice()
BleManager.getInstance().disconnectAllDevice();
-
void destroy()
BleManager.getInstance().destroy();
-
Data operation tool class
String formatHexString(byte[] data, boolean addSpace)
byte[] hexStringToBytes(String hexString)
char[] encodeHex(byte[] data, boolean toLowerCase)
-
BLE device object is the smallest unit object of scanning, connection and operation in this framework.
String getName()
Bluetooth broadcast nameString getMac()
Bluetooth MACbyte[] getScanRecord()
Broadcast dataint getRssi()
Initial signal intensity
If you have problems and ideas to communicate with me, you can contact me in the following ways.
QQ: 1033526540
Email: jasonchenlijian@gmail.com
If this framework is a great help to you and you want to support the subsequent development and maintenance of the library, you can scan the underside donation two-dimensional code to support me, and I would be grateful.
Copyright 2016 chenlijian
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.