Skip to content

Commit

Permalink
update 2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonchenlijian committed Dec 1, 2017
1 parent 5d6ee26 commit 95cd48a
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 159 deletions.
Binary file modified FastBLE.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion FastBleLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "2.1.3"
versionName "2.1.4"
}
buildTypes {
release {
Expand Down
51 changes: 49 additions & 2 deletions FastBleLib/src/main/java/com/clj/fastble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,59 @@ public List<BleDevice> getAllConnectedDevice() {

public BluetoothGatt getBluetoothGatt(BleDevice bleDevice) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null) {
if (bleBluetooth != null)
return bleBluetooth.getBluetoothGatt();
}
return null;
}

public void removeConnectGattCallback(BleDevice bleDevice) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeConnectGattCallback();
}

public void removeRssiCallback(BleDevice bleDevice) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeRssiCallback();
}

public void removeMtuChangedCallback(BleDevice bleDevice) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeMtuChangedCallback();
}

public void removeNotifyCallback(BleDevice bleDevice, String uuid_notify) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeNotifyCallback(uuid_notify);
}

public void removeIndicateCallback(BleDevice bleDevice, String uuid_indicate) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeIndicateCallback(uuid_indicate);
}

public void removeWriteCallback(BleDevice bleDevice, String uuid_write) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeWriteCallback(uuid_write);
}

public void removeReadCallback(BleDevice bleDevice, String uuid_read) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.removeReadCallback(uuid_read);
}

public void clearCharacterCallback(BleDevice bleDevice) {
BleBluetooth bleBluetooth = getBleBluetooth(bleDevice);
if (bleBluetooth != null)
bleBluetooth.clearCharacterCallback();
}

