@@ -368,21 +368,31 @@ public void run() {
368368 * @param gattCharacteristic - the characteristic to be read.
369369 */
370370 private void executeReadCharacteristic (BluetoothGattCharacteristic gattCharacteristic ) {
371- if (gattCharacteristic != null ) {
372- boolean retVal = mBluetoothGatt .readCharacteristic (gattCharacteristic );
373- if (retVal ) {
374- Log .d (TAG , "executeReadCharacteristic - read initiated successfully" );
375- } else {
376- Log .d (TAG , "executeReadCharacteristic - read initiation failed - waiting, then re-trying" );
377- mHandler .postDelayed (new Runnable () {
378- public void run () {
379- Log .w (TAG , "Executing delayed read of characteristic" );
380- executeReadCharacteristic (gattCharacteristic );
381- }
382- }, 100 );
383- }
371+ if (gattCharacteristic == null ) {
372+ Log .i (TAG , "ExecuteReadCharacteristic() - gatCharacteristic is null, so not doing anything" );
373+ mUtil .showToast ("ERROR: gatCharacteristic is null - this should not happen" );
374+ mSdDataReceiver .onSdDataFault (mSdData );
375+ return ;
376+ }
377+ if (mBluetoothGatt == null ) {
378+ Log .e (TAG , "executeReadCharacteristic() - mBluetoothGatt is null - Characteristic=" + gattCharacteristic .getUuid ().toString ());
379+ mUtil .showToast ("ERROR: mGatCharacteristic is null - this should not happen" );
380+ mSdDataReceiver .onSdDataFault (mSdData );
381+ return ;
382+ }
383+
384+ // To get here both gatCharacteristic and mBluetoothGatt must be non-null
385+ boolean retVal = mBluetoothGatt .readCharacteristic (gattCharacteristic );
386+ if (retVal ) {
387+ Log .d (TAG , "executeReadCharacteristic - read initiated successfully" );
384388 } else {
385- Log .i (TAG ,"ExecuteReadCharacteristic() - gatCharacteristic is null, so not doing anything" );
389+ Log .d (TAG , "executeReadCharacteristic - read initiation failed - waiting, then re-trying" );
390+ mHandler .postDelayed (new Runnable () {
391+ public void run () {
392+ Log .w (TAG , "Executing delayed read of characteristic" );
393+ executeReadCharacteristic (gattCharacteristic );
394+ }
395+ }, 100 );
386396 }
387397 }
388398
@@ -391,27 +401,38 @@ public void run() {
391401 * of a given characteristic.
392402 * Because only one BLE operation can be taking place at a time, it may fail, in which case
393403 * the read is re-tried after a 100ms delay.
404+ *
394405 * @param gattCharacteristic - the characteristic to be read.
395- * @param valBytes[] - array of bytes to send
396- * @param nBytes - number of bytes to send.
406+ * @param valBytes[] - array of bytes to send
407+ * @param nBytes - number of bytes to send.
397408 */
398409 private void executeWriteCharacteristic (BluetoothGattCharacteristic gattCharacteristic , byte [] valBytes ) {
399- if (gattCharacteristic != null ) {
400- gattCharacteristic .setValue (valBytes );
401- boolean retVal = mBluetoothGatt .writeCharacteristic (gattCharacteristic );
402- if (retVal ) {
403- Log .d (TAG , "executeWriteCharacteristic - write initiated successfully" );
404- } else {
405- Log .d (TAG , "executeWriteCharacteristic - write initiation failed - waiting, then re-trying" );
406- mHandler .postDelayed (new Runnable () {
407- public void run () {
408- Log .w (TAG , "Executing delayed write of characteristic" );
409- executeWriteCharacteristic (gattCharacteristic , valBytes );
410- }
411- }, 100 );
412- }
410+ if (gattCharacteristic == null ) {
411+ Log .i (TAG , "ExecuteWriteCharacteristic() - gatCharacteristic is null, so not doing anything" );
412+ mUtil .showToast ("ERROR: gatCharacteristic is null - this should not happen" );
413+ mSdDataReceiver .onSdDataFault (mSdData );
414+ return ;
415+ }
416+ if (mBluetoothGatt == null ) {
417+ Log .e (TAG , "executeWriteCharacteristic() - mBluetoothGatt is null - Characteristic=" + gattCharacteristic .getUuid ().toString ());
418+ mUtil .showToast ("ERROR: mGatCharacteristic is null - this should not happen" );
419+ mSdDataReceiver .onSdDataFault (mSdData );
420+ return ;
421+ }
422+
423+ // To get here both gatCharacteristic and mBluetoothGatt must be non-null
424+ gattCharacteristic .setValue (valBytes );
425+ boolean retVal = mBluetoothGatt .writeCharacteristic (gattCharacteristic );
426+ if (retVal ) {
427+ Log .d (TAG , "executeWriteCharacteristic - write initiated successfully" );
413428 } else {
414- Log .i (TAG ,"ExecuteWriteCharacteristic() - gatCharacteristic is null, so not doing anything" );
429+ Log .d (TAG , "executeWriteCharacteristic - write initiation failed - waiting, then re-trying" );
430+ mHandler .postDelayed (new Runnable () {
431+ public void run () {
432+ Log .w (TAG , "Executing delayed write of characteristic" );
433+ executeWriteCharacteristic (gattCharacteristic , valBytes );
434+ }
435+ }, 100 );
415436 }
416437 }
417438
0 commit comments