MCPcopy
hub / github.com/Jasonchenlijian/FastBle

github.com/Jasonchenlijian/FastBle @2.4.0 sqlite

repository ↗ · DeepWiki ↗ · release 2.4.0 ↗
434 symbols 1,058 edges 42 files 54 documented · 12%
README

效果图

Thanks to the logo designed by 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 Preview_2 Preview_3 Preview_4

APK

If you want to quickly preview all the functions, you can download APK as a test tool directly.

FastBLE.apk

Maven

<dependency>
   <groupId>com.clj.fastble</groupId>
   <artifactId>FastBleLib</artifactId>
   <version>2.3.4</version>
   <type>pom</type>
</dependency>

Gradle

compile 'com.clj.fastble:FastBleLib:2.3.4'

Jar

FastBLE-2.4.0.jar

Wiki

中文文档

Android BLE开发详解和FastBle源码解析

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(
            bleDevice,
            uuid_service,
            uuid_characteristic_notify,
            new BleNotifyCallback() {
                @Override
                public void onNotifySuccess() {
    
                }
    
                @Override
                public void onNotifyFailure(BleException exception) {
    
                }
    
                @Override
                public void onCharacteristicChanged(byte[] data) {
    
                }
            });
    
  • Stop Notify

    boolean stopNotify(BleDevice bleDevice, String uuid_service, String uuid_notify) boolean stopNotify(BleDevice bleDevice, String uuid_service, String uuid_notify, boolean useCharacteristicDescriptor)

    BleManager.getInstance().stopNotify(uuid_service, uuid_characteristic_notify);
    
  • Indicate

    void indicate(BleDevice bleDevice, String uuid_service, String uuid_indicate, BleIndicateCallback callback) void indicate(BleDevice bleDevice, String uuid_service, String uuid_indicate, boolean useCharacteristicDescriptor, 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) {
    
                }
            });
    
  • Stop Indicate

    boolean stopIndicate(BleDevice bleDevice, String uuid_service, String uuid_indicate) boolean stopIndicate(BleDevice bleDevice, String uuid_service, String uuid_indicate, boolean useCharacteristicDescriptor)

    BleManager.getInstance().stopIndicate(uuid_service, uuid_characteristic_indicate);
    
  • Write

    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) void write(BleDevice bleDevice, String uuid_service, String uuid_write, byte[] data, boolean split, boolean sendNextWhenLastSuccess, long intervalBetweenTwoPackage, 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; the write method without the boolean 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, and total represents the total packet data this time, and justWrite represents the successful packet that has just been sent.

  • Read

    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) {
    
                }
            });
    
  • Get Rssi

    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.

  • set Mtu

    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

Extension points exported contracts — how you extend this code

BleScanPresenterImp (Interface)
(no doc) [5 implementers]
FastBleLib/src/main/java/com/clj/fastble/callback/BleScanPresenterImp.java
Observable (Interface)
(no doc) [2 implementers]
app/src/main/java/com/clj/blesample/comm/Observable.java
Observer (Interface)
(no doc) [2 implementers]
app/src/main/java/com/clj/blesample/comm/Observer.java
OnDeviceClickListener (Interface)
(no doc) [1 implementers]
app/src/main/java/com/clj/blesample/adapter/DeviceAdapter.java

Core symbols most depended-on inside this repo

getInstance
called by 53
FastBleLib/src/main/java/com/clj/fastble/BleManager.java
toString
called by 42
FastBleLib/src/main/java/com/clj/fastble/utils/BleLruHashMap.java
getBleBluetooth
called by 18
FastBleLib/src/main/java/com/clj/fastble/BleManager.java
getKey
called by 17
FastBleLib/src/main/java/com/clj/fastble/data/BleDevice.java
runOnUiThread
called by 12
app/src/main/java/com/clj/blesample/operation/CharacteristicOperationFragment.java
addText
called by 12
app/src/main/java/com/clj/blesample/operation/CharacteristicOperationFragment.java
i
called by 11
FastBleLib/src/main/java/com/clj/fastble/utils/BleLog.java
newBleConnector
called by 10
FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java

Shape

Method 379
Class 49
Interface 4
Enum 2

Languages

Java100%

Modules by API surface

FastBleLib/src/main/java/com/clj/fastble/BleManager.java59 symbols
FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java39 symbols
app/src/main/java/com/clj/blesample/MainActivity.java32 symbols
FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleConnector.java29 symbols
app/src/main/java/com/clj/blesample/operation/OperationActivity.java19 symbols
app/src/main/java/com/clj/blesample/adapter/DeviceAdapter.java18 symbols
FastBleLib/src/main/java/com/clj/fastble/scan/BleScanPresenter.java18 symbols
app/src/main/java/com/clj/blesample/operation/CharacteristicOperationFragment.java17 symbols
FastBleLib/src/main/java/com/clj/fastble/data/BleDevice.java17 symbols
app/src/main/java/com/clj/blesample/operation/CharacteristicListFragment.java15 symbols
FastBleLib/src/main/java/com/clj/fastble/scan/BleScanRuleConfig.java15 symbols
FastBleLib/src/main/java/com/clj/fastble/bluetooth/MultipleBluetoothController.java15 symbols

For agents

$ claude mcp add FastBle \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact