title: BluetoothLeAdvertiser.stopAdvertising() causes device to disconnect
body: <p>Here my <a href="https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback" rel="nofollow noreferrer">BluetoothGattServerCallback</a> where I handle two events <code>STATE_CONNECTED</code> and <code>STATE_DISCONNECTED</code></p>
  
  <pre><code>    private static class CallBack extends BluetoothGattServerCallback {
  
          private Set&lt;BluetoothDevice&gt; mRegisteredDevices = new HashSet&lt;&gt;();
          private MainActivity mMainActivity;
  
          public CallBack(Set&lt;BluetoothDevice&gt; registeredDevices, MainActivity mainActivity){
              mRegisteredDevices = registeredDevices;
              mMainActivity =  mainActivity;
          }
  
          @Override
          public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
  
              Log.i(TAG,"onConnectionStateChange()");
  
              switch (newState)
              {
                  case STATE_CONNECTED :
                      handleStateConnected( device,  status,  newState);
                      break;
                  case STATE_DISCONNECTED :
                      handleStateDisconnected( device,  status, newState);
                      break;
              }
      }
  </code></pre>
  
  <p>Here is handler <code>STATE_CONNECTED</code></p>
  
  <pre><code>    private void handleStateConnected(BluetoothDevice device, int status, int newState)
      {
          Log.i(TAG, "BluetoothDevice CONNECTED: " + device);
          mRegisteredDevices.add(device);
          stopAdvertising();
      }
  </code></pre>
  
  <p>Here is handler <code>STATE_DISCONNECTED</code></p>
  
  <pre><code>    private void handleStateDisconnected(BluetoothDevice device, int status, int newState)
      {
          Log.i(TAG, "BluetoothDevice DISCONNECTED: " + device);
          mRegisteredDevices.remove(device);
          startAdvertising();
      }
  </code></pre>
  
  <p>On <code>STATE_CONNECTED</code> I would like my phone to stop advertising, and on <code>STATE_DISCONNECTED</code> I would like the phone to resume advertising; the default behaviour appears to be that the device will continue advertising after it's connected, so I added <code>stopAdvertising()</code> and <code>startAdvertising()</code> in the event handlers <code>handleStateConnected()</code> and <code>handleStateDisconnected()</code> respectively.</p>
  
  <p>The phone now goes into an ifinite loop of connecting and recconnecting, the reason being that after calling <code>stopAdvertising()</code> the phone will disconnect, causing it to begin advertising again, after which it will connect. </p>
  
  <p>The control flow of my app is to begin advertising, then set up GATT server:</p>
  
  <pre><code>private void initServer(){
  
      Log.v(TAG,"initServer()");
  
      mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
  
      BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
  
      // We can't continue without proper Bluetooth support
      if (!checkBluetoothSupport(bluetoothAdapter)) {
          finish();
      }
  
      // Register for system Bluetooth events (GATT server started in receiver)
      IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
      registerReceiver(mBluetoothReceiver, filter);
  
      if (!bluetoothAdapter.isEnabled()) {
          Log.d(TAG, "Bluetooth is currently disabled...enabling");
          bluetoothAdapter.enable();
  
      } else {
  
          startAdvertising();
          startServer();
      }
  }
  </code></pre>
  
  <p>Here is <code>startAdvertising()</code></p>
  
  <pre><code>public static void startAdvertising() {
  
      Log.i(TAG,"startAdvertising()");
  
      BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
  
      mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
      if (mBluetoothLeAdvertiser == null) {
          Log.w(TAG, "Failed to create mBleAdvertiser");
          return;
      }
  
      Log.v(TAG,"created advertizer");
  
      AdvertiseSettings settings = new AdvertiseSettings.Builder()
              .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
              .setConnectable(true)
              .setTimeout(0) // Limit advertising to a given amount of time A value of 0 will disable the time limit
              .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
              .build();
  
      boolean isNameChanged = BluetoothAdapter.getDefaultAdapter().setName(DEVICE_NAME);
      if(isNameChanged) Log.d(TAG,"Device name changed successfully.");
  
      AdvertiseData data = new AdvertiseData.Builder()
              .setIncludeDeviceName(true)
              .setIncludeTxPowerLevel(false)
              .addServiceUuid(ParcelUuid.fromString(Services.GloService.UUID))
              .build();
  
      mBluetoothLeAdvertiser
              .startAdvertising(settings, data, mAdvertiseCallback);
  }
  </code></pre>
  
  <p>Here is <code>startServer()</code></p>
  
  <pre><code>private void startServer() {
  
      Log.i(TAG,"startServer()");
  
      mBluetoothGattServer =
              MyProfile.getOpenServer(this, mBluetoothManager,mRegisteredDevices,this);
  
      if (mBluetoothGattServer == null) {
          Log.w(TAG, "Unable to create GATT server");
          return;
      }
  }
  </code></pre>
  
  <p>And here is <code>MyProfile.getOpenServer</code> which returns the server object initialized with the call back declared at the top of the post. </p>
  
  <pre><code>public static BluetoothGattServer getOpenServer(Context ctx,
                                                  BluetoothManager bluManager,
                                                  Set&lt;BluetoothDevice&gt; registeredDevices,
                                                  MainActivity mainActivity){
      mBluetoothManager = bluManager;
      CallBack callBack = new CallBack(registeredDevices, mainActivity);
      BluetoothGattServer server = bluManager.openGattServer(ctx, callBack);
      BluetoothGattService myService = getMyService();
      server.addService(myService);
      return server;
  }
  </code></pre>