Skip to content

Commit

Permalink
new version: 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonchenlijian committed Apr 29, 2018
1 parent 954595d commit 78a688b
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 237 deletions.
Binary file modified FastBLE.apk
Binary file not shown.
8 changes: 4 additions & 4 deletions FastBleLib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26
buildToolsVersion '26.0.2'

defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 225
versionName "2.2.5"
versionCode 230
versionName "2.3.0"
}
buildTypes {
release {
Expand Down
1 change: 0 additions & 1 deletion FastBleLib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Expand Down
4 changes: 3 additions & 1 deletion FastBleLib/src/main/java/com/clj/fastble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public BleManager setReConnectCount(int count) {
public BleManager setReConnectCount(int count, long interval) {
if (count > 10)
count = 10;
if (interval < 0)
interval = 0;
this.reConnectCount = count;
this.reConnectInterval = interval;
return this;
Expand Down Expand Up @@ -338,7 +340,7 @@ public BluetoothGatt connect(BleDevice bleDevice, BleGattCallback bleGattCallbac
if (bleDevice == null || bleDevice.getDevice() == null) {
bleGattCallback.onConnectFail(bleDevice, new OtherException("Not Found Device Exception Occurred!"));
} else {
BleBluetooth bleBluetooth = new BleBluetooth(bleDevice);
BleBluetooth bleBluetooth = multipleBluetoothController.buildConnectingBle(bleDevice);
boolean autoConnect = bleScanRuleConfig.isAutoConnect();
return bleBluetooth.connect(bleDevice, autoConnect, bleGattCallback);
}
Expand Down
201 changes: 78 additions & 123 deletions FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.data.BleMsg;
import com.clj.fastble.exception.ConnectException;
import com.clj.fastble.exception.OtherException;
import com.clj.fastble.utils.BleLog;

import java.lang.reflect.Method;
Expand All @@ -49,7 +50,6 @@ public class BleBluetooth {
private boolean isActiveDisconnect = false;
private BleDevice bleDevice;
private BluetoothGatt bluetoothGatt;
private boolean isReturnMainThread = false;
private MainHandler mainHandler = new MainHandler(Looper.getMainLooper());
private int connectRetryCount = 0;

Expand Down Expand Up @@ -162,7 +162,6 @@ public synchronized BluetoothGatt connect(BleDevice bleDevice,
+ "\ncurrentThread: " + Thread.currentThread().getId());

addConnectGattCallback(callback);
isReturnMainThread = Looper.myLooper() != null && Looper.myLooper() == Looper.getMainLooper();

lastState = LastState.CONNECT_CONNECTING;

Expand All @@ -178,7 +177,14 @@ public synchronized BluetoothGatt connect(BleDevice bleDevice,
if (bleGattCallback != null)
bleGattCallback.onStartConnect();
} else {
disconnectGatt();
refreshDeviceCache();
closeBluetoothGatt();
lastState = LastState.CONNECT_FAILURE;
BleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BleBluetooth.this);
if (bleGattCallback != null)
bleGattCallback.onConnectFail(bleDevice, new OtherException("GATT Connect Exception Occurred!"));

}
return gatt;
}
Expand All @@ -197,7 +203,7 @@ public synchronized void destroy() {
removeRssiCallback();
removeMtuChangedCallback();
clearCharacterCallback();
mainHandler.removeCallbacksAndMessages(this);
mainHandler.removeCallbacksAndMessages(null);
}

private synchronized void disconnectGatt() {
Expand Down Expand Up @@ -225,7 +231,7 @@ private synchronized void closeBluetoothGatt() {
}
}

private static final class MainHandler extends Handler {
private final class MainHandler extends Handler {

MainHandler(Looper looper) {
super(looper);
Expand All @@ -235,47 +241,71 @@ private static final class MainHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case BleMsg.MSG_CONNECT_FAIL: {
BleConnectStateParameter para = (BleConnectStateParameter) msg.obj;
BleGattCallback callback = para.getCallback();
BluetoothGatt gatt = para.getGatt();
BleDevice bleDevice = para.getBleDevice();
int status = para.getStatus();
if (callback != null)
callback.onConnectFail(bleDevice, new ConnectException(gatt, status));
disconnectGatt();
refreshDeviceCache();
closeBluetoothGatt();

if (connectRetryCount < BleManager.getInstance().getReConnectCount()) {
BleLog.e("Connect fail, try reconnect after " + BleManager.getInstance().getReConnectInterval() + " Millisecond later");

connectRetryCount++;

Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_RECONNECT;
mainHandler.sendMessageDelayed(message, BleManager.getInstance().getReConnectInterval());
} else {
connectRetryCount = 0;

lastState = LastState.CONNECT_FAILURE;
BleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BleBluetooth.this);

BleConnectStateParameter para = (BleConnectStateParameter) msg.obj;
int status = para.getStatus();
if (bleGattCallback != null)
bleGattCallback.onConnectFail(bleDevice, new ConnectException(bluetoothGatt, status));
}
}
break;

case BleMsg.MSG_DISCONNECTED: {
disconnectGatt();
refreshDeviceCache();
closeBluetoothGatt();

BleConnectStateParameter para = (BleConnectStateParameter) msg.obj;
BleGattCallback callback = para.getCallback();
BluetoothGatt gatt = para.getGatt();
boolean isActive = para.isAcitive();
BleDevice bleDevice = para.getBleDevice();
boolean isActive = para.isActive();
int status = para.getStatus();
if (callback != null)
callback.onDisConnected(isActive, bleDevice, gatt, status);
if (bleGattCallback != null)
bleGattCallback.onDisConnected(isActive, bleDevice, bluetoothGatt, status);
}
break;

case BleMsg.MSG_CONNECT_SUCCESS: {
BleConnectStateParameter para = (BleConnectStateParameter) msg.obj;
BleGattCallback callback = para.getCallback();
BluetoothGatt gatt = para.getGatt();
BleDevice bleDevice = para.getBleDevice();
int status = para.getStatus();
if (callback != null)
callback.onConnectSuccess(bleDevice, gatt, status);
if (bleGattCallback != null)
bleGattCallback.onConnectSuccess(bleDevice, bluetoothGatt, status);
}
break;

case BleMsg.MSG_RECONNECT: {
connect(bleDevice, false, bleGattCallback);
}
break;

case BleMsg.MSG_DISCOVER_SERVICES: {
if (bluetoothGatt != null) {
bluetoothGatt.discoverServices();
}
}

default:
super.handleMessage(msg);
break;
}
}
}


private BluetoothGattCallback coreGattCallback = new BluetoothGattCallback() {

@Override
Expand All @@ -289,90 +319,31 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
bluetoothGatt = gatt;

if (newState == BluetoothProfile.STATE_CONNECTED) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.discoverServices();

Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCOVER_SERVICES;
mainHandler.sendMessageDelayed(message, 500);

} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

if (lastState == LastState.CONNECT_CONNECTING) {

if (connectRetryCount < BleManager.getInstance().getReConnectCount()) {
connectRetryCount++;

BleLog.e("Connect fail, try reconnect");

try {
Thread.sleep(BleManager.getInstance().getReConnectInterval());
} catch (InterruptedException e) {
e.printStackTrace();
}

BluetoothGatt retryGatt;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
retryGatt = bleDevice.getDevice().connectGatt(BleManager.getInstance().getContext(),
false, coreGattCallback, TRANSPORT_LE);
} else {
retryGatt = bleDevice.getDevice().connectGatt(BleManager.getInstance().getContext(),
false, coreGattCallback);
}
if (retryGatt == null) {
connectRetryCount = 0;
disconnectGatt();
refreshDeviceCache();
closeBluetoothGatt();
lastState = LastState.CONNECT_FAILURE;
if (isReturnMainThread) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_FAIL;
BleConnectStateParameter para = new BleConnectStateParameter(bleGattCallback, gatt, status);
para.setBleDevice(getDevice());
message.obj = para;
mainHandler.sendMessage(message);
} else {
if (bleGattCallback != null)
bleGattCallback.onConnectFail(getDevice(), new ConnectException(gatt, status));
}
}

} else {
connectRetryCount = 0;
disconnectGatt();
refreshDeviceCache();
closeBluetoothGatt();
lastState = LastState.CONNECT_FAILURE;
if (isReturnMainThread) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_FAIL;
BleConnectStateParameter para = new BleConnectStateParameter(bleGattCallback, gatt, status);
para.setBleDevice(getDevice());
message.obj = para;
mainHandler.sendMessage(message);
} else {
if (bleGattCallback != null)
bleGattCallback.onConnectFail(getDevice(), new ConnectException(gatt, status));
}
}
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_FAIL;
message.obj = new BleConnectStateParameter(status);
mainHandler.sendMessage(message);

} else if (lastState == LastState.CONNECT_CONNECTED) {
BleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BleBluetooth.this);
BleManager.getInstance().getMultipleBluetoothController().removeBleBluetooth(BleBluetooth.this);
lastState = LastState.CONNECT_DISCONNECT;

