Example usage for android.bluetooth BluetoothGattCharacteristic getUuid

List of usage examples for android.bluetooth BluetoothGattCharacteristic getUuid

Introduction

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

Prototype

public UUID getUuid() 

Source Link

Document

Returns the UUID of this characteristic

Usage

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

@Override
public void onCharacteristicChanged(@NonNull BluetoothGatt gatt,
        @NonNull BluetoothGattCharacteristic characteristic) {
    if (gatt == this.gatt) {
        final UUID serviceId = characteristic.getService().getUuid();
        final NativeGattService gattService = services.get(serviceId);
        gattService.dispatchNotify(characteristic.getUuid(), characteristic.getValue());
    }/*w w  w .  jav a  2 s.  c  o m*/
}

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

public JSONObject asJSONObject(BluetoothGatt gatt) {

    JSONObject json = asJSONObject();/*from  ww w.  j av  a  2  s  .c  o m*/

    try {
        JSONArray servicesArray = new JSONArray();
        JSONArray characteristicsArray = new JSONArray();
        json.put("services", servicesArray);
        json.put("characteristics", characteristicsArray);

        if (connected && gatt != null) {
            for (BluetoothGattService service : gatt.getServices()) {
                servicesArray.put(UUIDHelper.uuidToString(service.getUuid()));

                for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                    JSONObject characteristicsJSON = new JSONObject();
                    characteristicsArray.put(characteristicsJSON);

                    characteristicsJSON.put("service", UUIDHelper.uuidToString(service.getUuid()));
                    characteristicsJSON.put("characteristic",
                            UUIDHelper.uuidToString(characteristic.getUuid()));
                    //characteristicsJSON.put("instanceId", characteristic.getInstanceId());

                    characteristicsJSON.put("properties", Helper.decodeProperties(characteristic));
                    // characteristicsJSON.put("propertiesValue", characteristic.getProperties());

                    if (characteristic.getPermissions() > 0) {
                        characteristicsJSON.put("permissions", Helper.decodePermissions(characteristic));
                        // characteristicsJSON.put("permissionsValue", characteristic.getPermissions());
                    }

                    JSONArray descriptorsArray = new JSONArray();

                    for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                        JSONObject descriptorJSON = new JSONObject();
                        descriptorJSON.put("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
                        descriptorJSON.put("value", descriptor.getValue()); // always blank

                        if (descriptor.getPermissions() > 0) {
                            descriptorJSON.put("permissions", Helper.decodePermissions(descriptor));
                            // descriptorJSON.put("permissionsValue", descriptor.getPermissions());
                        }
                        descriptorsArray.put(descriptorJSON);
                    }
                    if (descriptorsArray.length() > 0) {
                        characteristicsJSON.put("descriptors", descriptorsArray);
                    }
                }
            }
        }
    } catch (JSONException e) { // TODO better error handling
        e.printStackTrace();
    }

    return json;
}

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++;//from w w w . ja v a2s .c o m
        a.put(o);
    }
    callbackContext.success(a);
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support.java

@Override
public boolean onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
        int status) {
    UUID characteristicUUID = characteristic.getUuid();
    if (MiBand2Service.UUID_CHARACTERISTIC_AUTH.equals(characteristicUUID)) {
        LOG.info("KEY AES SEND");
        logMessageContent(characteristic.getValue());
        return true;
    }//from w w w .j  av  a2s  . com
    return false;
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support.java

@Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    super.onCharacteristicChanged(gatt, characteristic);

    UUID characteristicUUID = characteristic.getUuid();
    if (MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO.equals(characteristicUUID)) {
        handleBatteryInfo(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS);
        return true;
    } else if (MiBandService.UUID_CHARACTERISTIC_REALTIME_STEPS.equals(characteristicUUID)) {
        handleRealtimeSteps(characteristic.getValue());
        return true;
    } else if (GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT.equals(characteristicUUID)) {
        handleHeartrate(characteristic.getValue());
        return true;
    } else if (MiBand2Service.UUID_CHARACTERISTIC_AUTH.equals(characteristicUUID)) {
        LOG.info("AUTHENTICATION?? " + characteristicUUID);
        logMessageContent(characteristic.getValue());
        return true;
    } else if (MiBand2Service.UUID_CHARACTERISTIC_10_BUTTON.equals(characteristicUUID)) {
        handleButtonPressed(characteristic.getValue());
        return true;
    } else if (MiBand2Service.UUID_CHARACTERISTIC_7_REALTIME_STEPS.equals(characteristicUUID)) {
        handleRealtimeSteps(characteristic.getValue());
        return true;
    } else {//w  w  w.ja  va 2 s .co  m
        LOG.info("Unhandled characteristic changed: " + characteristicUUID);
        logMessageContent(characteristic.getValue());
    }
    return false;
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support.java

@Override
public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
        int status) {
    super.onCharacteristicRead(gatt, characteristic, status);

    UUID characteristicUUID = characteristic.getUuid();
    if (GattCharacteristic.UUID_CHARACTERISTIC_GAP_DEVICE_NAME.equals(characteristicUUID)) {
        handleDeviceName(characteristic.getValue(), status);
        return true;
    } else if (MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO.equals(characteristicUUID)) {
        handleBatteryInfo(characteristic.getValue(), status);
        return true;
    } else if (GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT.equals(characteristicUUID)) {
        logHeartrate(characteristic.getValue(), status);
        return true;
    } else if (MiBand2Service.UUID_CHARACTERISTIC_7_REALTIME_STEPS.equals(characteristicUUID)) {
        handleRealtimeSteps(characteristic.getValue());
        return true;
    } else if (MiBand2Service.UUID_CHARACTERISTIC_10_BUTTON.equals(characteristicUUID)) {
        handleButtonPressed(characteristic.getValue());
        return true;
    } else {//ww w. ja v  a 2  s . c o  m
        LOG.info("Unhandled characteristic read: " + characteristicUUID);
        logMessageContent(characteristic.getValue());
    }
    return false;
}

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,//from  w w w  . j ava 2  s .  c o 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);
}

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

