Java Code Examples for android.bluetooth.bluetoothgatt#STATE_DISCONNECTING

The following examples show how to use android.bluetooth.bluetoothgatt#STATE_DISCONNECTING . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Disconnects from the device or cancels the pending connection attempt. Does nothing if device was not connected.
 * @return true if device is to be disconnected. False if it was already disconnected.
 */
public boolean disconnect() {
	userDisconnected = true;
	initialConnection = false;

	if (bluetoothGatt != null) {
		connectionState = BluetoothGatt.STATE_DISCONNECTING;
		callbacks.onDeviceDisconnecting(bluetoothGatt.getDevice());
		final boolean wasConnected = connected;
		bluetoothGatt.disconnect();

		if (!wasConnected) {
			// There will be no callback, the connection attempt will be stopped
			connectionState = BluetoothGatt.STATE_DISCONNECTED;
			callbacks.onDeviceDisconnected(bluetoothGatt.getDevice());
		}
		return true;
	}
	return false;
}
 
Example 2
Source File: LightActivity.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt, status, newState);
    if (newState == BluetoothGatt.STATE_DISCONNECTED || newState == BluetoothGatt.STATE_DISCONNECTING) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                disconnectWithModal();
            }
        });
    }
}
 
Example 3
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean internalDisconnect() {
	userDisconnected = true;
	initialConnection = false;
	ready = false;

	if (bluetoothGatt != null) {
		connectionState = BluetoothGatt.STATE_DISCONNECTING;
		log(Log.VERBOSE, connected ? "Disconnecting..." : "Cancelling connection...");
		final BluetoothDevice device = bluetoothGatt.getDevice();
		if (connected) {
			postCallback(c -> c.onDeviceDisconnecting(device));
			postConnectionStateChange(o -> o.onDeviceDisconnecting(device));
		}
		log(Log.DEBUG, "gatt.disconnect()");
		bluetoothGatt.disconnect();
		final boolean wasConnected = connected;
		if (wasConnected)
			return true;

		// If the device wasn't connected, there will be no callback after calling
		// gatt.disconnect(), the connection attempt will be stopped.
		connectionState = BluetoothGatt.STATE_DISCONNECTED;
		log(Log.INFO, "Disconnected");
		postCallback(c -> c.onDeviceDisconnected(device));
		postConnectionStateChange(o -> o.onDeviceDisconnected(device, ConnectionObserver.REASON_SUCCESS));
	}
	// request may be of type DISCONNECT or CONNECT (timeout).
	// For the latter, it has already been notified with REASON_TIMEOUT.
	if (request != null && request.type == Request.Type.DISCONNECT) {
		if (bluetoothDevice != null)
			request.notifySuccess(bluetoothDevice);
		else
			request.notifyInvalidRequest();
	}
	nextRequest(true);
	return true;
}
 
Example 4
Source File: RxBleGattCallback.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
static RxBleConnectionState mapConnectionStateToRxBleConnectionStatus(int newState) {

        switch (newState) {
            case BluetoothGatt.STATE_CONNECTING:
                return RxBleConnectionState.CONNECTING;
            case BluetoothGatt.STATE_CONNECTED:
                return RxBleConnectionState.CONNECTED;
            case BluetoothGatt.STATE_DISCONNECTING:
                return RxBleConnectionState.DISCONNECTING;
            default:
                return RxBleConnectionState.DISCONNECTED;
        }
    }
 
Example 5
Source File: BleProfileService.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Disconnects from the sensor.
 */
public final void disconnect() {
    final int state = bleManager.getConnectionState();
    if (state == BluetoothGatt.STATE_DISCONNECTED || state == BluetoothGatt.STATE_DISCONNECTING) {
        bleManager.close();
        onDeviceDisconnected(bluetoothDevice);
        return;
    }

    bleManager.disconnect().enqueue();
}
 
Example 6
Source File: RxBleGattCallback.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
private boolean isDisconnectedOrDisconnecting(int newState) {
    return newState == BluetoothGatt.STATE_DISCONNECTED || newState == BluetoothGatt.STATE_DISCONNECTING;
}
 
Example 7
Source File: P_NativeDeviceWrapper.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyDisconnecting()
{
	return getConnectionState() == BluetoothGatt.STATE_DISCONNECTING;
}
 
Example 8
Source File: P_NativeDeviceWrapper.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyDisconnecting()
{
	return getConnectionState() == BluetoothGatt.STATE_DISCONNECTING;
}