Java Code Examples for android.bluetooth.bluetoothgatt#STATE_CONNECTING

The following examples show how to use android.bluetooth.bluetoothgatt#STATE_CONNECTING . 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: Device.java    From neatle with MIT License 6 votes vote down vote up
@VisibleForTesting
void connectWithGatt() {
    int oldState;
    int newState = BluetoothGatt.STATE_CONNECTING;
    synchronized (lock) {
        oldState = state;
        state = BluetoothGatt.STATE_CONNECTING;
    }

    NeatleLogger.d("Connecting with " + device.getName() + "[" + device.getAddress() + "]");
    BluetoothGatt gatt = device.connectGatt(context, false, callback);

    synchronized (lock) {
        if (state == BluetoothGatt.STATE_DISCONNECTED) {
            gatt = null;
        }

        this.gatt = gatt;
        if (gatt == null) {
            state = BluetoothGatt.STATE_DISCONNECTED;
            newState = BluetoothGatt.STATE_DISCONNECTED;
        }
    }

    notifyConnectionStateChange(oldState, newState);
}
 
Example 2
Source File: DeviceMirror.java    From BLE with Apache License 2.0 6 votes vote down vote up
/**
 * 连接状态改变,主要用来分析设备的连接与断开
 * @param gatt GATT
 * @param status 改变前状态
 * @param newState 改变后状态
 */
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
    ViseLog.i("onConnectionStateChange  status: " + status + " ,newState: " + newState +
            "  ,thread: " + Thread.currentThread());
    if (newState == BluetoothGatt.STATE_CONNECTED) {
        gatt.discoverServices();
    } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
        close();
        if (connectCallback != null) {
            if (handler != null) {
                handler.removeCallbacksAndMessages(null);
            }
            ViseBle.getInstance().getDeviceMirrorPool().removeDeviceMirror(deviceMirror);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                connectState = ConnectState.CONNECT_DISCONNECT;
                connectCallback.onDisconnect(isActiveDisconnect);
            } else {
                connectState = ConnectState.CONNECT_FAILURE;
                connectCallback.onConnectFailure(new ConnectException(gatt, status));
            }
        }
    } else if (newState == BluetoothGatt.STATE_CONNECTING) {
        connectState = ConnectState.CONNECT_PROCESS;
    }
}
 
Example 3
Source File: Device.java    From neatle with MIT License 5 votes vote down vote up
private void deviceDiscovered() {
    stopDiscovery();

    int state = getState();
    if (state == BluetoothGatt.STATE_CONNECTING) {
        NeatleLogger.i("Device discovered. Continuing with connecting");
        connectWithGatt();
    } else {
        NeatleLogger.e("Device discovered but no longer connecting");
    }
}
 
Example 4
Source File: Device.java    From neatle with MIT License 5 votes vote down vote up
public void run() {
    stopDiscovery();

    int state = getState();
    if (state == BluetoothGatt.STATE_CONNECTING) {
        NeatleLogger.e("Device no discovered failing connection attempt.");
        connectionFailed(BluetoothGatt.GATT_FAILURE);
    } else {
        NeatleLogger.e("Discover timeout but we are not connecting anymore.");
    }
}
 
Example 5
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 6
Source File: Device.java    From neatle with MIT License 4 votes vote down vote up
@Override
public void connect() {
    int oldState;
    int newState;
    boolean doConnectGatt = false;
    boolean doDiscovery = false;
    boolean adapterEnabled = adapter != null && adapter.isEnabled();

    synchronized (lock) {
        if (isConnected() || isConnecting()) {
            return;
        }
        if (this.gatt != null) {
            throw new IllegalStateException();
        }

        oldState = state;
        if (!adapterEnabled) {
            //newState = BluetoothAdapter.STATE_OFF;
            NeatleLogger.d("BT off. Won't connect to " + device.getName() + "[" + device.getAddress() + "]");
            connectionFailed(BluetoothGatt.GATT_FAILURE);
            return;
        } else {
            newState = BluetoothGatt.STATE_CONNECTING;
            if (device.getType() == BluetoothDevice.DEVICE_TYPE_UNKNOWN) {
                doDiscovery = true;
            } else {
                doConnectGatt = true;
            }
        }
    }
    //call these methods outside of the lock, to prevent deadlocks
    if (doConnectGatt) {
        connectWithGatt();
        return;
    }
    synchronized (lock) {
        state = newState;
    }

    notifyConnectionStateChange(oldState, newState);

    if (doDiscovery) {
        NeatleLogger.d("Device unknown, let's discover it" + device.getName() + "[" + device.getAddress() + "]");
        discoverDevice();
    }
}
 
