Search in sources :

Example 1 with Gas

use of com.savonia.thesis.db.entity.Gas in project Thesis by bajnax.

the class BluetoothLowEnergyService method broadcastUpdate.

// broadcasting characteristic's data to LeConnectedDeviceActivity
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    /*// 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());*/
    final byte[] data = characteristic.getValue();
    String value;
    if (data != null && data.length > 0) {
        value = new String(data);
        intent.putExtra(EXTRA_DATA, value);
        // retrieving the double value from the temperature notification
        if (value.charAt(0) == 't' && !value.contains("g")) {
            try {
                StringBuilder sb = new StringBuilder(value);
                value = sb.substring(2);
                value = value.trim();
                double tempValue = Double.parseDouble(value);
                Temperature temperature = new Temperature(tempValue);
                CentralRepository.getInstance(SensorsValuesDatabase.getDatabase(getApplicationContext())).insertTemperature(temperature);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (value.charAt(0) == 'g' && !value.contains("t")) {
            // retrieving the double value from the gas notification
            try {
                StringBuilder sb = new StringBuilder(value);
                value = sb.substring(2);
                value = value.trim();
                double gasValue = Double.parseDouble(value);
                Gas gas = new Gas(gasValue);
                CentralRepository.getInstance(SensorsValuesDatabase.getDatabase(getApplicationContext())).insertGas(gas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    sendBroadcast(intent);
}
Also used : Temperature(com.savonia.thesis.db.entity.Temperature) Gas(com.savonia.thesis.db.entity.Gas) Intent(android.content.Intent)

Aggregations

Intent (android.content.Intent)1 Gas (com.savonia.thesis.db.entity.Gas)1 Temperature (com.savonia.thesis.db.entity.Temperature)1