Example usage for android.bluetooth BluetoothGattCharacteristic getValue

List of usage examples for android.bluetooth BluetoothGattCharacteristic getValue

Introduction

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

Prototype

public byte[] getValue() 

Source Link

Document

Get the stored value for this characteristic.

Usage

From source file:com.shenqu.jlplayer.nRFUARTv2.UartService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (TX_CHAR_UUID.equals(characteristic.getUuid())) {
        // Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    }//w  ww. j  a v a2 s.co  m
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.cardiograph.service.UartService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (TX_CHAR_UUID.equals(characteristic.getUuid())) {

        // Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else {//from ww w  .j  a v a2  s .  com

    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.example.alexanderbolinsky.ble_uart_remote.UartService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (TX_CHAR_UUID.equals(characteristic.getUuid())) {

        // Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else {//from   w  w w  . jav  a  2 s  .c  om

    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

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

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    super.onCharacteristicChanged(gatt, characteristic);
    LOG.d(TAG, "onCharacteristicChanged " + characteristic);

    CallbackContext callback = notificationCallbacks.get(generateHashKey(characteristic));

    if (callback != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, characteristic.getValue());
        result.setKeepCallback(true);/*  w  w w .  j  av a2  s  .  c o  m*/
        callback.sendPluginResult(result);
    }
}

From source file:com.example.UART.UartService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (TX_CHAR_UUID.equals(characteristic.getUuid())) {
        // Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else {//from  w w w  .  java 2s. co  m

    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.example.kevin.health.ble.UartService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (HR_CHAR_UUID.equals(characteristic.getUuid())) {

        // Log.d(TAG, String.format("Received HR: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else if (HR_CHAR_UUID.equals(characteristic.getUuid())) {
        // Log.d(TAG, String.format("Received Tx: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else if (TX_CHAR_UUID.equals(characteristic.getUuid())) {
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else {//  w ww . j a v a2s.  c  om

    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.sdingba.su.alphabet_demotest.view.lanya.UartService.java

/**
 * ??/*from   w ww .  j a  v a 2  s .c  om*/
 * @param action
 */
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (TX_CHAR_UUID.equals(characteristic.getUuid())) {

        // Log.d(TAG, String.format("Received TX: %d",characteristic.getValue() ));
        intent.putExtra(EXTRA_DATA, characteristic.getValue());
    } else {

    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.cqupt.pedometer.main.UartService.java

/**
 * ???/*ww w .  j  a va2  s. c  om*/
 *
 * @param action
 * @param characteristic
 */
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // 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 (TX_CHAR_UUID.equals(characteristic.getUuid())) {

        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for (byte byteChar : data)
                stringBuilder.append(String.format("%X ", byteChar));
            String a = stringBuilder.toString();
            a = a.replaceAll("[^0-9a-zA-Z]", "");
            // String.subSequence(beginIndex, endIndex?)
            String b = (String) a.subSequence(0, 1);
            // ?
            String c = a.substring(1, a.length());
            // Integer.parseInt(s, radix) radix10101616
            int i = Integer.parseInt(c, 16);
            intent.putExtra(EXTRA_DATA, i + "");
            intent.putExtra(SAFE_DATA, b);
        }

    } else if (RX_CHAR_UUID.equals(characteristic.getUuid())) {
        intent.putExtra(CHAR1_DATA,
                "" + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));

    } else if (Battery_Level_UUID.equals(characteristic.getUuid())) {
        intent.putExtra(EXTRA_DATA,
                "" + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

private void createGATT(final BluetoothDevice device) {
    bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() {
        @Override// www  .ja  v  a2s. c o  m
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            isConnected = BluetoothProfile.STATE_CONNECTED == newState;
            if (isConnected) {
                Log.i(TAG, "GATT connected.");
                startForeground(442, getNotification(device));
                gatt.discoverServices();
            } else {
                Log.i(TAG, "GATT disconnected.");
                NotificationService_.intent(BluetoothService.this).stop();
                notificationManager.notify(442, getNotification(device));
                gatt.connect();
            }

            final Intent connectionStateChanged = new Intent(CONNECTION_STATE_CHANGED);
            connectionStateChanged.putExtra(CONNECTION_STATE, isConnected);
            sendBroadcast(connectionStateChanged);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.i(TAG, "Services discovered.");
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            discoverCharacteristics(gatt);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            Log.i(TAG, "Characteristic written: " + status);

            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final byte command = characteristic.getValue()[0];
            switch (command) {
            case WRITE_TIME:
                Log.i(TAG, "Time written.");
                final BluetoothGattCharacteristic batteryCharacteristic = readableCharacteristics
                        .get(Characteristic.BATTERY.getUuid());
                gatt.setCharacteristicNotification(batteryCharacteristic, true);
                for (BluetoothGattDescriptor descriptor : batteryCharacteristic.getDescriptors()) {
                    if (descriptor.getUuid().toString().startsWith("00002904")) {
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        gatt.writeDescriptor(descriptor);
                    }
                }
                break;
            case WRITE_NOTIFICATION:
                Log.i(TAG, "Notification sent.");
                if (notificationsQueue.isEmpty()) {
                    Log.i(TAG, "Reading characteristics...");
                    readNextCharacteristics(gatt);
                } else {
                    Log.i(TAG, "writing next notification...");
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }
                break;
            default:
                Log.w(TAG, "No such ALERT IN command: " + command);
                break;
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            readCharacteristic(gatt, Characteristic.MANUFACTURER);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                final BluetoothGattCharacteristic gattCharacteristic, int status) {
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            switch (characteristic) {
            case MANUFACTURER:
                manufacturerInfo.manufacturer = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.FW_REVISION);
                break;
            case FW_REVISION:
                manufacturerInfo.firmwareRevision = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.MODE);
                break;
            default:
                Log.v(TAG, "Characteristic read: " + characteristic.name());
                if (characteristic == Characteristic.MODE) {
                    final Mode newMode = Mode.bySymbol(gattCharacteristic.getValue()[0]);
                    if (mode != newMode) {
                        onModeChanged(newMode);
                    }
                } else {
                    onBluetoothDataReceived(characteristic, gattCharacteristic.getValue());
                }

                if (notificationsQueue.isEmpty()) {
                    readNextCharacteristics(gatt);
                } else {
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }

                break;
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                BluetoothGattCharacteristic gattCharacteristic) {
            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            Log.d(TAG, "Characteristic changed: " + characteristic);

            if (characteristic == Characteristic.BATTERY) {
                onBluetoothDataReceived(Characteristic.BATTERY, gattCharacteristic.getValue());
            }
        }
    });
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.HPlusSupport.java

@Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    if (super.onCharacteristicChanged(gatt, characteristic)) {
        return true;
    }//from w  w  w .j a  v a  2s  . c o  m

    UUID characteristicUUID = characteristic.getUuid();
    byte[] data = characteristic.getValue();
    if (data.length == 0)
        return true;

    switch (data[0]) {
    case HPlusConstants.DATA_VERSION:
        return syncHelper.processVersion(data);

    case HPlusConstants.DATA_STATS:
        return syncHelper.processRealtimeStats(data);

    case HPlusConstants.DATA_SLEEP:
        return syncHelper.processIncomingSleepData(data);

    case HPlusConstants.DATA_STEPS:
        return syncHelper.processDaySummary(data);

    case HPlusConstants.DATA_DAY_SUMMARY:
    case HPlusConstants.DATA_DAY_SUMMARY_ALT:
        return syncHelper.processIncomingDaySlotData(data);

    default:
        LOG.debug("Unhandled characteristic changed: " + characteristicUUID);
        return true;
    }
}