This commit is contained in:
leo 2022-12-19 11:44:36 +01:00
parent a4e6d4ba17
commit c74919496e
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 13 additions and 5 deletions

View File

@ -26,13 +26,9 @@ import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.nio.ByteBuffer;
import java.util.NoSuchElementException;
public abstract class BLEDevice {
private enum ConnectionState {NONE, STARTING_BT, CONNECTING, CONNECTED};
private ConnectionState state = ConnectionState.NONE;
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;
@ -114,7 +110,7 @@ public abstract class BLEDevice {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.d("bt", "onCharacteristicChanged: "+ ByteBuffer.wrap(characteristic.getValue()).getShort());
mainClass.onCharacteristicChanged(gatt, characteristic);
}
});
}
@ -165,6 +161,10 @@ public abstract class BLEDevice {
}
protected void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
}
@RequiresApi(api = Build.VERSION_CODES.S)
public void startScan(ScanCallback callback){
scanCallback = callback;

View File

@ -36,9 +36,17 @@ public class CO2Sensor extends BLEDevice {
@Override
protected void onServicesDiscovered() {
super.onServicesDiscovered();
Log.d("CO2Sense", "onServicesDiscovered: ");
enableNotifications(bluetoothGatt);
}
@Override
protected void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] rawValue = characteristic.getValue();
Log.d("CO2Sense", "onCharacteristicChanged: "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0));
}
@SuppressLint("MissingPermission")
private void enableNotifications(BluetoothGatt bluetoothGatt) {
BluetoothGattService ES = bluetoothGatt.getService(ENVIRONMENTAL_SENSING_UUID);