Skip to content

Commit

Permalink
make some codes more concise and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ficat committed Oct 10, 2018
1 parent 4b131e7 commit 63dcfa5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ If you want to open bluetooth, I strongly recommend you call enableBluetooth() r
//dialog, except some special android devices
BleManager.toggleBluetooth(true);

//open bluetooth with a request dialog, you must handle the
//result in the method onActivityResult() of this activity
//open bluetooth with a request dialog, you can receive the
//result from the method onActivityResult() of this activity
BleManager.enableBluetooth(activity,requestCode);
```

Expand All @@ -41,12 +41,12 @@ If you want to open bluetooth, I strongly recommend you call enableBluetooth() r

```java

BleManager.BleOptions options = new BleManager.BleOptions();
BleManager.Options options = new BleManager.Options();
options.loggable = true; //does it print log?
options.connectTimeout = 10000; //connection time out
options.scanPeriod = 12000; //scan period
options.scanDeviceName = "deviceName";
options.scanDeviceAddress = "deviceAddress";
options.scanDeviceAddress = "deviceAddress";//like "DD:0D:30:00:0D:9B"
options.scanServiceUuids = serviceUuidArray;

//get ble manager
Expand Down Expand Up @@ -202,6 +202,22 @@ You must call destroy() to release some resources after BLE communication end
//get service infomations which the remote supports,it may return
//null if the remote device is not connected
bleManager.getDeviceServices(bleDevice);
//you can see detail service infomations by this method
Map<ServiceInfo, List<CharacteristicInfo>> serviceInfoMap = bleManager.getDeviceServices(bleDevice);
if (serviceInfoMap != null){
for (Map.Entry<ServiceInfo, List<CharacteristicInfo>> entry : serviceInfoMap.entrySet()) {
ServiceInfo serviceInfo = entry.getKey();
Log.e("TAG", "service uuid: " + serviceInfo.uuid);
for (CharacteristicInfo characterInfo : entry.getValue()) {
Log.e("TAG", "chracteristic uuid: " + characterInfo.uuid);
boolean readable = characterInfo.readable;
boolean writeable = characterInfo.writeable;
boolean notification = characterInfo.notify;
boolean indicative = characterInfo.indicative;
}
}
}


//read characteristic data
bleManager.read(bleDevice, serviceUuid, readUuid, bleReadCallback);
Expand Down
21 changes: 18 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# EasyBle
EasyBle主要用于简化安卓BLE操作流程,降低BLE开发繁琐程度,使BLE开发更方便快捷。本库支持扫描(含自定义过滤条件扫描)、连接(包括设备多连接)、设备服务查询、读写数据(含分批写入)、读取设备信号、设置最大传输单元等BLE操作
EasyBle主要用于简化安卓BLE操作流程,降低BLE开发繁琐程度。本库支持扫描(含自定义过滤条件扫描)、连接(包括设备多连接)、设备服务查询、读写数据(含分批写入)、读取设备信号、设置最大传输单元等BLE操作

## Gradle dependency
```gradle
Expand Down Expand Up @@ -38,12 +38,12 @@ dependencies {

```java

BleManager.BleOptions options = new BleManager.BleOptions();
BleManager.Options options = new BleManager.Options();
options.loggable = true; //是否打印日志
options.connectTimeout = 10000; //连接超时时间
options.scanPeriod = 12000; //扫描周期
options.scanDeviceName = "targetDeviceName"; //扫描的目标设备名
options.scanDeviceAddress = "targetDeviceAddress"; //扫描的目标设备地址
options.scanDeviceAddress = "targetDeviceAddress"; //扫描目标设备地址如"DD:0D:30:00:0D:9B"
options.scanServiceUuids = serviceUuidArray; //扫描含该服务UUID的目标设备

