1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] get values(pressure blood) from ble device in flutter

Discussão em 'Mobile' iniciado por Stack, Novembro 3, 2024 às 13:42.

  1. Stack

    Stack Membro Participativo

    I used this code and utilized the flutter_reactive_ble package to read the blood pressure (systolic, diastolic) from my device in mmHg and pulse rate in bpm, but I am struggling to figure out how to do it , In my console, I received :

    D/BluetoothGatt(21288): setCharacteristicNotification() - uuid: 00002a35-0000-1000-8000-00805f9b34fb enable: true I/flutter (21288): Received raw data for blood pressure measurement: [222, 136, 244, 32, 243, 255, 7, 228, 7, 1, 19, 22, 55, 0, 188, 242, 1, 68, 1]

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text('BLE Scanner'),
    ),
    body: Center(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
    ElevatedButton(
    onPressed: _scanForDevices,
    child: Text('Scan for Devices'),
    ),
    SizedBox(height: 20),
    Text('Discovered Devices:'),
    Expanded(
    child: ListView.builder(
    itemCount: devices.length,
    itemBuilder: (context, index) {
    final device = devices[index];
    return ListTile(
    title: Text(device.name ?? 'Unknown'),
    subtitle: Text(device.id),
    onTap: () {
    _connectToDevice(device.id);
    _subscribeToBloodPressureMeasurement(device.id);
    },
    );
    },
    ),
    ),
    ],
    ),
    ),
    );
    }

    void _scanForDevices() {
    flutterReactiveBle.scanForDevices(
    withServices: [],
    scanMode: ScanMode.lowLatency,
    ).listen(
    (device) {
    final existingDeviceIndex =
    devices.indexWhere((d) => d.id == device.id);
    if (existingDeviceIndex != -1) {
    setState(() {
    devices[existingDeviceIndex] = device;
    });
    } else {
    setState(() {
    devices.add(device);
    });
    }
    },
    onError: (error) {
    print('Error: $error');
    },
    );
    }

    void _connectToDevice(String deviceId) {
    flutterReactiveBle
    .connectToDevice(
    id: deviceId,
    servicesWithCharacteristicsToDiscover: {
    Uuid.parse('00001810-0000-1000-8000-00805f9b34fb'): [ // Caractéristique de mesure de la pression artérielle Uuid.parse('00002a35-0000-1000-8000-00805f9b34fb'), // Caractéristique des fonctionnalités de la pression artérielle Uuid.parse('00002a49-0000-1000-8000-00805f9b34fb'), ],
    },
    connectionTimeout: const Duration(seconds: 2),
    )
    .listen(
    (connectionState) {
    // Gérez les mises à jour de l'état de connexion
    if (connectionState == DeviceConnectionState.connected) {
    print("Connected successfully to device: $deviceId");
    _subscribeToBloodPressureMeasurement(deviceId);
    }
    },
    onError: (error) {
    print('Error connecting to device: $error');
    // Gérez les erreurs éventuelles
    },
    );
    }

    void _subscribeToBloodPressureMeasurement(String deviceId) {
    final bloodPressureCharacteristic = QualifiedCharacteristic(
    characteristicId: Uuid.parse('00002a35-0000-1000-8000-00805f9b34fb'),
    deviceId: deviceId,
    serviceId: Uuid.parse('00001810-0000-1000-8000-00805f9b34fb'),
    );

    // Souscrire à la caractéristique de mesure de la pression artérielle
    flutterReactiveBle
    .subscribeToCharacteristic(bloodPressureCharacteristic)
    .listen(
    (data) {
    print('Received raw data for blood pressure measurement: $data');
    // Traitez les données de notification ici
    },
    onError: (error) {
    print('Error subscribing to blood pressure measurement: $error');
    // Gérez les erreurs éventuelles lors de l'abonnement
    },
    );
    }


    }

    Continue reading...

Compartilhe esta Página