// ---------------------------------------------------------------------------------------------------------------- // Broadcast an intent with a string representing an action an extra string with the data // Modify this code for data that is not in a string format private void broadcastUpdate( final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); // Create new intent to broadcast the action if (action.equals(ACTION_DATA_AVAILABLE)) { // See if we need to send data if (UUID_MLDP_DATA_PRIVATE_CHARACTERISTIC.equals( characteristic.getUuid())) { // See if this is the correct characteristic String dataValue = characteristic.getStringValue(0); // Get the data (in this case it is a string) intent.putExtra(EXTRA_DATA, dataValue); // Add the data string to the intent } } else { // Did not get an action string we expect Log.d(TAG, "Action: " + action); } sendBroadcast(intent); // Broadcast the intent }
// Called when a remote characteristic changes (like the RX characteristic). @Override public void onCharacteristicChanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); vitaminValue = characteristic.getStringValue(0); Intent intent = new Intent(MainActivity.this, ResultsActivity.class); intent.putExtra(EXTRA_MESSAGE, vitaminValue); startActivity(intent); animatedCircleLoadingView.stopOk(); presentValue(); }
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_LUGGAGE_NTF.equals(characteristic.getUuid())) { final String recData = characteristic.getStringValue(0); Log.d(TAG, String.format("Received heart rate: %s", recData)); intent.putExtra(EXTRA_DATA, recData); } 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()); } } sendBroadcast(intent); }
@Override public void onCharacteristicChanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { LogUtil.info(TAG, "接收数据onCharacteristicChanged()"); String str = ""; switch (DEVICE_TYPE) { case 1: // 脂肪秤 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); if (characteristic.getValue() != null) { LogUtil.info(TAG, characteristic.getStringValue(0)); } break; case 2: // 血压计 LogUtil.info(TAG, "数据长度:" + characteristic.getValue().length); for (int i = 0; ; ++i) { if (i >= characteristic.getValue().length) { receiveData = str; broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); LogUtil.info(TAG, "血压计结果数据:" + receiveData); return; } str = str + (0xFF & characteristic.getValue()[i]) + " "; } case 3: // 血糖仪 for (int i = 0; ; ++i) { if (i >= characteristic.getValue().length) { receiveData = str; broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); LogUtil.info(TAG, "血糖仪结果数据:" + receiveData); return; } str = str + (0xFF & characteristic.getValue()[i]) + " "; } default: break; } }
@Override public void onCharacteristicNotified( final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { final String data = characteristic.getStringValue(0); mCallbacks.onDataReceived(data); }
@Override public void onCharacteristicWrite( final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { final String data = characteristic.getStringValue(0); mCallbacks.onDataSent(data); }