package com.example.co2sense_app; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCharacteristic; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { CO2Sensor sensor; @RequiresApi(api = Build.VERSION_CODES.S) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar); myToolbar.showContextMenu(); myToolbar.showOverflowMenu(); setSupportActionBar(myToolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); SharedPreferences sharedPref = getSharedPreferences( getString(R.string.main_shared_pref),Context.MODE_PRIVATE); String targetDeviceMAC = sharedPref.getString("target_device_mac", null); Log.d("MAIN", "onCreate: "+targetDeviceMAC); Log.d("MAIN", "onCreate: "+sharedPref.hashCode()); sensor = new CO2Sensor(this); if(targetDeviceMAC != null) { sensor.setDeviceFromMAC(targetDeviceMAC); sensor.startConnectOnBTStart(true); sensor.startBT(); } ESWidget CO2_widget = findViewById(R.id.CO2_ES); ESWidget temp_widget = findViewById(R.id.TEMP_ES); ESWidget hum_widget = findViewById(R.id.HUM_ES); ESWidget batt_widget = findViewById(R.id.BATT_ES); sensor.setGattEventCallback(new BluetoothGattCallback() { @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); Log.d("UI_update", "onCharacteristicChanged: "+characteristic.getUuid()+";\n"+sensor.CHAR_TEMP_UUID+"\n"); if(characteristic.getUuid().equals(sensor.CHAR_CO2_UUID)){ Log.d("UI_update", "onCharacteristicChanged: co2"); int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0); CO2_widget.setValue(value); } else if(characteristic.getUuid().equals(sensor.CHAR_TEMP_UUID)){ Log.d("UI_update", "onCharacteristicChanged: temp"); int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 0); temp_widget.setValue(value/100.0f); } else if(characteristic.getUuid().equals(sensor.CHAR_HUM_UUID)){ Log.d("UI_update", "onCharacteristicChanged: hum"); int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 0); hum_widget.setValue(value/100.0f); } } @RequiresApi(api = Build.VERSION_CODES.TIRAMISU) @Override public void onCharacteristicRead(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); Log.d("UI_update", "onCharacteristicRead: "); if(characteristic.getUuid().equals(sensor.CHAR_BATT_LEVEL_UUID)){ Log.d("UI_update", "onCharacteristicChanged: batt"); int val = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); batt_widget.setValue(val); } } }); } @Override protected void onDestroy() { super.onDestroy(); sensor.disconnect(); } @RequiresApi(api = Build.VERSION_CODES.S) @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); sensor.onActivityResult(requestCode, resultCode, data); } @RequiresApi(api = Build.VERSION_CODES.S) @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); sensor.onRequestPermissionsResult(requestCode, permissions, grantResults); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.select_target: Intent myIntent = new Intent(this, search_ble_devices_activity.class); startActivity(myIntent); return true; default: // If we got here, the user's action was not recognized. // Invoke the superclass to handle it. return super.onOptionsItemSelected(item); } } }