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"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<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">
|
<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" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
|
@ -7,6 +7,7 @@ import android.bluetooth.BluetoothAdapter;
|
|||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothGatt;
|
import android.bluetooth.BluetoothGatt;
|
||||||
import android.bluetooth.BluetoothGattCallback;
|
import android.bluetooth.BluetoothGattCallback;
|
||||||
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
import android.bluetooth.BluetoothManager;
|
import android.bluetooth.BluetoothManager;
|
||||||
import android.bluetooth.BluetoothProfile;
|
import android.bluetooth.BluetoothProfile;
|
||||||
import android.companion.AssociationRequest;
|
import android.companion.AssociationRequest;
|
||||||
@ -26,9 +27,10 @@ import androidx.annotation.RequiresApi;
|
|||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
public class BLEDevice {
|
public abstract class BLEDevice {
|
||||||
private static final int REQUEST_BT_PERMISSION = 16535;
|
private static final int REQUEST_BT_PERMISSION = 16535;
|
||||||
private static final int REQUEST_ENABLE_BT = 16536;
|
private static final int REQUEST_ENABLE_BT = 16536;
|
||||||
private static final int SELECT_DEVICE_REQUEST_CODE = 16537;
|
private static final int SELECT_DEVICE_REQUEST_CODE = 16537;
|
||||||
@ -37,7 +39,7 @@ public class BLEDevice {
|
|||||||
private final Activity activity;
|
private final Activity activity;
|
||||||
private boolean startPairingOnBTStart;
|
private boolean startPairingOnBTStart;
|
||||||
|
|
||||||
private BluetoothGatt bluetoothGatt;
|
protected BluetoothGatt bluetoothGatt;
|
||||||
private BluetoothDevice device;
|
private BluetoothDevice device;
|
||||||
private final ActivityResultLauncher<String> requestPermissionLauncher;
|
private final ActivityResultLauncher<String> requestPermissionLauncher;
|
||||||
|
|
||||||
@ -48,6 +50,7 @@ public class BLEDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startBT() {
|
public void startBT() {
|
||||||
|
Log.d("bt", "startBT");
|
||||||
Context context = activity.getApplicationContext();
|
Context context = activity.getApplicationContext();
|
||||||
BluetoothManager bluetoothManager = ContextCompat.getSystemService(context, BluetoothManager.class);
|
BluetoothManager bluetoothManager = ContextCompat.getSystemService(context, BluetoothManager.class);
|
||||||
if (bluetoothManager == null)
|
if (bluetoothManager == null)
|
||||||
@ -69,6 +72,7 @@ public class BLEDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void pair() {
|
protected void pair() {
|
||||||
|
Log.d("bt", "pair");
|
||||||
BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder()
|
BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder()
|
||||||
//.setNamePattern(Pattern.compile("My device"))
|
//.setNamePattern(Pattern.compile("My device"))
|
||||||
//.addServiceUuid(new ParcelUuid(new UUID(0x123abcL, -1L)), null)
|
//.addServiceUuid(new ParcelUuid(new UUID(0x123abcL, -1L)), null)
|
||||||
@ -106,15 +110,33 @@ public class BLEDevice {
|
|||||||
return;
|
return;
|
||||||
}*/
|
}*/
|
||||||
Log.d("bt", device.toString());
|
Log.d("bt", device.toString());
|
||||||
|
BLEDevice mainClass = this;
|
||||||
bluetoothGatt = device.connectGatt(activity, false, new BluetoothGattCallback() {
|
bluetoothGatt = device.connectGatt(activity, false, new BluetoothGattCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
||||||
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
Log.d("bt", "connected");
|
Log.d("bt", "connected");
|
||||||
|
bluetoothGatt.discoverServices();
|
||||||
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||||
Log.d("bt", "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) {
|
public void startPairingOnBTStart(boolean status) {
|
||||||
startPairingOnBTStart = status;
|
startPairingOnBTStart = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void onServicesDiscovered(){
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,51 @@
|
|||||||
package com.example.co2sense_app;
|
package com.example.co2sense_app;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Activity;
|
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.os.Build;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class CO2Sensor extends BLEDevice {
|
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)
|
@RequiresApi(api = Build.VERSION_CODES.S)
|
||||||
CO2Sensor(Activity activity) {
|
CO2Sensor(Activity activity) {
|
||||||
super(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.Nullable;
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
BLEDevice sensor;
|
CO2Sensor sensor;
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.S)
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.S)
|
sensor = new CO2Sensor(this);
|
||||||
@Override
|
|
||||||
protected void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
|
|
||||||
sensor = new BLEDevice(this);
|
|
||||||
sensor.startPairingOnBTStart(true);
|
sensor.startPairingOnBTStart(true);
|
||||||
|
|
||||||
|
ConstraintLayout mainLayout = findViewById(R.id.mainLayout);
|
||||||
|
mainLayout.setOnClickListener(v -> {
|
||||||
sensor.startBT();
|
sensor.startBT();
|
||||||
sensor.pair();
|
sensor.pair();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
sensor.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.S)
|
@RequiresApi(api = Build.VERSION_CODES.S)
|
||||||
|
@ -2,17 +2,7 @@
|
|||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:id="@+id/mainLayout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
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>
|
|
Loading…
x
Reference in New Issue
Block a user