Example usage for android.bluetooth BluetoothGattCharacteristic FORMAT_UINT8

List of usage examples for android.bluetooth BluetoothGattCharacteristic FORMAT_UINT8

Introduction

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

Prototype

int FORMAT_UINT8

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

Click Source Link

Document

Characteristic value format type uint8

Usage

From source file:Main.java

/**
 * Returns the Alert level information from the characteristics
 * /*w w w  .j a v a 2  s. c o m*/
 * @param characteristics
 * @return {@link String}
 */
@SuppressLint("NewApi")
public static String getAlertLevel(BluetoothGattCharacteristic characteristics) {
    int alert_level = characteristics.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
    return String.valueOf(alert_level);
}

From source file:Main.java

/**
 * Returns the battery level information from the characteristics
 * /*from  ww w  . j  av a  2  s  .  com*/
 * @param characteristics
 * @return {@link String}
 */
@SuppressLint("NewApi")
public static String getBatteryLevel(BluetoothGattCharacteristic characteristics) {
    int battery_level = characteristics.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
    return String.valueOf(battery_level);
}

From source file:com.example.android.bluetoothlegatt.BluetoothLeService.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 (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 {/*ww w.  j a v a 2s .  c  o m*/
            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, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            //// print data received.
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for (byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            //Log.i(TAG, "windsome1:"+ stringBuilder.toString());
            //Log.i(TAG, "windsome2:"+ new String(data));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());

            //// write to file.
            long currTime = System.currentTimeMillis();
            /*if ((currTime - cacheTime) > 500) {
            Log.i(TAG, "write cache to file! currTime="+currTime+", cacheTime="+cacheTime+", interval="+(currTime - cacheTime));
            boolean isSame = writeCacheToFile2 ();
            cacheData2.clear();
            intent.putExtra(EXTRA_DATA_SAME, isSame);
            }*/
            cacheData2.add(data);
            cacheTime = currTime;
            mToastHandler.removeCallbacks(mRunnable);
            mToastHandler.postDelayed(mRunnable, 300);

        }
    }
    sendBroadcast(intent);
}

From source file:com.chenls.smartlock.UartService.java

/**
 * ???/*from  w  ww.  j a va  2  s .  com*/
 *
 * @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())) {
        intent.putExtra(EXTRA_DATA,
                "" + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));

    } 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.cqupt.sensor_ble.activity.UartService.java

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

    if (TX_CHAR_UUID.equals(characteristic.getUuid())) {
        final byte[] byte_data = characteristic.getValue();
        if (byte_data != null && byte_data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(byte_data.length);
            for (byte byteChar : byte_data) {
                stringBuilder.append(String.format("%X", byteChar));
            }//w ww . j a  v a2s  .c  om
            temp.append(stringBuilder.toString()).append(" ");
            if ("A".equals(stringBuilder.toString())) { //?
                String string = temp.toString();
                if (string.length() == 34) { //??
                    String t = String.valueOf(string.charAt(7)) + String.valueOf(string.charAt(10)); //??
                    String h = String.valueOf(string.charAt(25)) + String.valueOf(string.charAt(28));//??
                    int[] int_array = new int[2];
                    int_array[0] = Integer.parseInt(h);
                    int_array[1] = Integer.parseInt(t);
                    intent.putExtra(EXTRA_DATA, int_array);
                }
                temp = null;
                temp = new StringBuilder(38);
            }
        }
    } 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:net.kenevans.android.blecardiacmonitor.BCMBleService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    Date now = new Date();
    long date = now.getTime();
    // // DEBUG//ww w . ja v a  2  s.  c om
    // Set this to "" to not get the date in the return value
    // String dateStr = " @ " + millisecTimeFormater.format(now);
    String dateStr = "";
    final Intent intent = new Intent(action);
    intent.putExtra(EXTRA_UUID, characteristic.getUuid().toString());
    intent.putExtra(EXTRA_DATE, date);

    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        HeartRateValues values = new HeartRateValues(characteristic, date);
        mLastHr = values.getHr();
        mLastRr = values.getRr();
        mLastHrDate = date;
        // // DEBUG
        // Log.d(TAG, String.format("Received heart rate measurement: %d",
        // mLastHr));
        if (mDbAdapter != null) {
            mDbAdapter.createData(mLastHrDate, mSessionStartTime, mLastHr, mLastRr);
        }
        intent.putExtra(EXTRA_HR, String.valueOf(values.getHr() + dateStr));
        intent.putExtra(EXTRA_RR, values.getRr() + dateStr);
        intent.putExtra(EXTRA_DATA, values.getInfo());
    } else if (UUID_BATTERY_LEVEL.equals(characteristic.getUuid())) {
        mLastBat = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        Log.d(TAG, String.format("Received battery level: %d", mLastBat));
        intent.putExtra(EXTRA_BAT, String.valueOf(mLastBat) + dateStr);
        intent.putExtra(EXTRA_DATA, String.valueOf("Battery Level: " + mLastBat));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        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("%02X ", byteChar));
            }
            intent.putExtra(EXTRA_DATA,
                    BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + "\n"
                            + new String(data) + "\n" + stringBuilder.toString());
        } else {
            intent.putExtra(EXTRA_DATA,
                    BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + "\n"
                            + ((data == null) ? "null" : "No data"));
        }
    }
    sendBroadcast(intent);
}

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

