-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
# EasyBle | ||
EasyBle主要用于简化安卓BLE操作流程,降低BLE开发繁琐程度,使BLE开发更方便快捷。本库支持扫描(含自定义过滤条件扫描)、连接(包括设备多连接)、设备服务查询、读写数据(含分批写入)、读取设备信号、设置最大传输单元等BLE操作 | ||
|
||
|
||
|
||
## Usage | ||
本库主要通过BleManager类来进行BLE操作 | ||
### BleManager中常用基础api | ||
```java | ||
//是否支持BLE | ||
BleManager.supportBle(context); | ||
|
||
//蓝牙是否打开 | ||
BleManager.isBluetoothOn(); | ||
|
||
//打开或关闭蓝牙,不显示请求用户授权dialog(一些特殊设备如大部分国产手机除外) | ||
BleManager.toggleBluetooth(true); | ||
|
||
//显示dialog请求用户打开蓝牙,需在传入的activity的onActivityResult中处理请求结果 | ||
BleManager.enableBluetooth(activity,requestCode); | ||
``` | ||
|
||
### 步骤如下 | ||
|
||
### 1.获取BleManager对象 | ||
|
||
```java | ||
|
||
BleManager.BleOptions options = new BleManager.BleOptions(); | ||
options.loggable = true; //是否打印日志 | ||
options.connectTimeout = 10000; //连接超时时间 | ||
options.scanPeriod = 12000; //扫描周期 | ||
options.scanDeviceName = "targetDeviceName"; //扫描的目标设备名 | ||
options.scanDeviceAddress = "targetDeviceAddress"; //扫描的目标设备地址 | ||
options.scanServiceUuids = serviceUuidArray; //扫描含该服务UUID的目标设备 | ||
|
||
//获取管理器对象 | ||
BleManager bleManager = BleManager.getInstance(this.getApplication(), options); | ||
``` | ||
|
||
### 2.扫描 | ||
安卓版本不小于6.0的,扫描必须要有定位权限 | ||
```java | ||
bleManager.startScan(new BleScanCallback() { | ||
@Override | ||
public void onLeScan(BleDevice device, int rssi, byte[] scanRecord) { | ||
String name = device.name; | ||
String address = device.address; | ||
} | ||
|
||
@Override | ||
public void onStart(boolean startScanSuccess, String info) { | ||
if (startScanSuccess) { | ||
//开始扫描成功 | ||
} else { | ||
//未能成功开始扫描,可通过info查看详情 | ||
String failReason = info; | ||
} | ||
} | ||
|
||
@Override | ||
public void onFinish() { | ||
|
||
} | ||
}); | ||
|
||
``` | ||
当需要结束扫描时用以下方法结束扫描,建议在扫描到目标设备后停止扫描 | ||
```java | ||
bleManager.stopScan(); | ||
``` | ||
|
||
### 3.连接 | ||
|
||
```java | ||
|
||
BleConnectCallback bleConnectCallback = new BleConnectCallback() { | ||
@Override | ||
public void onStart(boolean startConnectSuccess, String info, BleDevice device) { | ||
if (startConnectSuccess) { | ||
//开始连接 | ||
} else { | ||
//未能成功开始连接,可通过info查看详情 | ||
String failReason = info; | ||
} | ||
} | ||
|
||
@Override | ||
public void onTimeout(BleDevice device) { | ||
|
||
} | ||
|
||
@Override | ||
public void onConnected(BleDevice device) { | ||
|
||
} | ||
|
||
@Override | ||
public void onDisconnected(BleDevice device) { | ||
|
||
} | ||
}; | ||
|
||
//通过BleDevice对象连接设备 | ||
bleManager.connect(bleDevice, bleConnectCallback); | ||
|
||
//直接通过mac地址连接 | ||
bleManager.connect(address, bleConnectCallback) | ||
``` | ||
|
||
当需要断开与设备的连接时可使用以下任一方法断开设备连接 | ||
```java | ||
|
||
//断开与指定设备的连接 | ||
bleManager.disconnect(bleDevice); | ||
|
||
//传入目标的mac地址断开与该设备的连接 | ||
bleManager.disconnect(address); | ||
|
||
//断开所有已连接设备 | ||
bleManager.disconnectAll(); | ||
``` | ||
|
||
### 4.Notify | ||
notify和indicate都使用notify()方法 | ||
```java | ||
bleManager.notify(bleDevice, serviceUuid, notifyUuid, new BleNotifyCallback() { | ||
@Override | ||
public void onCharacteristicChanged(byte[] data, BleDevice device) { | ||
|
||
} | ||
|
||
@Override | ||
public void onFail(int failCode, String info, BleDevice device) { | ||
|
||
} | ||
}); | ||
``` | ||
当需要取消notify或indicate时调用以下方法 | ||
```java | ||
bleManager.cancelNotify(bleDevice, notifyUuid); | ||
``` | ||
|
||
### 5.写入特征数据 | ||
```java | ||
bleManager.write(bleDevice, serviceUuid, writeUuid, data, new BleWriteCallback() { | ||
@Override | ||
public void onWrite(byte[] data, BleDevice device) { | ||
|
||
} | ||
|
||
@Override | ||
public void onFail(int failCode, String info, BleDevice device) { | ||
|
||
} | ||
}); | ||
``` | ||
如果一次性写入的数据大于MTU即最大传输单元,则可以使用下列方法进行分批写入 | ||
```java | ||
bleManager.writeByBatch(bleDevice, serviceUuid, writeUuid, data, lengthPerPackage, new BleWriteByBatchCallback() { | ||
@Override | ||
public void writeByBatchSuccess(byte[] data, BleDevice device) { | ||
|
||
} | ||
|
||
@Override | ||
public void onFail(int failCode, String info, BleDevice device) { | ||
|
||
} | ||
}); | ||
``` | ||
|
||
### 6.Destroy | ||
当结束BLE通信时必须调用destroy方法以释放资源 | ||
```java | ||
bleManager.destroy(); | ||
|
||
``` | ||
|
||
### 其他api | ||
```java | ||
//获取设备支持的服务信息,如果设备尚未连接上则返回值为null | ||
bleManager.getDeviceServices(bleDevice); | ||
|
||
|
||
//读取已连接的远程设备信号 | ||
bleManager.readRssi(bleDevice, bleRssiCallback); | ||
|
||
|
||
//设置MTU | ||
bleManager.setMtu(bleDevice, mtu, bleMtuCallback); | ||
|
||
|
||
//读取特征数据 | ||
bleManager.read(bleDevice, serviceUuid, readUuid, bleReadCallback); | ||
|
||
|
||
//获取当前连接的设备 | ||
bleManager.getConnectedDevices(); | ||
|
||
|
||
//判断是否已连接上某台设备 | ||
bleManager.isConnected(address); | ||
|
||
``` | ||
|
||
|
||
|
||
## License | ||
``` | ||
Copyright 2018 Ficat | ||
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters