@Test public void cancelConnectionTest() throws Exception { peripheral.connect(); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); ArgumentCaptor<BluetoothGattCallback> captor = ArgumentCaptor.forClass(BluetoothGattCallback.class); verify(device).connectGatt(any(Context.class), anyBoolean(), captor.capture(), anyInt()); BluetoothGattCallback callback = captor.getValue(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); peripheral.cancelConnection(); verify(gatt).disconnect(); assertEquals(STATE_DISCONNECTING, peripheral.getState()); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_DISCONNECTED); verify(gatt).close(); assertEquals(STATE_DISCONNECTED, peripheral.getState()); }
public void cancelConnection() { if (bluetoothGatt == null) { Timber.w("cannot cancel connection because no connection attempt is made yet"); return; } if (state == BluetoothProfile.STATE_DISCONNECTED || state == BluetoothProfile.STATE_DISCONNECTING) { return; } cancelConnectionTimer(); if (state == BluetoothProfile.STATE_CONNECTING) { disconnect(); mainHandler.postDelayed(new Runnable() { @Override public void run() { bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); } }, 50); } else { disconnect(); } }
BluetoothPeripheral { public void cancelConnection() { if (bluetoothGatt == null) { Timber.w("cannot cancel connection because no connection attempt is made yet"); return; } if (state == BluetoothProfile.STATE_DISCONNECTED || state == BluetoothProfile.STATE_DISCONNECTING) { return; } cancelConnectionTimer(); if (state == BluetoothProfile.STATE_CONNECTING) { disconnect(); mainHandler.postDelayed(new Runnable() { @Override public void run() { bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); } }, 50); } else { disconnect(); } } }
BluetoothPeripheral { public void cancelConnection() { if (bluetoothGatt == null) { Timber.w("cannot cancel connection because no connection attempt is made yet"); return; } if (state == BluetoothProfile.STATE_DISCONNECTED || state == BluetoothProfile.STATE_DISCONNECTING) { return; } cancelConnectionTimer(); if (state == BluetoothProfile.STATE_CONNECTING) { disconnect(); mainHandler.postDelayed(new Runnable() { @Override public void run() { bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); } }, 50); } else { disconnect(); } } BluetoothPeripheral(@NotNull Context context, @NotNull BluetoothDevice device, @NotNull InternalCallback listener, BluetoothPeripheralCallback peripheralCallback, Handler callbackHandler); }
BluetoothPeripheral { public void cancelConnection() { if (bluetoothGatt == null) { Timber.w("cannot cancel connection because no connection attempt is made yet"); return; } if (state == BluetoothProfile.STATE_DISCONNECTED || state == BluetoothProfile.STATE_DISCONNECTING) { return; } cancelConnectionTimer(); if (state == BluetoothProfile.STATE_CONNECTING) { disconnect(); mainHandler.postDelayed(new Runnable() { @Override public void run() { bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); } }, 50); } else { disconnect(); } } BluetoothPeripheral(@NotNull Context context, @NotNull BluetoothDevice device, @NotNull InternalCallback listener, BluetoothPeripheralCallback peripheralCallback, Handler callbackHandler); boolean createBond(); boolean requestConnectionPriority(final int priority); void cancelConnection(); @NotNull String getAddress(); int getType(); String getName(); int getBondState(); @SuppressWarnings("WeakerAccess") @NotNull List<BluetoothGattService> getServices(); @Nullable BluetoothGattService getService(@NotNull UUID serviceUUID); @Nullable BluetoothGattCharacteristic getCharacteristic(@NotNull UUID serviceUUID, @NotNull UUID characteristicUUID); int getState(); int getCurrentMtu(); boolean isNotifying(@NotNull BluetoothGattCharacteristic characteristic); boolean readCharacteristic(@NotNull final BluetoothGattCharacteristic characteristic); boolean writeCharacteristic(@NotNull final BluetoothGattCharacteristic characteristic, @NotNull final byte[] value, final int writeType); boolean readDescriptor(@NotNull final BluetoothGattDescriptor descriptor); boolean writeDescriptor(@NotNull final BluetoothGattDescriptor descriptor, @NotNull final byte[] value); boolean setNotify(@NotNull final BluetoothGattCharacteristic characteristic, final boolean enable); boolean clearServicesCache(); boolean readRemoteRssi(); boolean requestMtu(final int mtu); }
BluetoothPeripheral { public void cancelConnection() { if (bluetoothGatt == null) { Timber.w("cannot cancel connection because no connection attempt is made yet"); return; } if (state == BluetoothProfile.STATE_DISCONNECTED || state == BluetoothProfile.STATE_DISCONNECTING) { return; } cancelConnectionTimer(); if (state == BluetoothProfile.STATE_CONNECTING) { disconnect(); mainHandler.postDelayed(new Runnable() { @Override public void run() { bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); } }, 50); } else { disconnect(); } } BluetoothPeripheral(@NotNull Context context, @NotNull BluetoothDevice device, @NotNull InternalCallback listener, BluetoothPeripheralCallback peripheralCallback, Handler callbackHandler); boolean createBond(); boolean requestConnectionPriority(final int priority); void cancelConnection(); @NotNull String getAddress(); int getType(); String getName(); int getBondState(); @SuppressWarnings("WeakerAccess") @NotNull List<BluetoothGattService> getServices(); @Nullable BluetoothGattService getService(@NotNull UUID serviceUUID); @Nullable BluetoothGattCharacteristic getCharacteristic(@NotNull UUID serviceUUID, @NotNull UUID characteristicUUID); int getState(); int getCurrentMtu(); boolean isNotifying(@NotNull BluetoothGattCharacteristic characteristic); boolean readCharacteristic(@NotNull final BluetoothGattCharacteristic characteristic); boolean writeCharacteristic(@NotNull final BluetoothGattCharacteristic characteristic, @NotNull final byte[] value, final int writeType); boolean readDescriptor(@NotNull final BluetoothGattDescriptor descriptor); boolean writeDescriptor(@NotNull final BluetoothGattDescriptor descriptor, @NotNull final byte[] value); boolean setNotify(@NotNull final BluetoothGattCharacteristic characteristic, final boolean enable); boolean clearServicesCache(); boolean readRemoteRssi(); boolean requestMtu(final int mtu); @SuppressWarnings("WeakerAccess")
    static final int GATT_SUCCESS; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_L2C_FAILURE; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_TIMEOUT; @SuppressWarnings("WeakerAccess")
    static final int GATT_READ_NOT_PERMITTED; @SuppressWarnings("WeakerAccess")
    static final int GATT_WRITE_NOT_PERMITTED; @SuppressWarnings("WeakerAccess")
    static final int GATT_INSUFFICIENT_AUTHENTICATION; @SuppressWarnings("WeakerAccess")
    static final int GATT_REQUEST_NOT_SUPPORTED; @SuppressWarnings("WeakerAccess")
    static final int GATT_INSUFFICIENT_ENCRYPTION; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_TERMINATE_PEER_USER; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_TERMINATE_LOCAL_HOST; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_LMP_TIMEOUT; @SuppressWarnings("WeakerAccess")
    static final int BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_FAIL_ESTABLISH; @SuppressWarnings("WeakerAccess")
    static final int GATT_NO_RESOURCES; @SuppressWarnings("WeakerAccess")
    static final int GATT_INTERNAL_ERROR; @SuppressWarnings("WeakerAccess")
    static final int GATT_BUSY; @SuppressWarnings("WeakerAccess")
    static final int GATT_ERROR; @SuppressWarnings("WeakerAccess")
    static final int GATT_AUTH_FAIL; @SuppressWarnings("WeakerAccess")
    static final int GATT_CONN_CANCEL; @SuppressWarnings("WeakerAccess")
    static final int DEVICE_TYPE_UNKNOWN; @SuppressWarnings("WeakerAccess")
    static final int DEVICE_TYPE_CLASSIC; @SuppressWarnings("WeakerAccess")
    static final int DEVICE_TYPE_LE; @SuppressWarnings("WeakerAccess")
    static final int DEVICE_TYPE_DUAL; @SuppressWarnings("WeakerAccess")
    static final int BOND_NONE; @SuppressWarnings("WeakerAccess")
    static final int BOND_BONDING; @SuppressWarnings("WeakerAccess")
    static final int BOND_BONDED; @SuppressWarnings("WeakerAccess")
    static final int STATE_DISCONNECTED; @SuppressWarnings("WeakerAccess")
    static final int STATE_CONNECTING; @SuppressWarnings("WeakerAccess")
    static final int STATE_CONNECTED; @SuppressWarnings("WeakerAccess")
    static final int STATE_DISCONNECTING; }