title: Bluetooth LE advertising fails when started and stopped cyclically
body: <p>I've tried out Bluetooth LE advertising i.e. using an Android phone as a BLE "peripheral" device. Starting and stopping a constant advertising once works fine but if I'd like to actually transmit some varying data I would need to start, stop and restart the advertising in a cycle and always change the advertising package. </p>
  
  <p>I'm using these methods of course:<br>
  <a href="https://developer.android.com/reference/android/bluetooth/le/BluetoothLeAdvertiser.html#startAdvertising(android.bluetooth.le.AdvertiseSettings,%20android.bluetooth.le.AdvertiseData,%20android.bluetooth.le.AdvertiseCallback)" rel="nofollow noreferrer">BluetoothLeAdvertiser.startAdvertising()</a><br>
  <a href="https://developer.android.com/reference/android/bluetooth/le/BluetoothLeAdvertiser.html#stopAdvertising(android.bluetooth.le.AdvertiseCallback)" rel="nofollow noreferrer">BluetoothLeAdvertiser.stopAdvertising()</a></p>
  
  <p>At least on a Motorola Moto G 4G 2nd gen. (XT1072) with Android 5.0.2 the cycle only runs for a while and then something goes wrong in the Bluetooth stack and a new advertising can no longer be started. When running the cycle quickly this happens quickly and if running slowly it takes more time so it could be related to some buffer filling up.</p>
  
  <p><strong>My question is:</strong> Does this also happen on other devices than Motorola?</p>
  
  <p>(And any other related comments are of course welcome.)</p>
  
  <p>Theoretically it could be dependent on some manufacturer specific HAL implementation etc. so I'd like to know if I should just get another device to be able to work on this as any fix from Google/Motorola would take some time of course.</p>
  
  <p>This test app. can be used to try this out:</p>
  
  <p><a href="https://bitbucket.org/MarkusKauppinen/bleadvertisertest" rel="nofollow noreferrer">https://bitbucket.org/MarkusKauppinen/bleadvertisertest</a></p>
  
  <p>If everything goes fine it'll happily keep advertising for ever and if this problem reproduces, it will show a dialog within a couple of minutes or so. If your device doesn't support Bluetooth LE advertising or BLE at all it will just crash. (It's just a quick-and-dirty test app.)</p>
  
  <p>Another easy way to reproduce this is to run the "bluetoothadvertiser" app from <a href="https://github.com/devunwired/accessory-samples" rel="nofollow noreferrer">https://github.com/devunwired/accessory-samples</a> and just keep quickly tapping on the "Update advertisement" button on the UI for a while.</p>
  
  <p>Additional details:</p>
  
  <p>The Bluetooth LE peripheral mode support (needed for advertising) was added in Lollipop and is not available in 4.x. Only certain devices have the peripheral mode support. Compatibility is covered at least in:</p>
  
  <p><a href="https://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html" rel="nofollow noreferrer">https://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html</a>
  <a href="https://stackoverflow.com/questions/26482611/chipsets-devices-supporting-android-5-ble-peripheral-mode">Chipsets/Devices supporting Android 5 BLE peripheral mode</a>
  <a href="https://stackoverflow.com/questions/26997049/chipsets-supporting-ble-peripheral-role-on-android-5">chipsets supporting BLE peripheral role on Android 5</a></p>
  
  <p>For example these devices should have the support: Motorola Moto E 4G (2015), Motorola Moto G 4G (2nd gen.), Sony Xperia M4 Aqua, ZTE Blade S6, Motorola Moto X (2014), Samsung Galaxy S6, Google Nexus 6, Google Nexus 9, HTC One M9, Samsung Galaxy S6 Edge.</p>
  
  <p>As for the "dirty details" I can always see a "GKI_Exception" in LogCat before this happens:</p>
  
  <blockquote>
    <p>GKI_exception(): 65524 getbuf: out of buffers</p>
  </blockquote>
  
  <p>Soon after the first "GKI_Exception" the advertisement fails with AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR. The line...</p>
  
  <blockquote>
    <p>E/BtGatt.btif? ### ASSERT :
    external/bluetooth/bluedroid/main/../btif/src/btif_gatt_client.c line
    803 Context transfer failed! (3) ###</p>
  </blockquote>
  
  <p>...is probably relevant but I'm not familiar with BlueDroid. The said assert is here: </p>
  
  <p><a href="https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.0.2_r1/btif/src/btif_gatt_client.c#803" rel="nofollow noreferrer">https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.0.2_r1/btif/src/btif_gatt_client.c#803</a></p>
  
  <p>Some possibly related problems:<br>
  <a href="https://code.google.com/p/android/issues/detail?id=65455" rel="nofollow noreferrer">https://code.google.com/p/android/issues/detail?id=65455</a> &lt;-- I added some comments and log files to this one.<br>
  <a href="https://code.google.com/p/android-developer-preview/issues/detail?id=1753" rel="nofollow noreferrer">https://code.google.com/p/android-developer-preview/issues/detail?id=1753</a><br>
  <a href="https://stackoverflow.com/questions/20369888/scanning-large-number-of-ble-tags">Scanning large number of BLE Tags</a><br>
  <a href="https://stackoverflow.com/questions/24804613/bluetooth-crash-on-samsung-s4">Bluetooth Crash on Samsung S4</a>  </p>