diff --git a/.idea/misc.xml b/.idea/misc.xml index 860da66..9a43771 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/app/src/main/java/com/example/co2sense_app/BLEDevice.java b/app/src/main/java/com/example/co2sense_app/BLEDevice.java index 48bbcc9..5c48030 100644 --- a/app/src/main/java/com/example/co2sense_app/BLEDevice.java +++ b/app/src/main/java/com/example/co2sense_app/BLEDevice.java @@ -7,6 +7,7 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; +import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothProfile; import android.companion.AssociationRequest; @@ -26,9 +27,10 @@ import androidx.annotation.RequiresApi; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import java.nio.ByteBuffer; import java.util.NoSuchElementException; -public class BLEDevice { +public abstract class BLEDevice { private static final int REQUEST_BT_PERMISSION = 16535; private static final int REQUEST_ENABLE_BT = 16536; private static final int SELECT_DEVICE_REQUEST_CODE = 16537; @@ -37,7 +39,7 @@ public class BLEDevice { private final Activity activity; private boolean startPairingOnBTStart; - private BluetoothGatt bluetoothGatt; + protected BluetoothGatt bluetoothGatt; private BluetoothDevice device; private final ActivityResultLauncher requestPermissionLauncher; @@ -48,6 +50,7 @@ public class BLEDevice { } public void startBT() { + Log.d("bt", "startBT"); Context context = activity.getApplicationContext(); BluetoothManager bluetoothManager = ContextCompat.getSystemService(context, BluetoothManager.class); if (bluetoothManager == null) @@ -69,6 +72,7 @@ public class BLEDevice { } protected void pair() { + Log.d("bt", "pair"); BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder() //.setNamePattern(Pattern.compile("My device")) //.addServiceUuid(new ParcelUuid(new UUID(0x123abcL, -1L)), null) @@ -106,15 +110,33 @@ public class BLEDevice { return; }*/ Log.d("bt", device.toString()); + BLEDevice mainClass = this; bluetoothGatt = device.connectGatt(activity, false, new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.d("bt", "connected"); + bluetoothGatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.d("bt", "disconnected"); } } + + @Override + public void onServicesDiscovered(BluetoothGatt gatt, int status) { + if (status == BluetoothGatt.GATT_SUCCESS) { + Log.d("bt", "onServicesDiscovered: finished"); + mainClass.onServicesDiscovered(); + } else { + Log.w("bt", "onServicesDiscovered received: " + status); + } + } + + @Override + public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { + super.onCharacteristicChanged(gatt, characteristic); + Log.d("bt", "onCharacteristicChanged: "+ ByteBuffer.wrap(characteristic.getValue()).getShort()); + } }); } @@ -147,4 +169,8 @@ public class BLEDevice { public void startPairingOnBTStart(boolean status) { startPairingOnBTStart = status; } + + protected void onServicesDiscovered(){ + + } } diff --git a/app/src/main/java/com/example/co2sense_app/CO2Sensor.java b/app/src/main/java/com/example/co2sense_app/CO2Sensor.java index d02dbbd..6925477 100644 --- a/app/src/main/java/com/example/co2sense_app/CO2Sensor.java +++ b/app/src/main/java/com/example/co2sense_app/CO2Sensor.java @@ -1,13 +1,51 @@ package com.example.co2sense_app; +import android.Manifest; +import android.annotation.SuppressLint; import android.app.Activity; +import android.bluetooth.BluetoothAssignedNumbers; +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCharacteristic; +import android.bluetooth.BluetoothGattDescriptor; +import android.bluetooth.BluetoothGattService; +import android.content.pm.PackageManager; import android.os.Build; +import android.util.Log; import androidx.annotation.RequiresApi; +import androidx.core.app.ActivityCompat; + +import java.util.List; +import java.util.UUID; public class CO2Sensor extends BLEDevice { + private int co2, temp, hum; + + public final UUID ENVIRONMENTAL_SENSING_UUID = UUID.fromString("0000181a-0000-1000-8000-00805f9b34fb"); + public final UUID CHAR_CO2_UUID = UUID.fromString("00000C39-0000-1000-8000-00805f9b34fb"); + public final UUID CHAR_TEMP_UUID = UUID.fromString("00002A6E-0000-1000-8000-00805f9b34fb"); + public final UUID CHAR_HUM_UUID = UUID.fromString("00002A6F-0000-1000-8000-00805f9b34fb"); + public final UUID DESCR_CCC_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); + + @RequiresApi(api = Build.VERSION_CODES.S) CO2Sensor(Activity activity) { super(activity); } + + @Override + protected void onServicesDiscovered() { + super.onServicesDiscovered(); + enableNotifications(bluetoothGatt); + } + + @SuppressLint("MissingPermission") + private void enableNotifications(BluetoothGatt bluetoothGatt) { + BluetoothGattService ES = bluetoothGatt.getService(ENVIRONMENTAL_SENSING_UUID); + BluetoothGattCharacteristic CO2Char = ES.getCharacteristic(CHAR_CO2_UUID); + BluetoothGattDescriptor CO2Descr = CO2Char.getDescriptor(DESCR_CCC_UUID); + CO2Descr.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); + bluetoothGatt.setCharacteristicNotification(CO2Char, true); + bluetoothGatt.writeDescriptor(CO2Descr); + } } diff --git a/app/src/main/java/com/example/co2sense_app/MainActivity.java b/app/src/main/java/com/example/co2sense_app/MainActivity.java index 53e8fff..86f2056 100644 --- a/app/src/main/java/com/example/co2sense_app/MainActivity.java +++ b/app/src/main/java/com/example/co2sense_app/MainActivity.java @@ -3,29 +3,36 @@ package com.example.co2sense_app; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; +import androidx.constraintlayout.widget.ConstraintLayout; import android.content.Intent; import android.os.Build; import android.os.Bundle; +import android.view.View; public class MainActivity extends AppCompatActivity { - BLEDevice sensor; + CO2Sensor sensor; + @RequiresApi(api = Build.VERSION_CODES.S) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + sensor = new CO2Sensor(this); + sensor.startPairingOnBTStart(true); + + ConstraintLayout mainLayout = findViewById(R.id.mainLayout); + mainLayout.setOnClickListener(v -> { + sensor.startBT(); + sensor.pair(); + }); } - @RequiresApi(api = Build.VERSION_CODES.S) @Override - protected void onStart() { - super.onStart(); - - sensor = new BLEDevice(this); - sensor.startPairingOnBTStart(true); - sensor.startBT(); - sensor.pair(); + protected void onDestroy() { + super.onDestroy(); + sensor.disconnect(); } @RequiresApi(api = Build.VERSION_CODES.S) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4fc2444..6540f57 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -2,17 +2,7 @@ - - - - \ No newline at end of file + tools:context=".MainActivity"/> \ No newline at end of file