/**
 * ???// w w  w . j  a  v a2s.  com
 *
 * @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: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//w w  w.  j av a 2  s.c  o m
        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.qi.airstat.BluetoothLeService.java

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

    int signal = 0;

    // 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 {/*from  ww  w .  j a v a  2  s  .co  m*/
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }

        final int heartRate = characteristic.getIntValue(format, 1);
        signal = heartRate;
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        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("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());

            signal = Integer.parseInt(new String(data));
        }
    }

    DatabaseManager databaseManager = new DatabaseManager(BluetoothLeService.this);
    SQLiteDatabase database = databaseManager.getWritableDatabase();

    ContentValues values = new ContentValues();
    String date = new SimpleDateFormat("yyMMddHHmmss").format(new java.util.Date());

    values.put(Constants.DATABASE_COMMON_COLUMN_TIME_STAMP, date);
    values.put(Constants.DATABASE_HEART_RATE_COLUMN_HEART_RATE, signal);
    database.insert(Constants.DATABASE_HEART_RATE_TABLE, null, values);

    database.close();

    try {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    } catch (SecurityException exception) {
        exception.printStackTrace();
    }

    JSONObject reformedObject = new JSONObject();
    JSONArray reformedArray = new JSONArray();

    try {
        JSONObject item = new JSONObject();
        item.put("timeStamp", date);
        item.put("connectionID", Constants.CID_BLE);
        item.put("heartrate", signal);
        item.put("latitude", latitude);
        item.put("longitude", longitude);

        reformedArray.put(item);
        reformedObject.put("HR", reformedArray);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    HttpService httpService = new HttpService();
    String responseCode = httpService.executeConn(null, "POST",
            "http://teamc-iot.calit2.net/IOT/public/rcv_json_data", reformedObject);

    sendBroadcast(intent);
}

From source file:net.kenevans.android.hxmmonitor.HxMBleService.java

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    Date now = new Date();
    long date = now.getTime();
    // // DEBUG//from  w ww . java2  s.co  m
    // Set this to "" to not get the date in the return value
    // String dateStr = " @ " + millisecTimeFormater.format(now);
    String dateStr = "";
    final Intent intent = new Intent(action);
    intent.putExtra(EXTRA_UUID, characteristic.getUuid().toString());
    intent.putExtra(EXTRA_DATE, date);

    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        HeartRateValues values = new HeartRateValues(characteristic, date);
        mLastHr = values.getHr();
        mLastRr = values.getRr();
        mLastHrDate = date;
        // // DEBUG
        // Log.d(TAG, String.format("Received heart rate measurement: %d",
        // mLastHr));
        if (mDbAdapter != null && (mCharCustom == null || !mDoCustom)) {
            mDbAdapter.createData(mLastHrDate, mSessionStartTime, mLastHr, mLastRr, INVALID_INT, INVALID_INT);
        }
        intent.putExtra(EXTRA_HR, String.valueOf(values.getHr() + dateStr));
        intent.putExtra(EXTRA_RR, values.getRr() + dateStr);
        intent.putExtra(EXTRA_DATA, values.getInfo());
    } else if (UUID_CUSTOM_MEASUREMENT.equals(characteristic.getUuid())) {
        HxMCustomValues values = new HxMCustomValues(characteristic, date);
        mLastAct = values.getActivity();
        mLastPa = values.getPa();
        // // DEBUG
        // Log.d(TAG, String.format("Received custom measurement: %d %d",
        // mLastAct, mLastPa));
        if (mDbAdapter != null) {
            if (mCharHr == null || !mDoHr) {
                mDbAdapter.createData(date, mSessionStartTime, INVALID_INT, INVALID_STRING, mLastAct, mLastPa);
            } else {
                mDbAdapter.createData(mLastHrDate, mSessionStartTime, mLastHr, mLastRr, mLastAct, mLastPa);
            }
        }
        intent.putExtra(EXTRA_ACT, String.valueOf(values.getActivity() + dateStr));
        intent.putExtra(EXTRA_PA, String.valueOf(values.getPa() + dateStr));
        intent.putExtra(EXTRA_DATA, dateStr + values.getInfo());
    } else if (UUID_BATTERY_LEVEL.equals(characteristic.getUuid())) {
        mLastBat = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        Log.d(TAG, String.format("Received battery level: %d", mLastBat));
        intent.putExtra(EXTRA_BAT, String.valueOf(mLastBat) + dateStr);
        intent.putExtra(EXTRA_DATA, String.valueOf("Battery Level: " + mLastBat));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        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("%02X ", byteChar));
            }
            intent.putExtra(EXTRA_DATA,
                    BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + "\n"
                            + new String(data) + "\n" + stringBuilder.toString());
        } else {
            intent.putExtra(EXTRA_DATA,
                    BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + "\n"
                            + ((data == null) ? "null" : "No data"));
        }
    }
    sendBroadcast(intent);
}