Skip to content

Commit

Permalink
1.3.1 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonchenlijian committed May 19, 2018
1 parent d61c0d7 commit b5211de
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 98 deletions.
Binary file modified FastBLE.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

import com.clj.fastble.data.BleDevice;

public abstract class BleScanAndConnectCallback extends BleGattCallback {

public abstract void onScanStarted(boolean success);
public abstract class BleScanAndConnectCallback extends BleGattCallback implements BleScanPresenterImp {

public abstract void onScanFinished(BleDevice scanResult);

public void onScanning(BleDevice bleDevice){}

public void onLeScan(BleDevice bleDevice){}
public void onLeScan(BleDevice bleDevice) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

import java.util.List;

public abstract class BleScanCallback {

public abstract void onScanStarted(boolean success);

public abstract void onScanning(BleDevice result);
public abstract class BleScanCallback implements BleScanPresenterImp {

public abstract void onScanFinished(List<BleDevice> scanResultList);

public void onLeScan(BleDevice bleDevice){}
public void onLeScan(BleDevice bleDevice) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.clj.fastble.callback;

import com.clj.fastble.data.BleDevice;

public interface BleScanPresenterImp {

void onScanStarted(boolean success);

void onScanning(BleDevice bleDevice);

}
36 changes: 22 additions & 14 deletions FastBleLib/src/main/java/com/clj/fastble/scan/BleScanPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import android.os.Message;
import android.text.TextUtils;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleScanPresenterImp;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.data.BleMsg;
import com.clj.fastble.utils.BleLog;
Expand All @@ -25,18 +25,19 @@
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public abstract class BleScanPresenter implements BluetoothAdapter.LeScanCallback {

private static final String TAG = "BleScanPresenter";
private long mScanTimeout = BleManager.DEFAULT_SCAN_TIME;
private String[] mDeviceNames = null;
private String mDeviceMac = null;
private boolean mFuzzy = false;
private boolean mNeedConnect = false;
private String[] mDeviceNames;
private String mDeviceMac;
private boolean mFuzzy;
private boolean mNeedConnect;
private long mScanTimeout;
private BleScanPresenterImp mBleScanPresenterImp;

private List<BleDevice> mBleDeviceList = new ArrayList<>();

private Handler mMainHandler;
private Handler mMainHandler = new Handler(Looper.getMainLooper());
private HandlerThread mHandlerThread;
private Handler mHandler;
private boolean mHandling = true;
private boolean mHandling;

private static final class ScanHandler extends Handler {

Expand Down Expand Up @@ -71,22 +72,29 @@ public void run() {
checkDevice(bleDevice);
}

public BleScanPresenter(String[] names, String mac, boolean fuzzy, boolean needConnect, long timeOut) {
public void prepare(String[] names, String mac, boolean fuzzy, boolean needConnect,
long timeOut, BleScanPresenterImp bleScanPresenterImp) {
mDeviceNames = names;
mDeviceMac = mac;
mFuzzy = fuzzy;
mNeedConnect = needConnect;
mScanTimeout = timeOut;
mBleScanPresenterImp = bleScanPresenterImp;

mMainHandler = new Handler(Looper.getMainLooper());

mHandlerThread = new HandlerThread(TAG);
mHandlerThread = new HandlerThread(BleScanPresenter.class.getSimpleName());
mHandlerThread.start();
mHandler = new ScanHandler(mHandlerThread.getLooper(), this);

mHandling = true;
}

public boolean ismNeedConnect() {
return mNeedConnect;
}

public BleScanPresenterImp getBleScanPresenterImp() {
return mBleScanPresenterImp;
}

@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if (device == null)
Expand Down
134 changes: 68 additions & 66 deletions FastBleLib/src/main/java/com/clj/fastble/scan/BleScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleScanAndConnectCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.callback.BleScanPresenterImp;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.data.BleScanState;
import com.clj.fastble.utils.BleLog;

import java.util.List;
import java.util.UUID;
Expand All @@ -26,71 +28,47 @@ private static class BleScannerHolder {
private static final BleScanner sBleScanner = new BleScanner();
}

private BleScanPresenter bleScanPresenter;
private BleScanState scanState = BleScanState.STATE_IDLE;
private BleScanState mBleScanState = BleScanState.STATE_IDLE;

public void scan(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
long timeOut, final BleScanCallback callback) {
private BleScanPresenter mBleScanPresenter = new BleScanPresenter() {

startLeScan(serviceUuids, new BleScanPresenter(names, mac, fuzzy, false, timeOut) {
@Override
public void onScanStarted(boolean success) {
if (callback != null) {
callback.onScanStarted(success);
}
@Override
public void onScanStarted(boolean success) {
BleScanPresenterImp callback = mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onScanStarted(success);
}
}

@Override
public void onLeScan(BleDevice bleDevice) {
@Override
public void onLeScan(BleDevice bleDevice) {
if (mBleScanPresenter.ismNeedConnect()) {
BleScanAndConnectCallback callback = (BleScanAndConnectCallback)
mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onLeScan(bleDevice);
}
}

@Override
public void onScanning(BleDevice result) {
if (callback != null) {
callback.onScanning(result);
}
}

@Override
public void onScanFinished(List<BleDevice> scanResultList) {
if (callback != null) {
callback.onScanFinished(scanResultList);
}
}
});
}

public void scanAndConnect(UUID[] serviceUuids, String[] names, final String mac, boolean fuzzy,
long timeOut, final BleScanAndConnectCallback callback) {

startLeScan(serviceUuids, new BleScanPresenter(names, mac, fuzzy, true, timeOut) {

@Override
public void onScanStarted(boolean success) {
if (callback != null) {
callback.onScanStarted(success);
}
}

@Override
public void onLeScan(BleDevice bleDevice) {
} else {
BleScanCallback callback = (BleScanCallback) mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onLeScan(bleDevice);
}
}
}

@Override
public void onScanning(BleDevice result) {
if (callback != null) {
callback.onScanning(result);
}
@Override
public void onScanning(BleDevice result) {
BleScanPresenterImp callback = mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onScanning(result);
}
}

@Override
public void onScanFinished(final List<BleDevice> bleDeviceList) {
@Override
public void onScanFinished(List<BleDevice> bleDeviceList) {
if (mBleScanPresenter.ismNeedConnect()) {
final BleScanAndConnectCallback callback = (BleScanAndConnectCallback)
mBleScanPresenter.getBleScanPresenterImp();
if (bleDeviceList == null || bleDeviceList.size() < 1) {
if (callback != null) {
callback.onScanFinished(null);
Expand All @@ -99,38 +77,62 @@ public void onScanFinished(final List<BleDevice> bleDeviceList) {
if (callback != null) {
callback.onScanFinished(bleDeviceList.get(0));
}
final List<BleDevice> list = bleDeviceList;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
BleManager.getInstance().connect(bleDeviceList.get(0), callback);
BleManager.getInstance().connect(list.get(0), callback);
}
}, 100);
}
} else {
BleScanCallback callback = (BleScanCallback) mBleScanPresenter.getBleScanPresenterImp();
if (callback != null) {
callback.onScanFinished(bleDeviceList);
}
}
});
}
};

public void scan(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
long timeOut, final BleScanCallback callback) {

startLeScan(serviceUuids, names, mac, fuzzy, false, timeOut, callback);
}

private synchronized void startLeScan(UUID[] serviceUuids, BleScanPresenter presenter) {
if (presenter == null)
return;
public void scanAndConnect(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
long timeOut, BleScanAndConnectCallback callback) {

this.bleScanPresenter = presenter;
boolean success = BleManager.getInstance().getBluetoothAdapter().startLeScan(serviceUuids, bleScanPresenter);
scanState = success ? BleScanState.STATE_SCANNING : BleScanState.STATE_IDLE;
bleScanPresenter.notifyScanStarted(success);
startLeScan(serviceUuids, names, mac, fuzzy, true, timeOut, callback);
}

public synchronized void stopLeScan() {
if (bleScanPresenter == null)
private synchronized void startLeScan(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
boolean needConnect, long timeOut, BleScanPresenterImp imp) {

if (mBleScanState != BleScanState.STATE_IDLE) {
BleLog.w("scan action already exists, complete the previous scan action first");
if (imp != null) {
imp.onScanStarted(false);
}
return;
}

BleManager.getInstance().getBluetoothAdapter().stopLeScan(bleScanPresenter);
scanState = BleScanState.STATE_IDLE;
bleScanPresenter.notifyScanStopped();
mBleScanPresenter.prepare(names, mac, fuzzy, needConnect, timeOut, imp);

boolean success = BleManager.getInstance().getBluetoothAdapter()
.startLeScan(serviceUuids, mBleScanPresenter);
mBleScanState = success ? BleScanState.STATE_SCANNING : BleScanState.STATE_IDLE;
mBleScanPresenter.notifyScanStarted(success);
}

public synchronized void stopLeScan() {
BleManager.getInstance().getBluetoothAdapter().stopLeScan(mBleScanPresenter);
mBleScanState = BleScanState.STATE_IDLE;
mBleScanPresenter.notifyScanStopped();
}

public BleScanState getScanState() {
return scanState;
return mBleScanState;
}


Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ Android Bluetooth Low Energy 蓝牙快速开发框架。
<dependency>
<groupId>com.clj.fastble</groupId>
<artifactId>FastBleLib</artifactId>
<version>2.3.0</version>
<version>2.3.1</version>
<type>pom</type>
</dependency>

### Gradle

compile 'com.clj.fastble:FastBleLib:2.3.0'
compile 'com.clj.fastble:FastBleLib:2.3.1'


## 其他说明
Expand Down Expand Up @@ -76,10 +76,11 @@ FastBle 所有代码均可以加入混淆。
- #### (方法说明)初始化配置

BleManager.getInstance()
.enableLog(true) // 设置是否打印日志,默认开启
.setReConnectCount(1, 5000) // 设置连接时重连次数和重连间隔(毫秒),默认为0次不重连
.setSplitWriteNum(20) // 设置分包发送的时候,每一包的数据长度,默认20
.setOperateTimeout(5000); // 设置操作readRssi、setMtu、write、read、notify、indicate的超时时间(毫秒)
.enableLog(true) // 设置是否打印日志,默认开启
.setReConnectCount(1, 5000) // 设置连接时重连次数和重连间隔(毫秒),默认为0次不重连
.setSplitWriteNum(20) // 设置分包发送的时候,每一包的数据长度,默认20
.setConnectOverTime(10000) // 设置连接超时时间
.setOperateTimeout(5000); // 设置readRssi、setMtu、write、read、notify、indicate的超时(毫秒)

- #### (方法说明)配置扫描规则

Expand Down Expand Up @@ -518,6 +519,8 @@ FastBle 所有代码均可以加入混淆。


## 版本更新日志
- v2.3.1(2018-05-10)
- 增加连接超时的设置
- v2.3.0(2018-04-29)
- 增加通过mac直连的方法
- 增加连接失败后重连api
Expand Down

0 comments on commit b5211de

Please sign in to comment.