https://github.com/googlesamples/android-BluetoothLeGatt 의 소스로 개발 중이다.
disconnect를 해야하는데, disconnect()만 호출하면 연결이 완전히 종료되지 않는지 close()도 같이 호출해줘야 한다.
그래서 곧바로 호출했더니 이런 오류가 뜬다.
Unhandled exception in callback java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothGattCallback.onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)' on a null object reference at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:182) at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70) at android.os.Binder.execTransact(Binder.java:446)
이상하게도 검색이 잘 안됐는데, 다행히 https://code.google.com/p/android/issues/detail?id=58607#c6 에서 답변을 찾았다.
동시에 호출하면 생기는 문제였다.
if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.disconnect(); new Handler().postDelayed(new Runnable() { @Override public void run() { mBluetoothGatt.close(); mBluetoothGatt = null; } }, 1000);