title: bluetooth le gatt client and server
body: <p>I have been trying to get an android ble gatt client to talk to an android ble gatt server. </p>
  
  <p>I have been following the example discussed at this blog:</p>
  
  <p><a href="https://www.bignerdranch.com/blog/bluetooth-low-energy-part-1/" rel="nofollow noreferrer">https://www.bignerdranch.com/blog/bluetooth-low-energy-part-1/</a></p>
  
  <p>The source code for the blog is here:</p>
  
  <p><a href="https://github.com/bignerdranch/android-bluetooth-testbed" rel="nofollow noreferrer">https://github.com/bignerdranch/android-bluetooth-testbed</a></p>
  
  <p>The sample app can act as a gatt client or server. When I start the server, it correctly advertises the peripheral and the client scans and find the server. When I press "connect", the onCharacteristicReadRequest of the GattServerCallback is called and it attempts to send a response back to the client on line 52 of GattServerCallback.java:</p>
  
  <p><a href="https://github.com/bignerdranch/android-bluetooth-testbed/blob/master/BluetoothLowEnergy/app/src/main/java/com/bignerdranch/android/bluetoothtestbed/server/GattServerCallback.java" rel="nofollow noreferrer">https://github.com/bignerdranch/android-bluetooth-testbed/blob/master/BluetoothLowEnergy/app/src/main/java/com/bignerdranch/android/bluetoothtestbed/server/GattServerCallback.java</a></p>
  
  <p>This calls fails with a null pointer error:</p>
  
  <pre><code>17 14:44:43.358 17888-17925/com.bignerdranch.android.bluetoothlowenergy W/BluetoothGattServer: Unhandled exception in callback
      java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
          at android.os.Parcel.readException(Parcel.java:1699)
          at android.os.Parcel.readException(Parcel.java:1646)
          at android.bluetooth.IBluetoothGatt$Stub$Proxy.sendResponse(IBluetoothGatt.java:1424)
          at android.bluetooth.BluetoothGattServer.sendResponse(BluetoothGattServer.java:599)
          at com.bignerdranch.android.bluetoothtestbed.server.ServerActivity.sendResponse(ServerActivity.java:315)
          at com.bignerdranch.android.bluetoothtestbed.server.GattServerCallback.onDescriptorWriteRequest(GattServerCallback.java:115)
          at android.bluetooth.BluetoothGattServer$1.onDescriptorWriteRequest(BluetoothGattServer.java:261)
          at android.bluetooth.IBluetoothGattServerCallback$Stub.onTransact(IBluetoothGattServerCallback.java:263)
          at android.os.Binder.execTransact(Binder.java:573)
  </code></pre>
  
  <p>Following the exception, the client disconnects and the <code>onConnectionStateChange</code> method is called and the client device is removed on lines 34 to 36:</p>
  
  <pre><code>else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
          mServerActionListener.removeDevice(device);
  }
  </code></pre>
  
  <p>No further communication between the client and server is possible. On the client there is a text box which allows you to send a message to the server but no message gets through to the server presumably because of the disconnect.</p>
  
  <p>I have put together an example which generates the same exception up on github at <a href="https://github.com/jmc420/ble-echo" rel="nofollow noreferrer">ble-echo</a>.</p>
  
  <p>Can anyone explain why this example does not work?</p>