Example usage for android.bluetooth BluetoothGatt disconnect

List of usage examples for android.bluetooth BluetoothGatt disconnect

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGatt disconnect.

Prototype

public void disconnect() 

Source Link

Document

Disconnects an established connection, or cancels a connection attempt currently in progress.

Usage

From source file:ble.AndroidBle.java

@Override
public void disconnect(String address) {
    if (mBluetoothGatts.containsKey(address)) {
        BluetoothGatt gatt = mBluetoothGatts.remove(address);
        if (gatt != null) {
            gatt.disconnect();
            gatt.close();//from w  w  w .j  av  a2s.c  om
        }
    }
}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

public void disconnectDevice(String address) {
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    if (btAdapter == null || address == null || bluetoothGatt == null) {
        return;//from   w  w w. j av a2  s .c  o  m
    }
    BluetoothDevice device = btAdapter.getRemoteDevice(address);
    int bleState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
    if (bleState != BluetoothProfile.STATE_DISCONNECTED && bleState != BluetoothProfile.STATE_DISCONNECTING) {
        bluetoothGatt.disconnect();
    } else {
        bluetoothGatt.close();
        addressToGattClient.remove(address);
        sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null);
    }
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * Disconnects an existing connection or cancel a pending connection. The disconnection result
 * is reported asynchronously through the
 * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
 * callback./*from  w ww . j  ava  2s.c  o m*/
 */
public void disconnect(String address) {
    if (StringUtils.isEmptyString(address)) {
        Log.w(TAG, "Unknown address");
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    Log.d(TAG, "Disconnect connection!!");
    bluetoothGatt.disconnect();
}

From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java

/**
 * Disconnects from the device. This is SYNCHRONOUS method and waits until the callback returns new state. Terminates immediately if device is already disconnected. Do not call this method
 * directly, use {@link #terminateConnection(BluetoothGatt, int)} instead.
 * //from  w w  w .j a  va  2  s .  c o  m
 * @param gatt
 *            the GATT device that has to be disconnected
 */
private void disconnect(final BluetoothGatt gatt) {
    if (mConnectionState == STATE_DISCONNECTED)
        return;

    mConnectionState = STATE_DISCONNECTING;

    logi("Disconnecting from the device...");
    gatt.disconnect();

    // We have to wait until device gets disconnected or an error occur
    waitUntilDisconnected();
}

From source file:is.hello.buruberi.bluetooth.stacks.android.NativeGattPeripheral.java

@NonNull
@Override//  w  w w.ja v a  2  s.  c om
@RequiresPermission(Manifest.permission.BLUETOOTH)
public Observable<GattPeripheral> disconnect() {
    return createObservable(new Observable.OnSubscribe<GattPeripheral>() {
        @Override
        public void call(final Subscriber<? super GattPeripheral> subscriber) {
            final int connectionStatus = getConnectionStatus();
            if (connectionStatus == STATUS_DISCONNECTED || connectionStatus == STATUS_DISCONNECTING
                    || gatt == null) {
                subscriber.onNext(NativeGattPeripheral.this);
                subscriber.onCompleted();
                return;
            } else if (connectionStatus == STATUS_CONNECTING) {
                subscriber.onError(new ConnectionStateException("Peripheral is connecting"));
                return;
            }

            final GattDispatcher.ConnectionListener listener = new GattDispatcher.ConnectionListener() {
                @Override
                boolean onDisconnected(@NonNull BluetoothGatt gatt, int status) {
                    logger.info(LOG_TAG, "Disconnected " + NativeGattPeripheral.this.toString());

                    stopObservingBluetoothState();
                    subscriber.onNext(NativeGattPeripheral.this);
                    subscriber.onCompleted();

                    return false;
                }

                @Override
                boolean onError(@NonNull BluetoothGatt gatt, int status, int state) {
                    logger.info(LOG_TAG, "Could not disconnect " + NativeGattPeripheral.this.toString() + "; "
                            + GattException.statusToString(status));

                    subscriber.onError(new GattException(status, Operation.DISCONNECT));

                    return false;
                }
            };
            gattDispatcher.addConnectionListener(listener);

            logger.info(LOG_TAG, "Disconnecting " + NativeGattPeripheral.this.toString());

            gatt.disconnect();
        }
    });
}

From source file:net.emilymaier.movebot.HeartFragment.java

@Override
@SuppressWarnings("deprecation")
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    Log.d("HeartFragment", "Bluetooth LE device found");
    BluetoothGatt bluetoothGatt = device.connectGatt(act, false, new BluetoothGattCallback() {
        @Override//from  ww w.j  a  v a2 s .  c o m
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.d("HeartFragment", "Connected to LE device");
                gatt.discoverServices();
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattCharacteristic characteristic = null;
                for (BluetoothGattService service : gatt.getServices()) {
                    characteristic = service
                            .getCharacteristic(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"));
                    if (characteristic != null) {
                        break;
                    }
                }
                if (characteristic != null) {
                    Log.d("HeartFragment", "Found device with HRM characteristic");
                    HeartDevice device = new HeartDevice();
                    device.bluetoothGatt = gatt;
                    device.characteristic = characteristic;
                    for (HeartDevice testDevice : heartDevices) {
                        if (testDevice.bluetoothGatt.getDevice().getAddress()
                                .equals(device.bluetoothGatt.getDevice().getAddress())) {
                            heartDevices.remove(testDevice);
                        }
                    }
                    heartDevices.add(device);
                } else {
                    Log.d("HeartFragment", "Device does not have HRM characteristic");
                    gatt.disconnect();
                    gatt.close();
                }
            } else {
                Log.w("HeartFragment", "Failed to discover device services");
                gatt.disconnect();
                gatt.close();
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            int flag = characteristic.getProperties();
            int format = -1;
            if ((flag & 0x01) != 0) {
                format = BluetoothGattCharacteristic.FORMAT_UINT16;
            } else {
                format = BluetoothGattCharacteristic.FORMAT_UINT8;
            }
            final int heartRate = characteristic.getIntValue(format, 1);
            act.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    act.updateHeart(heartRate);
                }
            });
        }
    });
}