List of usage examples for android.bluetooth BluetoothGattCharacteristic getValue
public byte[] getValue()
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. ja va 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.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 {/*from ww w .j ava2 s. 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:br.liveo.ndrawer.ui.activity.MainActivity.java
License:asdf
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic, final int status) { final Intent intent = new Intent(action); intent.putExtra(EXTRA_UUID, characteristic.getUuid().toString()); intent.putExtra(EXTRA_DATA, characteristic.getValue()); intent.putExtra(EXTRA_STATUS, status); sendBroadcast(intent);/*from w w w . j a v a 2 s .co m*/ }
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 {/*w w w. j av a2 s . c o 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:undot.safedrivers.BLE.BluetoothLeService.java
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); System.out.println("BluetoothLeService broadcastUpdate"); // 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, 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) { intent.putExtra(EXTRA_DATA, new String(data)); sendBroadcast(intent);/*w w w . ja va 2s. co m*/ } // } }
From source file:com.umundus.service.NCallServiceOld.java
public 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 (BATTERY_CHARACTERISTIC.equals(characteristic.getUuid())) { int battery = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); Log.d(TAG, String.format("battery value : %d", battery)); intent.putExtra(BATTERY_VALUE, battery); } else if (NCALL_REMOCON_KEY_NOTI_CHARACTERISTIC.equals(characteristic.getUuid())) { int cmd = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); if (cmd == 0x00) { intent.putExtra(KEY_INPUT_DATA, characteristic.getValue()); } else if (cmd == 0x22) { int status = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1); Log.d(TAG, String.format("scenario status : %d", status)); intent.putExtra(SCENARIO_STATUS_DATA, status); } else if (cmd == 0x32) { intent.putExtra(LED_INPUT_DATA, characteristic.getValue()); } else if (cmd == 0x42) { intent.putExtra(VERSION_INPUT_DATA, characteristic.getValue()); } else if (cmd == 0x52) { int status = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1); Log.i(TAG, "RESPONSE_PATRERN_DATA ++"); if (status == RESPONSE_PATRERN_DATA_WRITE_ERROR) { Log.e(TAG, "PATTERN_DATA_WRITE_ERROR"); } else if (status == RESPONSE_PATRERN_DATA_WRITE_START) { Log.i(TAG, "pattern data response ok!!!!!!!!!!!!!!!!!!!!!!!!!!!"); if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "writeRemoconCharacteristic BluetoothAdapter not initialized"); return; }//from ww w . j a v a2s. c o m BluetoothGattService RemoconService = mBluetoothGatt.getService(NCALL_REMOCON_SERVICE); if (RemoconService == null) { showMessage("NCALL_REMOCON_SERVICE = NULL"); return; } BluetoothGattCharacteristic RemoconChar = RemoconService .getCharacteristic(NCALL_REMOCON_SEND_PATTERN_CHARACTERISTIC); if (RemoconChar == null) { showMessage("NCALL_REMOCON_SEND_PATTERN_CHARACTERISTIC = NULL"); return; } try { final byte[] buffer = mBuffer; final int size = mInputStream.read(buffer); writePacket(mBluetoothGatt, RemoconChar, buffer, size); } catch (Exception e) { Log.e(TAG, "" + e); } } else if (status == RESPONSE_PATRERN_DATA_WRITE_END) { try { patternCount++; if (patternCount < 12) { Log.w(TAG, "" + patternCount); PatternTransferStart(patternCount); } else { broadcastUpdate(PROGRESS_SET_INVISIBLE); } } catch (Exception e) { // TODO: handle exception } } Log.i(TAG, "RESPONSE_PATRERN_DATA --"); } else if (cmd == 0x62) { Log.i(TAG, "/ ack:" + characteristic.getValue()); } } else if (MANUFACTURER_NAME_UUID.equals(characteristic.getUuid())) { byte getdata[] = characteristic.getValue(); Log.i(TAG, "Manufacturer Name : " + new String(getdata, 0, getdata.length)); intent.putExtra(MANUFACTURER_NAME, characteristic.getValue()); } else if (MODEL_NAME_UUID.equals(characteristic.getUuid())) { byte getdata[] = characteristic.getValue(); Log.i(TAG, "Model Number : " + new String(getdata, 0, getdata.length)); intent.putExtra(MODEL_NAME, characteristic.getValue()); } else if (FIRMWARE_REVISON_UUID.equals(characteristic.getUuid())) { byte getdata[] = characteristic.getValue(); Log.i(TAG, "firmware vision = " + new String(getdata, 0, getdata.length)); intent.putExtra(FIRMWARE_REVISON, characteristic.getValue()); } else if (HARDWARE_REVISON_UUID.equals(characteristic.getUuid())) { byte getdata[] = characteristic.getValue(); Log.i(TAG, "hardware revision = " + new String(getdata, 0, getdata.length)); intent.putExtra(HARDWARE_REVISON, characteristic.getValue()); } // synchronized (mLock) { // mLock.notifyAll(); // } 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/*from 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.guangyao.bluetoothtest.service.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 { // 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) { // 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()); // } // }/*from w w w .ja va 2 s . c o m*/ if (TX_CHAR_UUID.equals(characteristic.getUuid())) { byte[] data = characteristic.getValue(); Log.d(TAG, "broadcastUpdate: received from ble:" + DataHandlerUtils.bytesToHexStr(data)); if (ble_status == FREE || ble_status == RECEIVING) { ble_status = RECEIVING; if (data != null) { intent.putExtra(EXTRA_DATA, data); sendBroadcast(intent); } ble_status = FREE; } else if (ble_status == SENDING) { if (final_packet) { final_packet = false; } ble_status = FREE; } } }
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 w w .ja v a2s .c o 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); }