同步操作将从 mirrors_Jasonchenlijian/FastBle 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Thanks to the logo designed by anharismail
Android Bluetooth Low Energy
If you want to quickly preview all the functions, you can download APK as a test tool directly.
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'
}
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:
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:
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:
connect
method in onConnectFail
callback automatically.connect
method again in the onDisConnected
callback method.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:
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:
onScanFinished
method.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) {
}
});
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);
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) {
}
});
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);
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:
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.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.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:
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:
boolean requestConnectionPriority(BleDevice bleDevice,int connectionPriority)
Tips:
BleDevice convertBleDevice(BluetoothDevice bluetoothDevice)
BleDevice convertBleDevice(ScanResult scanResult)
Tips:
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 name
String getMac()
Bluetooth MAC
byte[] getScanRecord()
Broadcast data
int getRssi()
Initial signal intensity
If you have problems and ideas to communicate with me, you can contact me in the following ways.
WeChat: chenlijian1216
Email: jasonchenlijian@gmail.com
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。