I'm working on BLE based android application. Where I'm testing it on Samsung S4. When I close application from recent app list, what app do is close Bluetooth GATT.
Problem is if I unpair bluetooth device from the bluetooth setting. Then on second try if user try to disconnect connection it also closes Bluetooth Gatt or On closing BluetoothGatt it doen't close the bluetooth gatt. So, the behavior of Bluetooth gatt operation is very unstable on Samsung S4.
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS
&& newState == BluetoothProfile.STATE_CONNECTED) {
// Do something
} else if (status == BluetoothGatt.GATT_SUCCESS
&& newState == BluetoothProfile.STATE_DISCONNECTED) {
mBluetoothDeviceAddress = null;
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
//....
} else if (status != BluetoothGatt.GATT_SUCCESS) {
mBluetoothDeviceAddress = null;
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
//...
}
}
}
To disconnect Bluetooth connection.
public void disconnect() {
if (mBtAdapter == null) {
Log.w(TAG, "disconnect: BluetoothAdapter not initialized");
return;
}
if (mBluetoothGatt != null) {
Log.i(TAG, "disconnect");
// mBluetoothGatt.disconnect();
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mBluetoothGatt.disconnect();
}
});
}
}
To close bluetooth gatt.
public void close() {
if (mBluetoothGatt != null) {
if (mBluetoothDeviceAddress != null) {
Log.i(TAG, "close");
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
// mBluetoothGatt.close();
}
mBluetoothGatt = null;
}
}
I'm performing all this BluetoothGatt related operation on service.
Device: Samsung S4
Android version: 5.0.1
Build number: LRX22C.19500XXUHOK2