service+notifs
This commit is contained in:
parent
7653acb442
commit
0c552e2968
7
.idea/misc.xml
generated
7
.idea/misc.xml
generated
@ -1,5 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DesignSurface">
|
||||
<option name="filePathToZoomLevelMap">
|
||||
<map>
|
||||
<entry key="app/src/main/res/layout/activity_main.xml" value="0.33" />
|
||||
</map>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
|
@ -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<String> 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(){
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -2,17 +2,7 @@
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/mainLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
tools:context=".MainActivity"/>
|
Loading…
x
Reference in New Issue
Block a user