Example usage for android.bluetooth BluetoothGattDescriptor DISABLE_NOTIFICATION_VALUE

List of usage examples for android.bluetooth BluetoothGattDescriptor DISABLE_NOTIFICATION_VALUE

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattDescriptor DISABLE_NOTIFICATION_VALUE.

Prototype

null DISABLE_NOTIFICATION_VALUE

To view the source code for android.bluetooth BluetoothGattDescriptor DISABLE_NOTIFICATION_VALUE.

Click Source Link

Document

Value used to disable notifications or indicatinos

Usage

From source file:Main.java

public static boolean disableNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    BluetoothGattDescriptor descriptor = configDescriptor(characteristic);
    descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    return gatt.writeDescriptor(descriptor) && gatt.setCharacteristicNotification(characteristic, false);
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void setNotification(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "setNotification");
    String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
    if (connectedDevice.get(deviceAddress) == null) {
        Tools.sendErrorMsg(callbackContext);
        return;/*from  w  w  w.  j ava  2 s  .  c  o  m*/
    }
    int serviceIndex = Integer.parseInt(Tools.getData(json, Tools.SERVICE_INDEX));
    int characteristicIndex = Integer.parseInt(Tools.getData(json, Tools.CHARACTERISTIC_INDEX));
    String enable = Tools.getData(json, Tools.ENABLE);
    BluetoothGattCharacteristic characteristic = deviceServices.get(deviceAddress).get(serviceIndex)
            .getCharacteristics().get(characteristicIndex);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Tools.NOTIFICATION_UUID);
    if (enable.equals("true")) {
        setNotificationCC.put(characteristic, callbackContext);
        if (Tools.lookup(characteristic.getProperties(), BluetoothGattCharacteristic.PROPERTY_NOTIFY) != null) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        } else {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        }
        mBluetoothGatts.get(deviceAddress).writeDescriptor(descriptor);
        mBluetoothGatts.get(deviceAddress).setCharacteristicNotification(characteristic, true);
        recordServiceIndex.put(characteristic, serviceIndex);
        recordCharacteristicIndex.put(characteristic, characteristicIndex);

    } else {
        descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        mBluetoothGatts.get(deviceAddress).writeDescriptor(descriptor);
        mBluetoothGatts.get(deviceAddress).setCharacteristicNotification(characteristic, false);
        Tools.sendSuccessMsg(callbackContext);
        setNotificationCC.remove(characteristic);
        recordServiceIndex.remove(characteristic);
        recordCharacteristicIndex.remove(characteristic);
    }
}

From source file:com.cerevo.blueninja.blueninja_ble_example.MainActivity.java

/**
 * Characteristic?Notification?//  www. j  a  v  a  2s .com
 */
