title: How to prevent BluetoothGattCallback from being executed multiple times at a time
body: <p>I have a service that has one instance of <code>BluetoothGattCallback</code></p>
  
  <pre><code>public class MyService extends Service {
  
      private BluetoothGattCallback callback;
  
      @Override
      public void onCreate() {
              super.onCreate();
  
              callback = new BluetoothGattCallback() {
                        @Override
                        public synchronized void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                                Log.i("onConnectionStateChanged", "Status " + status);                
                                Log.i("onConnectionStateChanged", "New State " + newState);                
                        }
              };
      }
  
      // registration of bluetooth adapter and blah blah blah
  
  
  }
  </code></pre>
  
  <p>When I start the app, it works fine and the callback only gets called once, but after a couple of tries, it gets called twice.</p>
  
  <p>Sample logs</p>
  
  <pre><code>10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
  10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2
  10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
  10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2
  </code></pre>
  
  <p>More Sample logs</p>
  
  <pre><code>10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 8
  10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 0
  10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: Status 8
  10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: New State 0
  </code></pre>
  
  <p>And it gets called a lot more times the longer the app stays active. How do I prevent this?</p>