title: Android Central with GattServer
body: <p>I'm trying to program Bluetooth low energy - AlertNotificationService (ANS). It is a bit weird service because typically services run on GAP peripheral. But ANS run on GAP central. So typical way of working shall be for example this:</p>
  
  <pre><code>Watch - GAP peripheral (broadcasting), GATT client
  Phone - GAP central                  , GATT server
  </code></pre>
  
  <p>Basically it works for me but not always. And this instability is very strange to me. When I look with bluetooth LE analyzer I see that Android GATT server sometimes tells there are no characteristic in my profile...   </p>
  
  <p>It looks like this: "watch" ask for my GATT service (I know it is proprietary not ANS UUID)</p>
  
  <pre><code>Slave-&gt;Master ATT Rcvd Find By Type Value Request, GATT Primary Service Declaration 11:22:33:44:11:22:33:44:11:76:62:65:01:00:00:00
  </code></pre>
  
  <p>Phone says the service is there starting from handle 0x35</p>
  
  <pre><code>Master-&gt;Slave ATT Rcvd Find By Type Value Response Handle: 0x0035
  </code></pre>
  
  <p>Watch ask for charactersitisc from handle 0x35            </p>
  
  <pre><code>Slave-&gt;Master ATT Rcvd Read By Type Request, GATT Characteristic Declaration, Handles: 0x0035..0xffff
  </code></pre>
  
  <p>But phone sometimes incorrectly says that there is no characteristic from that handle:</p>
  
  <pre><code>Master-&gt;Slave ATT Rcvd Error Response - Attribute Not Found, Handle: 0x0035
  </code></pre>
  
  <p>When I do add service and characteristic into GATT server I always get "true" from functions. I do it like this: </p>
  
  <pre><code>BluetoothGattService service =new BluetoothGattService(Vbe_AnsExt.UUID_SERVICE,
          BluetoothGattService.SERVICE_TYPE_PRIMARY);
  
  BluetoothGattCharacteristic characApp =
          new BluetoothGattCharacteristic(Vbe_AnsExt.UUID_CharacApp,
                  BluetoothGattCharacteristic.PROPERTY_READ ,
                  BluetoothGattCharacteristic.PERMISSION_READ);
  
  BluetoothGattCharacteristic characMsg =
          new BluetoothGattCharacteristic(Vbe_AnsExt.UUID_CharacMsg,
                  BluetoothGattCharacteristic.PROPERTY_READ ,
                  BluetoothGattCharacteristic.PERMISSION_READ );
  
  boolean ret;
  ret = service.addCharacteristic(characApp);
  Log.i("vbeInit_ASN_Ext_Server","addCharApp  retruned: "+ret);
  ret = service.addCharacteristic(characMsg);
  Log.i("vbeInit_ASN_Ext_Server","addCharMsg  retruned: "+ret);
  
  ret = mGattServer.addService(service);
  Log.i("vbeInit_ASN_Ext_Server","addService  retruned: "+ret);
  </code></pre>
  
  <p>Any idea what might be the problem? I noticed that there is function <code>BluetoothGattServer::connect()</code>. I'm not sure how to use it. I'm using standard <code>BluetoothDevice::connectGatt()</code>. But I guess if I would do something wrong it shall work never - not sometimes... 
  I'm using Android M (6.0.1) on Samsung SM-G920F. </p>
  
  <p>[UPDATE]</p>
  
  <p>I noticed that after phone restart it always works. After application close and reopen it usually does not works. It does not works in different ways -> </p>
  
  <ul>
  <li>sometimes no characteristic is found;  </li>
  <li>sometimes only first characteristic is found  </li>
  <li>sometimes only last(second) characteristic is found.</li>
  </ul>
  
  <p>When I start Gatt Server I do it like this:</p>
  
  <pre><code>mGattServer = bluetoothManager.openGattServer(appContext, mGattServerCallback);
  mGattServer.clearServices();
  </code></pre>
  
  <p>When I close app (<code>onDestroy()</code>) I do close gattserver:</p>
  
  <pre><code>mGattServer.close();
  </code></pre>
  
  <p>I also tried not to close the GATT server, but it did not help. Any ideas what might get wrong between closing and reopening?</p>