public BleScanState getScanSate() {
return bleScanner.getScanState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public synchronized void destroy() {
}
removeConnectGattCallback();
removeRssiCallback();
removeMtuChangedCallback();
clearCharacterCallback();
if (handler != null) {
handler.removeCallbacksAndMessages(null);
Expand Down Expand Up @@ -345,7 +346,7 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
final Object call = entry.getValue();
if (call instanceof BleNotifyCallback) {
if (descriptor.getCharacteristic().getUuid().toString().equals(((BleNotifyCallback) call).getKey())) {
((BleNotifyCallback) call).getBleConnector().notifySuccess();
((BleNotifyCallback) call).getBleConnector().notifyMsgInit();
handler.post(new Runnable() {
@Override
public void run() {
Expand All @@ -366,7 +367,7 @@ public void run() {
final Object call = entry.getValue();
if (call instanceof BleIndicateCallback) {
if (descriptor.getCharacteristic().getUuid().toString().equals(((BleIndicateCallback) call).getKey())) {
((BleIndicateCallback) call).getBleConnector().indicateSuccess();
((BleIndicateCallback) call).getBleConnector().indicateMsgInit();
handler.post(new Runnable() {
@Override
public void run() {
Expand All @@ -393,7 +394,7 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
final Object call = entry.getValue();
if (call instanceof BleWriteCallback) {
if (characteristic.getUuid().toString().equals(((BleWriteCallback) call).getKey())) {
((BleWriteCallback) call).getBleConnector().writeSuccess();
((BleWriteCallback) call).getBleConnector().writeMsgInit();
handler.post(new Runnable() {
@Override
public void run() {
Expand All @@ -420,7 +421,7 @@ public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacte
final Object call = entry.getValue();
if (call instanceof BleReadCallback) {
if (characteristic.getUuid().toString().equals(((BleReadCallback) call).getKey())) {
((BleReadCallback) call).getBleConnector().readSuccess();
((BleReadCallback) call).getBleConnector().readMsgInit();
handler.post(new Runnable() {
@Override
public void run() {
Expand All @@ -439,10 +440,10 @@ public void run() {
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, final int rssi, final int status) {
super.onReadRemoteRssi(gatt, rssi, status);
BleLog.i("BluetoothGattCallback:onReadRemoteRssi ");
BleLog.i("BluetoothGattCallback:onReadRemoteRssi " + status);

if (bleRssiCallback != null) {
bleRssiCallback.getBleConnector().rssiSuccess();
bleRssiCallback.getBleConnector().rssiMsgInit();
handler.post(new Runnable() {
@Override
public void run() {
Expand All @@ -463,7 +464,7 @@ public void onMtuChanged(BluetoothGatt gatt, final int mtu, final int status) {
BleLog.i("BluetoothGattCallback:onMtuChanged ");

if (bleMtuChangedCallback != null) {
bleMtuChangedCallback.getBleConnector().mtuChangedSuccess();
bleMtuChangedCallback.getBleConnector().mtuChangedMsgInit();
handler.post(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private UUID formUUID(String uuid) {
public void enableCharacteristicNotify(BleNotifyCallback bleNotifyCallback, String uuid_notify) {
if (characteristic != null
&& (characteristic.getProperties() | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
if (setCharacteristicNotification(bluetoothGatt, characteristic, true, bleNotifyCallback)) {
handleCharacteristicNotificationCallback(bleNotifyCallback, uuid_notify);
}

handleCharacteristicNotifyCallback(bleNotifyCallback, uuid_notify);
setCharacteristicNotification(bluetoothGatt, characteristic, true, bleNotifyCallback);
} else {
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("this characteristic not support notify!"));
Expand All @@ -165,20 +165,23 @@ private boolean setCharacteristicNotification(BluetoothGatt gatt,
boolean enable,
BleNotifyCallback bleNotifyCallback) {
if (gatt == null || characteristic == null) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("gatt or characteristic equal null"));
return false;
}

boolean success1 = gatt.setCharacteristicNotification(characteristic, enable);
if (!success1) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("gatt setCharacteristicNotification fail"));
return false;
}

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(formUUID(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR));
if (descriptor == null) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("descriptor equals null"));
return false;
Expand All @@ -187,6 +190,7 @@ private boolean setCharacteristicNotification(BluetoothGatt gatt,
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
boolean success2 = gatt.writeDescriptor(descriptor);
if (!success2) {
notifyMsgInit();
if (bleNotifyCallback != null)
bleNotifyCallback.onNotifyFailure(new OtherException("gatt writeDescriptor fail"));
}
Expand All @@ -200,9 +204,8 @@ private boolean setCharacteristicNotification(BluetoothGatt gatt,
public void enableCharacteristicIndicate(BleIndicateCallback bleIndicateCallback, String uuid_indicate) {
if (characteristic != null
&& (characteristic.getProperties() | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
if (setCharacteristicIndication(bluetoothGatt, characteristic, true, bleIndicateCallback)) {
handleCharacteristicIndicationCallback(bleIndicateCallback, uuid_indicate);
}
handleCharacteristicIndicateCallback(bleIndicateCallback, uuid_indicate);
setCharacteristicIndication(bluetoothGatt, characteristic, true, bleIndicateCallback);
} else {
if (bleIndicateCallback != null)
bleIndicateCallback.onIndicateFailure(new OtherException("this characteristic not support indicate!"));
Expand Down Expand Up @@ -230,20 +233,23 @@ private boolean setCharacteristicIndication(BluetoothGatt gatt,
boolean enable,
BleIndicateCallback bleIndicateCallback) {
if (gatt == null || characteristic == null) {
indicateMsgInit();
if (bleIndicateCallback != null)
bleIndicateCallback.onIndicateFailure(new OtherException("gatt or characteristic equal null"));
return false;
}

boolean success1 = gatt.setCharacteristicNotification(characteristic, enable);
if (!success1) {
indicateMsgInit();
if (bleIndicateCallback != null)
bleIndicateCallback.onIndicateFailure(new OtherException("gatt setCharacteristicNotification fail"));
return false;
}

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(formUUID(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR));
if (descriptor == null) {
indicateMsgInit();
if (bleIndicateCallback != null)
bleIndicateCallback.onIndicateFailure(new OtherException("descriptor equals null"));
return false;
Expand All @@ -252,6 +258,7 @@ private boolean setCharacteristicIndication(BluetoothGatt gatt,
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
boolean success2 = gatt.writeDescriptor(descriptor);
if (!success2) {
indicateMsgInit();
if (bleIndicateCallback != null)
bleIndicateCallback.onIndicateFailure(new OtherException("gatt writeDescriptor fail"));
}
Expand All @@ -277,9 +284,9 @@ public void writeCharacteristic(byte[] data, BleWriteCallback bleWriteCallback,
}

if (characteristic.setValue(data)) {
if (bluetoothGatt.writeCharacteristic(characteristic)) {
handleCharacteristicWriteCallback(bleWriteCallback, uuid_write);
} else {
handleCharacteristicWriteCallback(bleWriteCallback, uuid_write);
if (!bluetoothGatt.writeCharacteristic(characteristic)) {
writeMsgInit();
if (bleWriteCallback != null)
bleWriteCallback.onWriteFailure(new OtherException("gatt writeCharacteristic fail"));
}
Expand All @@ -296,9 +303,9 @@ public void readCharacteristic(BleReadCallback bleReadCallback, String uuid_read
if (characteristic != null
&& (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {

if (bluetoothGatt.readCharacteristic(characteristic)) {
handleCharacteristicReadCallback(bleReadCallback, uuid_read);
} else {
handleCharacteristicReadCallback(bleReadCallback, uuid_read);
if (!bluetoothGatt.readCharacteristic(characteristic)) {
readMsgInit();
if (bleReadCallback != null)
bleReadCallback.onReadFailure(new OtherException("gatt readCharacteristic fail"));
}
Expand All @@ -312,9 +319,9 @@ public void readCharacteristic(BleReadCallback bleReadCallback, String uuid_read
* rssi
*/
public void readRemoteRssi(BleRssiCallback bleRssiCallback) {
if (bluetoothGatt.readRemoteRssi()) {
handleRSSIReadCallback(bleRssiCallback);
} else {
handleRSSIReadCallback(bleRssiCallback);
if (!bluetoothGatt.readRemoteRssi()) {
rssiMsgInit();
if (bleRssiCallback != null)
bleRssiCallback.onRssiFailure(new OtherException("gatt readRemoteRssi fail"));
}
Expand All @@ -325,9 +332,9 @@ public void readRemoteRssi(BleRssiCallback bleRssiCallback) {
*/
public void setMtu(int requiredMtu, BleMtuChangedCallback bleMtuChangedCallback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (bluetoothGatt.requestMtu(requiredMtu)) {
handleSetMtuCallback(bleMtuChangedCallback);
} else {
handleSetMtuCallback(bleMtuChangedCallback);
if (!bluetoothGatt.requestMtu(requiredMtu)) {
mtuChangedMsgInit();
if (bleMtuChangedCallback != null)
bleMtuChangedCallback.onsetMTUFailure(new OtherException("gatt requestMtu fail"));
}
Expand All @@ -343,10 +350,10 @@ public void setMtu(int requiredMtu, BleMtuChangedCallback bleMtuChangedCallback)
/**
* notify
*/
private void handleCharacteristicNotificationCallback(BleNotifyCallback bleNotifyCallback,
String uuid_notify) {
private void handleCharacteristicNotifyCallback(BleNotifyCallback bleNotifyCallback,
String uuid_notify) {
if (bleNotifyCallback != null) {
handler.removeMessages(MSG_NOTIFY_CHA, this);
notifyMsgInit();
bleNotifyCallback.setBleConnector(this);
bleNotifyCallback.setKey(uuid_notify);
bleBluetooth.addNotifyCallback(uuid_notify, bleNotifyCallback);
Expand All @@ -358,10 +365,10 @@ private void handleCharacteristicNotificationCallback(BleNotifyCallback bleNotif
/**
* indicate
*/
private void handleCharacteristicIndicationCallback(BleIndicateCallback bleIndicateCallback,
String uuid_indicate) {
private void handleCharacteristicIndicateCallback(BleIndicateCallback bleIndicateCallback,
String uuid_indicate) {
if (bleIndicateCallback != null) {
handler.removeMessages(MSG_INDICATE_DES, this);
indicateMsgInit();
bleIndicateCallback.setBleConnector(this);
bleIndicateCallback.setKey(uuid_indicate);
bleBluetooth.addIndicateCallback(uuid_indicate, bleIndicateCallback);
Expand All @@ -376,7 +383,7 @@ private void handleCharacteristicIndicationCallback(BleIndicateCallback bleIndic
private void handleCharacteristicWriteCallback(BleWriteCallback bleWriteCallback,
String uuid_write) {
if (bleWriteCallback != null) {
handler.removeMessages(MSG_WRITE_CHA, this);
writeMsgInit();
bleWriteCallback.setBleConnector(this);
bleWriteCallback.setKey(uuid_write);
bleBluetooth.addWriteCallback(uuid_write, bleWriteCallback);
Expand All @@ -391,7 +398,7 @@ private void handleCharacteristicWriteCallback(BleWriteCallback bleWriteCallback
private void handleCharacteristicReadCallback(BleReadCallback bleReadCallback,
String uuid_read) {
if (bleReadCallback != null) {
handler.removeMessages(MSG_READ_CHA, this);
readMsgInit();
bleReadCallback.setBleConnector(this);
bleReadCallback.setKey(uuid_read);
bleBluetooth.addReadCallback(uuid_read, bleReadCallback);
Expand All @@ -405,7 +412,7 @@ private void handleCharacteristicReadCallback(BleReadCallback bleReadCallback,
*/
private void handleRSSIReadCallback(final BleRssiCallback bleRssiCallback) {
if (bleRssiCallback != null) {
handler.removeMessages(MSG_READ_RSSI, this);
rssiMsgInit();
bleRssiCallback.setBleConnector(this);
bleBluetooth.addRssiCallback(bleRssiCallback);
handler.sendMessageDelayed(handler.obtainMessage(MSG_READ_RSSI, bleRssiCallback),
Expand All @@ -418,36 +425,36 @@ private void handleRSSIReadCallback(final BleRssiCallback bleRssiCallback) {
*/
private void handleSetMtuCallback(final BleMtuChangedCallback bleMtuChangedCallback) {
if (bleMtuChangedCallback != null) {
handler.removeMessages(MSG_SET_MTU, this);
mtuChangedMsgInit();
bleMtuChangedCallback.setBleConnector(this);
bleBluetooth.addMtuChangedCallback(bleMtuChangedCallback);
handler.sendMessageDelayed(handler.obtainMessage(MSG_SET_MTU, bleMtuChangedCallback),
BleManager.getInstance().getOperateTimeout());
}
}

public void notifySuccess() {
handler.removeMessages(MSG_NOTIFY_CHA, this);
public void notifyMsgInit() {
handler.removeMessages(MSG_NOTIFY_CHA);
}

public void indicateSuccess() {
handler.removeMessages(MSG_INDICATE_DES, this);
public void indicateMsgInit() {
handler.removeMessages(MSG_INDICATE_DES);
}

public void writeSuccess() {
handler.removeMessages(MSG_WRITE_CHA, this);
public void writeMsgInit() {
handler.removeMessages(MSG_WRITE_CHA);
}

public void readSuccess() {
handler.removeMessages(MSG_READ_CHA, this);
public void readMsgInit() {
handler.removeMessages(MSG_READ_CHA);
}

public void rssiSuccess() {
handler.removeMessages(MSG_READ_RSSI, this);
public void rssiMsgInit() {
handler.removeMessages(MSG_READ_RSSI);
}

public void mtuChangedSuccess() {
handler.removeMessages(MSG_SET_MTU, this);
public void mtuChangedMsgInit() {
handler.removeMessages(MSG_SET_MTU);
}


Expand Down
Loading

0 comments on commit 95cd48a

Please sign in to comment.