title: BLuetooth Gatt Callback not working with new API for Lollipop
body: <p>I currently have a method which writes to the BLE devices to beep it. My Bluetooth Callback goes as follows : </p>
  
  <pre><code>ReadCharacteristic rc = new ReadCharacteristic(context, ds.getMacAddress(), serviceUUID, UUID.fromString(myUUID), "") {
                  @Override
                  public void onRead() {
                      Log.w(TAG, "callDevice onRead");
                      try{Thread.sleep(1000);}catch(InterruptedException ex){}
                      WriteCharacteristic wc = new WriteCharacteristic(activity, context, getMacAddress(), serviceUUID, UUID.fromString(myUUID), ""){
                          @Override
                          public void onWrite(){
                              Log.w(TAG, "callDevice onWrite");
                          }
                          @Override
                          public void onError(){
                              Log.w(TAG, "callDevice onWrite-onError");
                          }
                      };
  
  //                  Store data in writeBuffer
                      wc.writeCharacteristic(writeBuffer);
                  }
  
                  @Override
                  public void onError(){
                      Log.w(TAG, "callDevice onRead-onError");
                  }
              };
  
              rc.readCharacteristic();
  </code></pre>
  
  <p>My ReadCharacteristic implementation is as follows :</p>
  
  <pre><code>public class ReadCharacteristic extends BluetoothGattCallback {
      public ReadCharacteristic(Context context, String macAddress, UUID service, UUID characteristic, Object tag) {
          mMacAddress = macAddress;
          mService = service;
          mCharacteristic = characteristic;
          mTag = tag;
          mContext = context;
          this.activity =activity;
          final BluetoothManager bluetoothManager =
                  (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
          mBluetoothAdapter = bluetoothManager.getAdapter();
  
      }
  
      final private static String TAG = "ReadCharacteristic";
      private Object mTag;
      private String mMacAddress;
      private UUID mService;
      private UUID mCharacteristic;
      private byte[] mValue;
      private Activity activity;
      private BluetoothAdapter mBluetoothAdapter;
      private Context mContext;
  
      private int retry = 5;
  
  
      public String getMacAddress() {
          return mMacAddress;
      }
  
      public UUID getService() {
          return mService;
      }
  
      public UUID getCharacteristic() {
          return mCharacteristic;
      }
  
      public byte[] getValue() { return mValue; }
  
      public void onRead() {
          Log.w(TAG, "onRead: " + getDataHex(getValue()));
      }
  
      public void onError() {
          Log.w(TAG, "onError");
      }
  
      public void readCharacteristic(){
          if (retry == 0)
          {
              onError();
              return;
          }
          retry--;
  
  
  
                  final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(getMacAddress());
                  if (device != null) {
                      Log.w(TAG, "Starting Read [" + getService() + "|" + getCharacteristic() + "]");
                      final ReadCharacteristic rc = ReadCharacteristic.this;
                      device.connectGatt(mContext, false, rc);
                  }
  
      }
  
      @Override
      public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          Log.w(TAG,"onConnectionStateChange [" + status + "|" + newState + "]");
          if ((newState == 2)&amp;&amp;(status ==0)) {
              gatt.discoverServices();
          }
  
          else{
              Log.w(TAG, "[" + status + "]");
           //   gatt.disconnect();
              gatt.close();
              try
              {
                  Thread.sleep(2000);
              }
              catch(Exception e)
              {
  
              }
              readCharacteristic();
          }
      }
  
      @Override
      public void onServicesDiscovered(BluetoothGatt gatt, int status) {
          Log.w(TAG,"onServicesDiscovered [" + status + "]");
          BluetoothGattService bgs = gatt.getService(getService());
          if (bgs != null) {
              BluetoothGattCharacteristic bgc = bgs.getCharacteristic(getCharacteristic());
              gatt.readCharacteristic(bgc);
          }
      }
  
      @Override
      public void onCharacteristicRead(BluetoothGatt gatt,
                                       BluetoothGattCharacteristic characteristic,
                                       int status) {
          Log.w(TAG,"onCharacteristicRead [" + status + "]");
          if (status == BluetoothGatt.GATT_SUCCESS) {
              mValue = characteristic.getValue();
              Log.w(TAG,"onCharacteristicRead [" + mValue + "]");
              gatt.disconnect();
              gatt.close();
              onRead();
          }
  
          else {
              gatt.disconnect();
              gatt.close();
          }
      }
  
  
  }
  </code></pre>
  
  <p>This current method works perfectly fine for devices running KitKat and below. But when I run the same function on Lollipop, it beeps the device a couple of times and then stops working. From then on wards, whenever I try to connect, it says the device is disconnected and gives me an error code of 257 in OnConnectionStateChanged method. </p>
  
  <p>I also get this error whenever I call this method - </p>
  
  <pre><code>04-20 14:14:23.503  12329-12384/com.webble.xy W/BluetoothGatt﹕ Unhandled exception in callback
      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothGattCallback.onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)' on a null object reference
              at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:181)
              at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
              at android.os.Binder.execTransact(Binder.java:446)
  </code></pre>
  
  <p>IS there anyone who has faced the same problem? I never encountered the object to be null when ever I tried debugging. </p>