private void disableBleNotification() {
    for (int i = 0; i < UUIDS_CHARACTERISTIC.length; i++) {
        BluetoothGattDescriptor desc = mCharacteristics[i]
                .getDescriptor(UUID.fromString(UUID_CLIENT_CHARACTERISTIC_CONFIG));
        desc.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        mGatt.writeDescriptor(desc);
        if (!mGatt.setCharacteristicNotification(mCharacteristics[i], false)) {
            setStatus(AppState.BLE_NOTIF_REGISTER_FAILED);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    setStatus(AppState.BLE_NOTIF_REGISTERD);
}

From source file:com.huiwu.bluetooth.BluetoothService.java

/**
 * Enable TXNotification/*w w w. ja v  a 2  s .com*/
 *
 * @return
 */
public void enableTXNotification(boolean enable) {
    /*
    if (mBluetoothGatt == null) {
      showMessage("mBluetoothGatt null" + mBluetoothGatt);
      broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
      return;
    }
      */
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    if (TxChar == null) {
        showMessage("Tx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);

    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    if (enable) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    } else {
        descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    }
    mBluetoothGatt.writeDescriptor(descriptor);

}

From source file:ble.AndroidBle.java

@Override
public boolean characteristicNotification(String address, BleGattCharacteristic characteristic) {
    BleRequest request = mService.getCurrentRequest();
    BluetoothGatt gatt = mBluetoothGatts.get(address);
    if (gatt == null || characteristic == null) {
        return false;
    }//from   w w w .j  av  a2s.  c o  m

    boolean enable = true;
    if (request.type == RequestType.CHARACTERISTIC_STOP_NOTIFICATION) {
        enable = false;
    }
    BluetoothGattCharacteristic c = characteristic.getGattCharacteristicA();
    if (!gatt.setCharacteristicNotification(c, enable)) {
        return false;
    }

    BluetoothGattDescriptor descriptor = c.getDescriptor(BleService.DESC_CCC);
    if (descriptor == null) {
        return false;
    }

    byte[] val_set = null;
    if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION) {
        val_set = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
    } else if (request.type == RequestType.CHARACTERISTIC_INDICATION) {
        val_set = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
    } else {
        val_set = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
    }
    if (!descriptor.setValue(val_set)) {
        return false;
    }

    return gatt.writeDescriptor(descriptor);
}

From source file:com.ec.android.module.bluetooth40.BluetoothLeService.java

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;//  www .  j  a  va 2  s.  co m
    }
    if (enabled == true) {
        mBluetoothGatt.setCharacteristicNotification(characteristic, true);
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
        if (descriptor != null) {
            //??
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    } else {
        mBluetoothGatt.setCharacteristicNotification(characteristic, false);
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
        if (descriptor != null) {
            //??
            descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }

}

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

public BleFlow disableNotification() {
    actions.add(Action.DISABLE_NOTIF);

    lookupDescriptor(BLE_CLIENT_CONFIG_CHARACTERISTIC);

    actions.add(Action.WRITE_DESC);
    values.add(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    return this;
}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

public int sendBlockingNotifySetting(bleRequest request) {
    request.status = bleRequestStatus.processing;
    int timeout = 0;
    if (request.characteristic == null) {
        return -1;
    }/*  w  w  w .  j ava  2 s. com*/
    if (!checkGatt())
        return -2;

    if (mGatt.setCharacteristicNotification(request.characteristic, request.notifyenable)) {

        BluetoothGattDescriptor clientConfig = request.characteristic
                .getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG);
        if (clientConfig != null) {

            if (request.notifyenable) {
                // Log.i(TAG, "Enable notification: " +
                // characteristic.getUuid().toString());
                clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            } else {
                // Log.i(TAG, "Disable notification: " +
                // characteristic.getUuid().toString());
                clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            }
            mGatt.writeDescriptor(clientConfig);
            // Log.i(TAG, "writeDescriptor: " +
            // characteristic.getUuid().toString());
            this.blocking = true; // Set read to be blocking
            while (this.blocking) {
                timeout++;
                waitIdle(1);
                if (timeout > GATT_TIMEOUT) {
                    this.blocking = false;
                    request.status = bleRequestStatus.timeout;
                    return -1;
                } //Read failed TODO: Fix this to follow connection interval !
            }
            request.status = bleRequestStatus.done;
            return lastGattStatus;
        }
    }
    return -3; // Set notification to android was wrong ...
}

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

/**
 * Enables or disables the notifications for given characteristic. This method is SYNCHRONOUS and wait until the
 * {@link BluetoothGattCallback#onDescriptorWrite(BluetoothGatt, BluetoothGattDescriptor, int)} will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If
 * connection state will change, or an error will occur, an exception will be thrown.
 * /*from   ww  w .  ja  v  a  2s  . co m*/
 * @param gatt
 *            the GATT device
 * @param characteristic
 *            the characteristic to enable or disable notifications for
 * @param enable
 *            <code>true</code> to enable notifications, <code>false</code> to disable them
 * @throws DfuException
 * @throws UploadAbortedException
 */
private void setCharacteristicNotification(final BluetoothGatt gatt,
        final BluetoothGattCharacteristic characteristic, final boolean enable)
        throws DeviceDisconnectedException, DfuException, UploadAbortedException {
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState);
    mErrorState = 0;

    if (mNotificationsEnabled == enable)
        return;

    logi((enable ? "Enabling " : "Disabling") + " notifications...");

    // enable notifications locally
    gatt.setCharacteristicNotification(characteristic, enable);

    // enable notifications on the device
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
            : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(descriptor);

    // We have to wait until device gets disconnected or an error occur
    try {
        synchronized (mLock) {
            while ((mNotificationsEnabled != enable && mConnectionState == STATE_CONNECTED_AND_READY
                    && mErrorState == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }
    if (mAborted)
        throw new UploadAbortedException();
    if (mErrorState != 0)
        throw new DfuException("Unable to set notifications state", mErrorState);
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState);
}