OSDN Git Service

Fix NPE when accessing mCallback in BluetoothGatt
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 27 Apr 2017 14:04:15 +0000 (07:04 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 27 Apr 2017 20:16:32 +0000 (13:16 -0700)
This issue was introduced in commit
4eab49652e48159fd37f827d30b822f2f187551e.

Bug: 37710354
Test: none
Change-Id: I2d985ce97c44d4e096713e156e57437f44ea3ddb
(cherry picked from commit 0e4ac75f2e291305a493796186d44a081926f3a8)

core/java/android/bluetooth/BluetoothGatt.java

index e77ab2e..0f01d62 100644 (file)
@@ -159,8 +159,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                     mHandler.post(new Runnable() {
                         @Override
                         public void run() {
-                            mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
-                                              BluetoothProfile.STATE_DISCONNECTED);
+                            if (mCallback != null) {
+                                mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
+                                                  BluetoothProfile.STATE_DISCONNECTED);
+                            }
                         }
                     });
 
@@ -192,7 +194,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status);
+                        if (mCallback != null) {
+                            mCallback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status);
+                        }
                     }
                 });
             }
@@ -212,7 +216,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status);
+                        if (mCallback != null) {
+                            mCallback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status);
+                        }
                     }
                 });
             }
@@ -235,7 +241,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState);
+                        if (mCallback != null) {
+                            mCallback.onConnectionStateChange(BluetoothGatt.this, status,
+                                                              profileState);
+                        }
                     }
                 });
 
@@ -294,7 +303,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onServicesDiscovered(BluetoothGatt.this, status);
+                        if (mCallback != null) {
+                            mCallback.onServicesDiscovered(BluetoothGatt.this, status);
+                        }
                     }
                 });
             }
@@ -344,7 +355,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, status);
+                        if (mCallback != null) {
+                            mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic,
+                                                           status);
+                        }
                     }
                 });
             }
@@ -390,7 +404,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status);
+                        if (mCallback != null) {
+                            mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic,
+                                                            status);
+                        }
                     }
                 });
             }
@@ -416,7 +433,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
+                        if (mCallback != null) {
+                            mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
+                        }
                     }
                 });
             }
@@ -461,7 +480,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
+                        if (mCallback != null) {
+                            mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
+                        }
                     }
                 });
             }
@@ -505,7 +526,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
+                        if (mCallback != null) {
+                            mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
+                        }
                     }
                 });
             }
@@ -529,7 +552,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
+                        if (mCallback != null) {
+                           mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
+                        }
                     }
                 });
             }
@@ -548,7 +573,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
+                        if (mCallback != null) {
+                            mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
+                        }
                     }
                 });
             }
@@ -568,7 +595,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onMtuChanged(BluetoothGatt.this, mtu, status);
+                        if (mCallback != null) {
+                            mCallback.onMtuChanged(BluetoothGatt.this, mtu, status);
+                        }
                     }
                 });
             }
@@ -590,8 +619,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
-                        mCallback.onConnectionUpdated(BluetoothGatt.this, interval, latency,
-                                                      timeout, status);
+                        if (mCallback != null) {
+                            mCallback.onConnectionUpdated(BluetoothGatt.this, interval, latency,
+                                                          timeout, status);
+                        }
                     }
                 });
             }