Example usage for android.bluetooth BluetoothGattCharacteristic getProperties

List of usage examples for android.bluetooth BluetoothGattCharacteristic getProperties

Introduction

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

Prototype

public int getProperties() 

Source Link

Document

Returns the properties of this characteristic.

Usage

From source file:com.evothings.BLE.java

private void characteristics(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    JSONArray a = new JSONArray();
    for (BluetoothGattCharacteristic c : gh.mServices.get(args.getInt(1)).getCharacteristics()) {
        if (gh.mCharacteristics == null)
            gh.mCharacteristics = new HashMap<Integer, BluetoothGattCharacteristic>();
        Object res = gh.mCharacteristics.put(gh.mNextHandle, c);
        assert (res == null);

        JSONObject o = new JSONObject();
        o.put("handle", gh.mNextHandle);
        o.put("uuid", c.getUuid().toString());
        o.put("permissions", c.getPermissions());
        o.put("properties", c.getProperties());
        o.put("writeType", c.getWriteType());

        gh.mNextHandle++;// ww w .ja v a 2  s. c  o  m
        a.put(o);
    }
    callbackContext.success(a);
}

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;//from   w ww  . j  av a2s .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:com.cypress.cysmart.BLEServiceFragments.SensorHubService.java

/**
 * Preparing Broadcast receiver to broadcast read characteristics
 *
 * @param gattCharacteristic/*from w w  w.  j  a va 2s .  c  o m*/
 */
void prepareBroadcastDataRead(BluetoothGattCharacteristic gattCharacteristic) {
    final int charaProp = gattCharacteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        BluetoothLeService.readCharacteristic(gattCharacteristic);
    }
}

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

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

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

    }

}

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

/**
 * Preparing Broadcast receiver to broadcast indicate characteristics
 *
 * @param gattCharacteristic/*from www.  ja v a  2  s  .c o  m*/
 */
void prepareBroadcastDataIndicate(BluetoothGattCharacteristic gattCharacteristic) {
    final int charaProp = gattCharacteristic.getProperties();

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
        BluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);

    }

}

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

/**
 * Enables or disables notification on a give characteristic.
 *
 * @param enabled If true, enable notification.  False otherwise.
 *///from www .  j  a  v  a  2s .  com
public boolean setCharacteristicNotification(String address, String serviceUuid, String characteristicUuid,
        boolean enabled) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }
    BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        Log.w(TAG, "Service not found.");
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid));
    if (characteristic == null) {
        Log.w(TAG, "characteristic not found.");
        return false;
    }

    bluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    final int charaProp = characteristic.getProperties();
    if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor != null) {
            Log.d(TAG, ">>>> ENABLE_NOTIFICATION_VALUE : " + characteristic.getUuid().toString());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);

            return true;
        } else {
            return false;
        }
    } else if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor != null) {
            Log.d(TAG, ">>>> ENABLE_INDICATION_VALUE : " + characteristic.getUuid().toString());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);

            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

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  w w w . ja v a  2  s. c om
        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);
                }
            });
        }
    });
}

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

private void broadcastUpdate(final String address, final String action,
        final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    intent.putExtra(EXTRA_DATA_SERVICE_UUID, characteristic.getService().getUuid().toString());
    intent.putExtra(EXTRA_DATA_CHARACTERISTIC_UUID, characteristic.getUuid().toString());

    String str = "";
    byte[] values = characteristic.getValue();

    Log.d(TAG,//  w w w .j  ava  2s.  co m
            "onCharacteristicChanged: address : " + address + ", uuid:" + characteristic.getUuid().toString());

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, values);
    } else if (UUID_WEIGHT_MEASUREMENT.equals(characteristic.getUuid())) { // for weight scale
        int flag = values[0] & 0xff;
        Log.w(TAG, String.format("Measurement data received flag = %02x", flag));
        /**
         *  ? reserved field ? 2? ? ??.
         */
        if (address != null && address.startsWith(GattAttributes.XIAOMI_MAC_ADDRESS_FILTER)) {
            if (values == null || values.length <= 0 || (values[0] & 0xf0) != 0x20) {
                Log.d(TAG, "ignore ... flag 4nibble 0x20 is not ... ");
                return;
            }
        }

        ArrayList<WeightMeasurement> measurements = WeightMeasurement.parseWeightMeasurement(address,
                characteristic.getUuid().toString(), values);

        intent.putParcelableArrayListExtra(EXTRA_DATA, measurements);
    } else if (UUID_GLUCOSE_MEASUREMENT.equals(characteristic.getUuid())) {
        GlucoseMeasurement measurement = GlucoseMeasurement.parseGlucoseMeasurement(values);

        intent.putExtra(EXTRA_DATA, measurement);
    } else if (UUID_GLUCOSE_MEASUREMENT_CONTEXT.equals(characteristic.getUuid())) {

    } else if (UUID_RECORD_ACCESS_CONTROL_POINT.equals(characteristic.getUuid())) {
        RecordAccessControlPoint recordAccessControlPoint = RecordAccessControlPoint
                .parseRecordAccessControlPoint(values);
        intent.putExtra(EXTRA_DATA, recordAccessControlPoint);
    } else if (UUID.fromString(GattAttributes.CURRENT_TIME).equals(characteristic.getUuid())) {
        if (values != null && values.length > 0) {
            intent.putExtra(EXTRA_DATA, values);
        }
        //intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else {
        // For all other profiles, writes the data formatted in HEX.
        if (values != null && values.length > 0) {
            intent.putExtra(EXTRA_DATA, values);
        }
    }
    sendBroadcast(intent);
}