Example usage for android.bluetooth BluetoothGattCharacteristic PROPERTY_NOTIFY

List of usage examples for android.bluetooth BluetoothGattCharacteristic PROPERTY_NOTIFY

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattCharacteristic PROPERTY_NOTIFY.

Prototype

int PROPERTY_NOTIFY

To view the source code for android.bluetooth BluetoothGattCharacteristic PROPERTY_NOTIFY.

Click Source Link

Document

Characteristic property: Characteristic supports notification

Usage

From source file:com.cypress.cysmart.BLEServiceFragments.CapsenseService.java

/**
 * Preparing Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic//  ww  w  . j a  v a 2 s.c  o m
 */
void prepareBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    final BluetoothGattCharacteristic characteristic = gattCharacteristic;
    final int charaProp = characteristic.getProperties();

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        BluetoothLeService.setCharacteristicNotification(characteristic, true);
    }

}

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  ww w  . j ava2 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.megster.cordova.ble.central.Peripheral.java

private void registerNotifyCallback(CallbackContext callbackContext, UUID serviceUUID,
        UUID characteristicUUID) {

    boolean success = false;

    if (gatt == null) {
        callbackContext.error("BluetoothGatt is null");
        return;/* w  w w. j  a  va 2 s  . c o  m*/
    }

    BluetoothGattService service = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
    String key = generateHashKey(serviceUUID, characteristic);

    if (characteristic != null) {

        notificationCallbacks.put(key, callbackContext);

        if (gatt.setCharacteristicNotification(characteristic, true)) {

            // Why doesn't setCharacteristicNotification write the descriptor?
            BluetoothGattDescriptor descriptor = characteristic
                    .getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID);
            if (descriptor != null) {

                // prefer notify over indicate
                if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                } else if ((characteristic.getProperties()
                        & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                } else {
                    LOG.w(TAG, "Characteristic " + characteristicUUID
                            + " does not have NOTIFY or INDICATE property set");
                }

                if (gatt.writeDescriptor(descriptor)) {
                    success = true;
                } else {
                    callbackContext.error(
                            "Failed to set client characteristic notification for " + characteristicUUID);
                }

            } else {
                callbackContext.error("Set notification failed for " + characteristicUUID);
            }

        } else {
            callbackContext.error("Failed to register notification for " + characteristicUUID);
        }

    } else {
        callbackContext.error("Characteristic " + characteristicUUID + " not found");
    }

    if (!success) {
        commandCompleted();
    }
}

From source file:ti.android.ble.devicemonitor.ServiceView.java

void displayNotif(byte[] value, byte len) {
    if (mChar != null) {
        if ((mProperty & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            // Only show data if selected characteristic has notifications
            displayData(value, len);/*from   ww  w  .  j  a va  2s .com*/
        }
    }
}

From source file:com.cypress.cysmart.BLEServiceFragments.BatteryInformationService.java

/**
 * Preparing Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic/*from w  ww.  ja  v a  2  s  .com*/
 */
void prepareBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    Logger.i("Notify called");
    final int charaProp = gattCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        BluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);

    }

}

From source file:com.cypress.cysmart.BLEServiceFragments.BatteryInformationService.java

/**
 * Stopping Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic// w  w  w .ja va2s .  c o m
 */
void stopBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    Logger.i("Notify stopped");
    final int charaProp = gattCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        if (mNotifyCharacteristic != null) {
            BluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
        }

    }

}

From source file:ti.android.ble.devicemonitor.ServiceView.java

private String getPropertyDescription(int prop) {
    String str = new String();

    if ((prop & BluetoothGattCharacteristic.PROPERTY_READ) > 0)
        str += "R";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0)
        str += "W";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)
        str += "N";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0)
        str += "I";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0)
        str += "*";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_BROADCAST) > 0)
        str += "B";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS) > 0)
        str += "E";

    if ((prop & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) > 0)
        str += "S";

    return str;//from w  w  w. j  a  v a  2s .  c  om
}

From source file:com.cypress.cysmart.BLEServiceFragments.HeartRateService.java

/**
 * Preparing Broadcast receiver to broadcast notify characteristics
 *
 * @param gattCharacteristic/*from  w  w w  . j  av a 2s .  com*/
 */
void prepareBroadcastDataNotify(BluetoothGattCharacteristic gattCharacteristic) {
    final int charaProp = gattCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        mNotifyCharacteristic = gattCharacteristic;
        BluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);
    }
}

From source file:com.megster.cordova.ble.central.Peripheral.java

private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service,
        UUID characteristicUUID) {
    BluetoothGattCharacteristic characteristic = null;

    // Check for Notify first
    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;//from w  w  w  .  j a v a 2s  .com
            break;
        }
    }

    if (characteristic != null)
        return characteristic;

    // If there wasn't Notify Characteristic, check for Indicate
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;
            break;
        }
    }

    // As a last resort, try and find ANY characteristic with this UUID, even if it doesn't have the correct properties
    if (characteristic == null) {
        characteristic = service.getCharacteristic(characteristicUUID);
    }

    return characteristic;
}

From source file:edu.stanford.cs.sing.helena.DeviceControlActivity.java

private void checkServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null)
        return;//from   www  .j  a v a 2s .  com
    String uuid;
    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        uuid = gattService.getUuid().toString();
        //only interested in helena service
        if (uuid.equals(HelenaGattAttributes.HELENA_SERVICE)) {
            mHelenaService = gattService;
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            // Loops through available Characteristics.
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                //only interested in this characteristic
                if (gattCharacteristic.getUuid().toString().equals(HelenaGattAttributes.UUID_LISTED_DEVICE)) {
                    mNotifyCharacteristic = gattCharacteristic;
                    final int charaProp = gattCharacteristic.getProperties();
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        //Initiate notifications
                        mNotifyCharacteristic = gattCharacteristic;
                        mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
                    }

                }
            }

        } else {
            Log.d(TAG, "Skipping other services");
        }
    }
}