Skip to content

Commit

Permalink
Fixed Assertion bug crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
bayosip committed Nov 23, 2021
1 parent 0917315 commit 205c5c6
Showing 1 changed file with 142 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,30 +189,40 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {


private void handleBleWriteQueue() {

Queue<Object> BleWriteQueue = writeQueueMap.get(address);
assert BleWriteQueue!=null;
if(BleWriteQueue.size() > 0) {
// Determine which type of event is next and fire it off
if (BleWriteQueue.element() instanceof BluetoothGattDescriptor) {
bleGatt.writeDescriptor((BluetoothGattDescriptor) BleWriteQueue.element());
} else if (BleWriteQueue.element() instanceof BluetoothGattCharacteristic) {
bleGatt.writeCharacteristic((BluetoothGattCharacteristic) BleWriteQueue.element());
try {
Queue<Object> BleWriteQueue = writeQueueMap.get(address);
assert BleWriteQueue != null;
if (BleWriteQueue.size() > 0) {
// Determine which type of event is next and fire it off
if (BleWriteQueue.element() instanceof BluetoothGattDescriptor) {
bleGatt.writeDescriptor((BluetoothGattDescriptor) BleWriteQueue.element());
} else if (BleWriteQueue.element() instanceof BluetoothGattCharacteristic) {
bleGatt.writeCharacteristic((BluetoothGattCharacteristic) BleWriteQueue.element());
}
}
}catch (AssertionError ae){
Log.w(TAG, "Characteristic|Descriptor is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

private void handleBleReadQueue() {

Queue<Object> BleReadQueue = readQueueMap.get(address);
assert BleReadQueue != null;
if(BleReadQueue.size() > 0) {
// Determine which type of event is next and fire it off
if (BleReadQueue.element() instanceof BluetoothGattDescriptor) {
bleGatt.readDescriptor((BluetoothGattDescriptor) BleReadQueue.element());
} else if (BleReadQueue.element() instanceof BluetoothGattCharacteristic) {
bleGatt.readCharacteristic((BluetoothGattCharacteristic) BleReadQueue.element());
try {
Queue<Object> BleReadQueue = readQueueMap.get(address);
assert BleReadQueue != null;
if (BleReadQueue.size() > 0) {
// Determine which type of event is next and fire it off
if (BleReadQueue.element() instanceof BluetoothGattDescriptor) {
bleGatt.readDescriptor((BluetoothGattDescriptor) BleReadQueue.element());
} else if (BleReadQueue.element() instanceof BluetoothGattCharacteristic) {
bleGatt.readCharacteristic((BluetoothGattCharacteristic) BleReadQueue.element());
}
}
}catch (AssertionError ae){
Log.w(TAG, "Characteristic|Descriptor is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

Expand Down Expand Up @@ -462,20 +472,31 @@ public boolean connect(@NonNull final BluetoothDevice device, @Nullable String s

if(multiBleGatt.containsKey(device.getAddress())) {
//try to reconnect to previously connected device
Log.i(TAG, "Trying to use an existing mBluetoothGatt for connection.");
MyLogData += "Trying to use an existing mBluetoothGatt for connection.\n";
BluetoothGatt gatt = multiBleGatt.get(device.getAddress());
assert gatt != null;
return gatt.connect();
try {
Log.i(TAG, "Trying to use an existing mBluetoothGatt for connection.");
MyLogData += "Trying to use an existing mBluetoothGatt for connection.\n";
BluetoothGatt gatt = multiBleGatt.get(device.getAddress());
assert gatt != null;
return gatt.connect();
} catch (AssertionError ae) {
Log.w(TAG, "Gatt is null: " + ae.toString());
Util.getHandler().post(() -> Util.message(BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

BluetoothGatt gatt = device.connectGatt(this,
false,
createGattCallBack(device.getAddress()));

if(!TextUtils.isEmpty(serviceUUID)){
assert serviceUUID != null;
selectServiceFromUUID(device.getAddress(), serviceUUID);
try {
if (!TextUtils.isEmpty(serviceUUID)) {
assert serviceUUID != null;
selectServiceFromUUID(device.getAddress(), serviceUUID);
}
}catch (AssertionError ae){
Log.w(TAG, "UUID is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
Log.i(TAG, "Trying to create a new connection.");
MyLogData +="Trying to create a new connection.\n";
Expand Down Expand Up @@ -506,14 +527,21 @@ public void maxOutGattMtuSize(String addr){
public void selectServiceFromUUID (@NonNull String deviceAddr, @NonNull String UUID){
//this.serviceUUID = UUID;
BluetoothGatt gatt = multiBleGatt.get(deviceAddr);
assert gatt != null;
BluetoothGattService service = getGattServices(gatt, UUID);

if (service==null){
Util.message(BleGattService.this, "No service with selected UUID!");
}else {
getGattServicesCharx(gatt, service);
Log.d(TAG, "selectServiceFromUUID: service uuid: " + UUID + " connected");
try {

assert gatt != null;
BluetoothGattService service = getGattServices(gatt, UUID);

if (service == null) {
Util.message(BleGattService.this, "No service with selected UUID!");
} else {
getGattServicesCharx(gatt, service);
Log.d(TAG, "selectServiceFromUUID: service uuid: " + UUID + " connected");
}
}catch (AssertionError ae){
Log.w(TAG, "Gatt is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

Expand Down Expand Up @@ -630,11 +658,15 @@ public void sendInstructionsToConnectedDevice(String deviceAddr, @Nullable UUID
gattCharacteristic.setValue(data);
writeBleCharacteristic(bleGatt, gattCharacteristic);
}
}catch (NullPointerException e)
{
}catch (NullPointerException e) {
Log.w(TAG, "Characteristic is null: " + e.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this, getString(R.string.ble_err_connection)));
}
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}catch (AssertionError ae){
Log.w(TAG, "Characteristic is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

public void sendInstructionsToConnectedDevice(String deviceAddr, @Nullable UUID charxDescriptor,
Expand Down Expand Up @@ -667,6 +699,10 @@ public void sendInstructionsToConnectedDevice(String deviceAddr, @Nullable UUID
}catch (Exception ex){
Log.w(TAG, "Service is null: " + ex.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this, getString(R.string.ble_err_connection)));
}catch (AssertionError ae){
Log.w(TAG, "Characteristic is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

Expand Down Expand Up @@ -718,19 +754,25 @@ public void readBleCharacteristic(BluetoothGatt bleGatt,

public void readDescriptor(String serviceUUID, String charxUUID,
String CCC_DESCRIPTOR_UUID, String deviceAddress) {
BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt!= null;
BluetoothGattCharacteristic charx =gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
try {
BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt != null;
BluetoothGattCharacteristic charx = gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
BluetoothGattDescriptor descriptor = charx.getDescriptor(
UUID.fromString(CCC_DESCRIPTOR_UUID));
Log.d(TAG, String.format("readDescriptor(%s)", descriptor));
Log.d(TAG, String.format("readDescriptor(%s)", descriptor));

Queue<Object> BleReadQueue = readQueueMap.get(deviceAddress);
Objects.requireNonNull(BleReadQueue).add(descriptor);
readQueueMap.put(deviceAddress, BleReadQueue);
if (BleReadQueue.size() == 1){
gatt.readDescriptor(descriptor);
Queue<Object> BleReadQueue = readQueueMap.get(deviceAddress);
Objects.requireNonNull(BleReadQueue).add(descriptor);
readQueueMap.put(deviceAddress, BleReadQueue);
if (BleReadQueue.size() == 1) {
gatt.readDescriptor(descriptor);
}
}catch (AssertionError ae){
Log.w(TAG, "Gatt is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

Expand All @@ -753,44 +795,57 @@ private boolean setCharacteristicNotification(BluetoothGatt bleGatt,
public void writeToDescriptorToEnableNotifications(String serviceUUID, String charxUUID,
String CCC_DESCRIPTOR_UUID, String deviceAddress){

BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt!= null;
BluetoothGattCharacteristic charx =gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
if (setCharacteristicNotification(gatt, charx, false)) {
try {
BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt != null;
BluetoothGattCharacteristic charx = gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
if (setCharacteristicNotification(gatt, charx, false)) {
BluetoothGattDescriptor descriptor = charx.getDescriptor(
UUID.fromString(CCC_DESCRIPTOR_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

Queue<Object> BleWriteQueue = writeQueueMap.get(deviceAddress);
Objects.requireNonNull(BleWriteQueue).add(descriptor);
writeQueueMap.put(deviceAddress, BleWriteQueue);
if(BleWriteQueue.size()==1)
Log.d(TAG, "writeToDescriptorToEnableNotifications: ->"
+gatt.writeDescriptor(descriptor));
Queue<Object> BleWriteQueue = writeQueueMap.get(deviceAddress);
Objects.requireNonNull(BleWriteQueue).add(descriptor);
writeQueueMap.put(deviceAddress, BleWriteQueue);
if (BleWriteQueue.size() == 1)
Log.d(TAG, "writeToDescriptorToEnableNotifications: ->"
+ gatt.writeDescriptor(descriptor));
}
}catch (AssertionError ae){
Log.w(TAG, "Gatt is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

public void writeToDescriptorToEnableIndicator(String serviceUUID, String charxUUID,
String CCC_DESCRIPTOR_UUID, String deviceAddress){

BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt!= null;
BluetoothGattCharacteristic charx =gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
if (setCharacteristicNotification(gatt, charx, false)) {
BluetoothGattDescriptor descriptor = charx.getDescriptor(
UUID.fromString(CCC_DESCRIPTOR_UUID));
if(descriptor!=null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
try {

Queue<Object> BleWriteQueue = writeQueueMap.get(deviceAddress);
Objects.requireNonNull(BleWriteQueue).add(descriptor);
writeQueueMap.put(deviceAddress, BleWriteQueue);
if (BleWriteQueue.size() == 1)
Log.d(TAG, "writeToDescriptorToEnableNotifications: ->"
+ gatt.writeDescriptor(descriptor));
BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt != null;
BluetoothGattCharacteristic charx = gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
if (setCharacteristicNotification(gatt, charx, false)) {
BluetoothGattDescriptor descriptor = charx.getDescriptor(
UUID.fromString(CCC_DESCRIPTOR_UUID));
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

Queue<Object> BleWriteQueue = writeQueueMap.get(deviceAddress);
Objects.requireNonNull(BleWriteQueue).add(descriptor);
writeQueueMap.put(deviceAddress, BleWriteQueue);
if (BleWriteQueue.size() == 1)
Log.d(TAG, "writeToDescriptorToEnableNotifications: ->"
+ gatt.writeDescriptor(descriptor));
}
}
}catch (AssertionError ae){
Log.w(TAG, "Gatt is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

Expand Down Expand Up @@ -832,16 +887,16 @@ private void writeToDescriptorToEnableIndicator(BluetoothGattCharacteristic char

public void writeToDescriptorToDisableNotifications(String serviceUUID, String charxUUID,
String CCC_DESCRIPTOR_UUID, String deviceAddress){

BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt!= null;
BluetoothGattCharacteristic charx =gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
if (setCharacteristicNotification(gatt, charx, false)) {
try {
BluetoothGatt gatt = multiBleGatt.get(deviceAddress);
assert gatt != null;
BluetoothGattCharacteristic charx = gatt.getService(UUID.fromString(serviceUUID))
.getCharacteristic(UUID.fromString(charxUUID));
if (setCharacteristicNotification(gatt, charx, false)) {

BluetoothGattDescriptor descriptor = charx.getDescriptor(
UUID.fromString(CCC_DESCRIPTOR_UUID));
if(descriptor!=null) {
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
Queue<Object> BleWriteQueue = writeQueueMap.get(deviceAddress);
Objects.requireNonNull(BleWriteQueue).add(descriptor);
Expand All @@ -850,6 +905,11 @@ public void writeToDescriptorToDisableNotifications(String serviceUUID, String c
Log.d(TAG, "writeToDescriptorToDisableNotifications: "
+ gatt.writeDescriptor(descriptor));
}
}
}catch (AssertionError ae){
Log.w(TAG, "Gatt is null: " + ae.toString());
Util.getHandler().post(() -> Util.message( BleGattService.this,
getString(R.string.ble_err_connection)));
}
}

Expand Down

0 comments on commit 205c5c6

Please sign in to comment.