/**
 * Method to get required characteristics from service
 *///  w w  w  .  j av  a2  s.  c om
void getGattData() {
    for (int position = 0; position < mExtraservice.size(); position++) {
        HashMap<String, BluetoothGattService> item = mExtraservice.get(position);
        BluetoothGattService bgs = item.get("UUID");
        if (bgs.getUuid().equals(UUIDDatabase.UUID_ACCELEROMETER_SERVICE)) {
            mAccservice = bgs;
        }
        List<BluetoothGattCharacteristic> gattCharacteristicsCurrent = bgs.getCharacteristics();
        for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristicsCurrent) {
            String uuidchara = gattCharacteristic.getUuid().toString();
            if (uuidchara.equalsIgnoreCase(GattAttributes.ACCELEROMETER_READING_X)) {
                mReadACCXCharacteristic = gattCharacteristic;
                mNotifyACCXCharacteristic = gattCharacteristic;

                prepareBroadcastDataRead(mReadACCXCharacteristic);
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.ACCELEROMETER_READING_Y)) {
                mReadACCYCharacteristic = gattCharacteristic;
                mNotifyACCYCharacteristic = gattCharacteristic;

            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.ACCELEROMETER_READING_Z)) {
                mReadACCZCharacteristic = gattCharacteristic;
                mNotifyACCZCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.BATTERY_LEVEL)) {
                mReadBATCharacteristic = gattCharacteristic;
                mNotifyBATCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.TEMPERATURE_READING)) {
                mReadSTEMPCharacteristic = gattCharacteristic;
                mNotifySTEMPCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.BAROMETER_READING)) {
                mReadSPRESSURECharacteristic = gattCharacteristic;
                mIndicateSPRESSURECharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.ALERT_LEVEL)) {
                mWriteAlertCharacteristic = gattCharacteristic;

            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.ACCELEROMETER_SENSOR_SCAN_INTERVAL)) {
                mReadACCSensorScanCharacteristic = gattCharacteristic;

            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.ACCELEROMETER_ANALOG_SENSOR)) {
                mReadACCSensorTypeCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.ACCELEROMETER_DATA_ACCUMULATION)) {
                mReadACCFilterConfigurationCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.TEMPERATURE_SENSOR_SCAN_INTERVAL)) {
                mReadSTEMPSensorScanCharacteristic = gattCharacteristic;

            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.TEMPERATURE_ANALOG_SENSOR)) {
                mReadSTEMPSensorTypeCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.BAROMETER_SENSOR_SCAN_INTERVAL)) {
                mReadSPRESSURESensorScanCharacteristic = gattCharacteristic;

            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.BAROMETER_DIGITAL_SENSOR)) {
                mReadSPRESSURESensorTypeCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.BAROMETER_DATA_ACCUMULATION)) {
                mReadSPRESSUREFilterConfigurationCharacteristic = gattCharacteristic;
            }
            if (uuidchara.equalsIgnoreCase(GattAttributes.BAROMETER_THRESHOLD_FOR_INDICATION)) {
                mReadSPRESSUREThresholdCharacteristic = gattCharacteristic;
            }
        }
    }
}

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

/**
 * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
 * asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
 * callback./*from   w  w w .  j  av a 2s.  co m*/
 *
 */
public boolean readCharacteristic(String address, String serviceUuid, String characteristicUuid) {
    Log.d(TAG, "readCharacteristic 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;
    }
    boolean result = bluetoothGatt.readCharacteristic(characteristic);
    Log.d(TAG, "Read charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}

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

public boolean writeRemoteCharacteristic(String address, String serviceUuid, String characteristicUuid,
        byte[] value) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);//from w  w  w .j a va 2  s  . co m
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    if (value == null) {
        Log.w(TAG, "value is empty");
        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;
    }

    characteristic.setValue(value);
    boolean result = bluetoothGatt.writeCharacteristic(characteristic);
    Log.d(TAG, "Write charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}