ihealth_sdk 0.0.1 copy "ihealth_sdk: ^0.0.1" to clipboard
ihealth_sdk: ^0.0.1 copied to clipboard

A Flutter plugin to integrate the iHealth KN-550BT blood pressure monitor.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:ihealth_sdk/ihealth_sdk.dart';

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyApp> {
  // Use the plugin's event stream instead of direct EventChannel:
  Stream<dynamic> get dataEventsStream => IHealthHRPlugin.deviceDataStream;

  String connectionStatus = "Waiting for device status...";
  String batteryLevel = "--";
  int offlineHistoryCount = 0;
  String message = "";

  int? latestHeartRate;
  int? latestSys;
  int? latestDia;
  String latestTime = "";
  String deviceTime = "";
  bool? isBackLightOn;
  bool? isClockOn;

  final List<Map<String, dynamic>> historyData = [];

  @override
  void initState() {
    super.initState();
    connectionEvents();
  }

  void connectionEvents() {
    dataEventsStream.listen(
      (event) {
        final Map<dynamic, dynamic> data = Map<dynamic, dynamic>.from(event);
        final String eventType = data['event'] ?? '';

        switch (eventType) {
          case 'connectionStateChanged':
            final String status = data['status'] ?? 'unknown';
            setState(() {
              connectionStatus =
                  status.isNotEmpty
                      ? status[0].toUpperCase() + status.substring(1)
                      : "Unknown";
            });
            break;

          case 'offlineHistoryCount':
            final int count = data['count'] ?? 0;
            final String msg = data['message'] ?? '';
            setState(() {
              offlineHistoryCount = count;
              message = msg;
            });
            break;

          case 'historyData':
            setState(() {
              latestHeartRate = data['heartRate'] as int?;
              latestSys = data['sys'] as int?;
              latestDia = data['dia'] as int?;
              latestTime = data['time'] ?? "";

              final newEntry = {
                "time": latestTime,
                "sys": latestSys,
                "dia": latestDia,
                "heartRate": latestHeartRate,
              };
              if (!historyData.any((e) => e['time'] == latestTime)) {
                historyData.add(newEntry);
              }
            });
            break;

          case 'displayStatus':
            setState(() {
              isBackLightOn = data['backlightOn'] as bool?;
              isClockOn = data['clockOn'] as bool?;
            });
            break;

          case 'deviceTime':
            setState(() {
              deviceTime = data['time'] ?? "";
            });
            break;

          default:
            print("Unknown event type: $eventType");
        }
      },
      onError: (error) {
        print("Error receiving data events: $error");
        setState(() {
          message = "Error receiving data: $error";
        });
      },
    );
  }

  // ... Rest of your UI code here unchanged ...

  Widget buildHistoryList(double maxHeight) {
    if (historyData.isEmpty) {
      return Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text("No historical data yet.", style: TextStyle(fontSize: 16)),
      );
    }

    return Container(
      constraints: BoxConstraints(maxHeight: maxHeight),
      child: ListView.separated(
        shrinkWrap: true,
        itemCount: historyData.length,
        separatorBuilder: (_, __) => Divider(),
        itemBuilder: (context, index) {
          final item = historyData[index];
          return Padding(
            padding: const EdgeInsets.symmetric(vertical: 6),
            child: Text(
              "Time: ${item['time']}, Sys: ${item['sys']}, Dia: ${item['dia']}, HR: ${item['heartRate']}",
              style: TextStyle(fontSize: 16),
            ),
          );
        },
      ),
    );
  }

  Widget buildStatusText(String title, String value, {Color? color}) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: RichText(
        text: TextSpan(
          text: '$title ',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: Colors.black,
          ),
          children: [
            TextSpan(
              text: value,
              style: TextStyle(
                fontWeight: FontWeight.normal,
                color: color ?? Colors.black,
                fontSize: 18,
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final maxHistoryHeight =
        screenHeight * 0.3 > 300 ? 300.0 : screenHeight * 0.3;

    return Scaffold(
      appBar: AppBar(title: Text('BP Monitor')),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Container(
          constraints: BoxConstraints(maxWidth: 600),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              buildStatusText(
                'Connection Status:',
                connectionStatus,
                color:
                    connectionStatus.toLowerCase() == 'connected'
                        ? Colors.green
                        : Colors.red,
              ),
              buildStatusText(
                'Battery Level:',
                batteryLevel,
                color: Colors.green,
              ),
              buildStatusText('Offline History Count:', '$offlineHistoryCount'),
              buildStatusText(
                'Latest Heart Rate (BPM):',
                latestHeartRate != null ? '$latestHeartRate' : '--',
                color: Colors.red,
              ),
              buildStatusText(
                'Latest Blood Pressure:',
                (latestSys != null && latestDia != null)
                    ? 'High: $latestSys mmHg, Low: $latestDia mmHg'
                    : '--',
                color: Colors.blue,
              ),

              if (latestTime.isNotEmpty)
                buildStatusText('Measurement Time:', latestTime),
              if (deviceTime.isNotEmpty)
                buildStatusText('Device Reported Time:', deviceTime),
              buildStatusText(
                'Backlight:',
                isBackLightOn == null ? 'N/A' : (isBackLightOn! ? 'ON' : 'OFF'),
                color: isBackLightOn == true ? Colors.green : Colors.grey,
              ),
              buildStatusText(
                'Clock Display:',
                isClockOn == null ? 'N/A' : (isClockOn! ? 'ON' : 'OFF'),
                color: isClockOn == true ? Colors.green : Colors.grey,
              ),
              Divider(height: 32, thickness: 2),
              Text(
                'Historical Data:',
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
              SizedBox(height: 8),
              buildHistoryList(maxHistoryHeight),
              SizedBox(height: 32),
              if (message.isNotEmpty)
                Text(
                  'Message: $message',
                  style: TextStyle(fontSize: 16, color: Colors.blueGrey),
                  textAlign: TextAlign.center,
                ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
0
points
13
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin to integrate the iHealth KN-550BT blood pressure monitor.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on ihealth_sdk

Packages that implement ihealth_sdk