if (isReturnMainThread) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCONNECTED;
BleConnectStateParameter para = new BleConnectStateParameter(bleGattCallback, gatt, status);
para.setAcitive(isActiveDisconnect);
para.setBleDevice(getDevice());
message.obj = para;
mainHandler.sendMessage(message);
} else {
if (bleGattCallback != null)
bleGattCallback.onDisConnected(isActiveDisconnect, getDevice(), gatt, status);
}
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_DISCONNECTED;
BleConnectStateParameter para = new BleConnectStateParameter(status);
para.setActive(isActiveDisconnect);
message.obj = para;
mainHandler.sendMessage(message);
}
}
}
Expand All @@ -389,34 +360,19 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
lastState = LastState.CONNECT_CONNECTED;
isActiveDisconnect = false;
BleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BleBluetooth.this);
BleManager.getInstance().getMultipleBluetoothController().addBleBluetooth(BleBluetooth.this);

if (isReturnMainThread) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_SUCCESS;
BleConnectStateParameter para = new BleConnectStateParameter(bleGattCallback, gatt, status);
para.setBleDevice(getDevice());
message.obj = para;
mainHandler.sendMessage(message);
} else {
if (bleGattCallback != null)
bleGattCallback.onConnectSuccess(getDevice(), gatt, status);
}
} else {
closeBluetoothGatt();
lastState = LastState.CONNECT_FAILURE;
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_SUCCESS;
message.obj = new BleConnectStateParameter(status);
mainHandler.sendMessage(message);

if (isReturnMainThread) {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_FAIL;
BleConnectStateParameter para = new BleConnectStateParameter(bleGattCallback, gatt, status);
para.setBleDevice(getDevice());
message.obj = para;
mainHandler.sendMessage(message);
} else {
if (bleGattCallback != null)
bleGattCallback.onConnectFail(getDevice(), new ConnectException(gatt, status));
}
} else {
Message message = mainHandler.obtainMessage();
message.what = BleMsg.MSG_CONNECT_FAIL;
message.obj = new BleConnectStateParameter(status);
mainHandler.sendMessage(message);
}
}

Expand Down Expand Up @@ -607,7 +563,6 @@ public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
}
};


enum LastState {
CONNECT_IDLE,
CONNECT_CONNECTING,
Expand Down
Loading

0 comments on commit 78a688b

Please sign in to comment.