//获取管理器对象
Expand Down Expand Up @@ -193,6 +193,21 @@ notify和indicate都使用以下方法
```java
//获取设备支持的服务信息,如果设备尚未连接上则返回值为null
bleManager.getDeviceServices(bleDevice);
//可以通过该方法获取所有服务(含其包含的特征等)信息
Map<ServiceInfo, List<CharacteristicInfo>> serviceInfoMap = bleManager.getDeviceServices(bleDevice);
if (serviceInfoMap != null){
for (Map.Entry<ServiceInfo, List<CharacteristicInfo>> entry : serviceInfoMap.entrySet()) {
ServiceInfo serviceInfo = entry.getKey();
Log.e("TAG", "service uuid: " + serviceInfo.uuid);
for (CharacteristicInfo characterInfo : entry.getValue()) {
Log.e("TAG", "chracteristic uuid: " + characterInfo.uuid);
boolean readable = characterInfo.readable;
boolean writeable = characterInfo.writeable;
boolean notification = characterInfo.notify;
boolean indicative = characterInfo.indicative;
}
}
}


//读取已连接的远程设备信号
Expand Down
22 changes: 11 additions & 11 deletions easyble/src/main/java/com/ficat/easyble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@
public final class BleManager {
private Context mContext;
private BluetoothAdapter mBluetoothAdapter;
private BleOptions mBleOptions;
private Options mOptions;
private volatile BleScan<BleScanCallback> mScan;
private volatile BleGatt mGatt;
private static volatile BleManager instance;

private final Object mLock1 = new Object();
private final Object mLock2 = new Object();

private BleManager(Context context, BleOptions options) {
private BleManager(Context context, Options options) {
if (context == null) {
throw new IllegalArgumentException("Context is null");
}
if (context instanceof Activity) {
Logger.w("Activity Leak Risk: " + context.getClass().getSimpleName());
}
if (options == null) {
options = new BleOptions();
options = new Options();
}
this.mContext = context;
this.mBleOptions = options;
this.mOptions = options;
this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
setLoggable(options.loggable);
registerBleReceiver();
Expand All @@ -67,7 +67,7 @@ private void registerBleReceiver() {
mContext.registerReceiver(BleReceiver.getInstance(), intentFilter);
}

public static BleManager getInstance(Context context, BleOptions options) {
public static BleManager getInstance(Context context, Options options) {
if (instance == null) {
synchronized (BleManager.class) {
if (instance == null) {
Expand Down Expand Up @@ -387,10 +387,10 @@ private void checkBleScan() {
synchronized (mLock1) {
if (mScan == null) {
mScan = new BleScanner.Builder()
.setScanPeriod(mBleOptions.scanPeriod)
.setDeviceName(mBleOptions.scanDeviceName)
.setDeviceAddress(mBleOptions.scanDeviceAddress)
.setServiceUuids(mBleOptions.scanServiceUuids)
.setScanPeriod(mOptions.scanPeriod)
.setDeviceName(mOptions.scanDeviceName)
.setDeviceAddress(mOptions.scanDeviceAddress)
.setServiceUuids(mOptions.scanServiceUuids)
.build();
}
}
Expand All @@ -402,7 +402,7 @@ private void checkBleGatt() {
synchronized (mLock2) {
if (mGatt == null) {
mGatt = new BleGattImpl.Builder(mContext)
.setConnectTimeout(mBleOptions.connectTimeout)
.setConnectTimeout(mOptions.connectTimeout)
.build();
}
}
Expand All @@ -422,7 +422,7 @@ private BleDevice newBleDevice(BluetoothDevice device) {
}
}

public static final class BleOptions {
public static final class Options {
public int scanPeriod;
public String scanDeviceName;
public String scanDeviceAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public void notify(final BleDevice device, String serviceUuid, String notifyUuid
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(notifyUuid));
boolean notify = (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0;
boolean indicate = (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0;
if (!notify &&!indicate) {
if (!notify && !indicate) {
mHandler.post(new Runnable() {
@Override
public void run() {
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/com/ficat/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void initBleManager() {
//open bluetooth without a request dialog
BleManager.toggleBluetooth(true);

BleManager.BleOptions options = new BleManager.BleOptions();
BleManager.Options options = new BleManager.Options();
options.loggable = true;
options.scanPeriod = 10000;
options.connectTimeout = 10000;
Expand Down

0 comments on commit 63dcfa5

Please sign in to comment.