From 63dcfa585c2087dcb98deb523b2fc910fde38f24 Mon Sep 17 00:00:00 2001 From: pw <329803553@qq.com> Date: Wed, 10 Oct 2018 22:26:50 +0800 Subject: [PATCH] make some codes more concise and update readme --- README.md | 24 +++++++++++++++---- README_CN.md | 21 +++++++++++++--- .../java/com/ficat/easyble/BleManager.java | 22 ++++++++--------- .../com/ficat/easyble/gatt/BleGattImpl.java | 2 +- .../java/com/ficat/sample/MainActivity.java | 2 +- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c861f64..cc2e2a9 100644 --- a/README.md +++ b/README.md @@ -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); ``` @@ -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 @@ -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> serviceInfoMap = bleManager.getDeviceServices(bleDevice); + if (serviceInfoMap != null){ + for (Map.Entry> 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); diff --git a/README_CN.md b/README_CN.md index 65116cf..d4c1020 100644 --- a/README_CN.md +++ b/README_CN.md @@ -1,5 +1,5 @@ # EasyBle - EasyBle主要用于简化安卓BLE操作流程,降低BLE开发繁琐程度,使BLE开发更方便快捷。本库支持扫描(含自定义过滤条件扫描)、连接(包括设备多连接)、设备服务查询、读写数据(含分批写入)、读取设备信号、设置最大传输单元等BLE操作 + EasyBle主要用于简化安卓BLE操作流程,降低BLE开发繁琐程度。本库支持扫描(含自定义过滤条件扫描)、连接(包括设备多连接)、设备服务查询、读写数据(含分批写入)、读取设备信号、设置最大传输单元等BLE操作 ## Gradle dependency ```gradle @@ -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的目标设备 //获取管理器对象 @@ -193,6 +193,21 @@ notify和indicate都使用以下方法 ```java //获取设备支持的服务信息,如果设备尚未连接上则返回值为null bleManager.getDeviceServices(bleDevice); + //可以通过该方法获取所有服务(含其包含的特征等)信息 + Map> serviceInfoMap = bleManager.getDeviceServices(bleDevice); + if (serviceInfoMap != null){ + for (Map.Entry> 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; + } + } + } //读取已连接的远程设备信号 diff --git a/easyble/src/main/java/com/ficat/easyble/BleManager.java b/easyble/src/main/java/com/ficat/easyble/BleManager.java index 17d2521..c807e66 100644 --- a/easyble/src/main/java/com/ficat/easyble/BleManager.java +++ b/easyble/src/main/java/com/ficat/easyble/BleManager.java @@ -32,7 +32,7 @@ public final class BleManager { private Context mContext; private BluetoothAdapter mBluetoothAdapter; - private BleOptions mBleOptions; + private Options mOptions; private volatile BleScan mScan; private volatile BleGatt mGatt; private static volatile BleManager instance; @@ -40,7 +40,7 @@ public final class BleManager { 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"); } @@ -48,10 +48,10 @@ private BleManager(Context context, BleOptions options) { 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(); @@ -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) { @@ -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(); } } @@ -402,7 +402,7 @@ private void checkBleGatt() { synchronized (mLock2) { if (mGatt == null) { mGatt = new BleGattImpl.Builder(mContext) - .setConnectTimeout(mBleOptions.connectTimeout) + .setConnectTimeout(mOptions.connectTimeout) .build(); } } @@ -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; diff --git a/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java b/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java index da1a2f7..e378c9a 100644 --- a/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java +++ b/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java @@ -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() { diff --git a/sample/src/main/java/com/ficat/sample/MainActivity.java b/sample/src/main/java/com/ficat/sample/MainActivity.java index 1ecf13e..c924181 100644 --- a/sample/src/main/java/com/ficat/sample/MainActivity.java +++ b/sample/src/main/java/com/ficat/sample/MainActivity.java @@ -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;