Example 7
Source File: Device.java    From neatle with MIT License 4 votes vote down vote up
public boolean isConnecting() {
    synchronized (lock) {
        return state == BluetoothGatt.STATE_CONNECTING;
    }
}
 
Example 8
Source File: P_NativeDeviceWrapper.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyConnecting()
{
	return getConnectionState() == BluetoothGatt.STATE_CONNECTING;
}
 
Example 9
Source File: P_NativeDeviceWrapper.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyConnectingOrConnected()
{
	int state = getConnectionState();
	return state == BluetoothGatt.STATE_CONNECTED || state == BluetoothGatt.STATE_CONNECTING;
}
 
Example 10
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Connects to the Bluetooth Smart device.
 *
 * @param device a device to connect to
 */
public void connect(final BluetoothDevice device) {
	if (connected)
		return;

	synchronized (lock) {
		if (bluetoothGatt != null) {
			// There are 2 ways of reconnecting to the same device:
			// 1. Reusing the same BluetoothGatt object and calling connect() - this will force the autoConnect flag to true
			// 2. Closing it and reopening a new instance of BluetoothGatt object.
			// The gatt.close() is an asynchronous method. It requires some time before it's finished and
			// device.connectGatt(...) can't be called immediately or service discovery
			// may never finish on some older devices (Nexus 4, Android 5.0.1).
			// If shouldAutoConnect() method returned false we can't call gatt.connect() and have to close gatt and open it again.
			if (!initialConnection) {
				bluetoothGatt.close();
				bluetoothGatt = null;
				try {
					Thread.sleep(200); // Is 200 ms enough?
				} catch (final InterruptedException e) {
					// Ignore
				}
			} else {
				// Instead, the gatt.connect() method will be used to reconnect to the same device.
				// This method forces autoConnect = true even if the gatt was created with this flag set to false.
				initialConnection = false;
				connectionState = BluetoothGatt.STATE_CONNECTING;
				callbacks.onDeviceConnecting(device);
				bluetoothGatt.connect();
				return;
			}
		} else {
			// Register bonding broadcast receiver
			context.registerReceiver(bluetoothStateBroadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
			context.registerReceiver(bondingBroadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
		}
	}

	final boolean shouldAutoConnect = shouldAutoConnect();
	userDisconnected = !shouldAutoConnect; // We will receive Linkloss events only when the device is connected with autoConnect=true
	// The first connection will always be done with autoConnect = false to make the connection quick.
	// If the shouldAutoConnect() method returned true, the manager will automatically try to reconnect to this device on link loss.
	if (shouldAutoConnect)
		initialConnection = true;
	bluetoothDevice = device;
	connectionState = BluetoothGatt.STATE_CONNECTING;
	callbacks.onDeviceConnecting(device);
	bluetoothGatt = device.connectGatt(context, false, gattCallback = new BleManagerGattCallback());
}
 
Example 11
Source File: P_NativeDeviceWrapper.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyConnecting()
{
	return getConnectionState() == BluetoothGatt.STATE_CONNECTING;
}
 
Example 12
Source File: P_NativeDeviceWrapper.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyConnectingOrConnected()
{
	int state = getConnectionState();
	return state == BluetoothGatt.STATE_CONNECTED || state == BluetoothGatt.STATE_CONNECTING;
}