'how to read Step count in MI Band 4 using Flutter and flutter_blue?

I'm developing an app in flutter which can read the data (specifically the Step count) from MI 4 Band and pass it to my flutter app. so far I'm able to connect the device and able to show service lists. But, I'm Not Able to find the step count Data. How can I do so?

the full code I'm using....

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart' as flbs;
import 'package:flutter_blue/flutter_blue.dart';
import 'package:toast/toast.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter  BLE Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter BLE Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  FlutterBlue flutterBlue = FlutterBlue.instance;
  flbs.FlutterBluetoothSerial bluetooth = flbs.FlutterBluetoothSerial.instance;
  flbs.BluetoothConnection bleConn;
  List<BluetoothDevice> devices = [];

  List<BluetoothService> services = [];
  BluetoothService myImportantService;
  List<BluetoothCharacteristic> characteristics = [];
  BluetoothCharacteristic myImportantCharacteristic;

  int counter = 10;

  @override
  void initState() {
    super.initState();
    bluetooth.requestEnable();
    discover();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void discover() async {
    List<ScanResult> res = await flutterBlue.startScan(
      timeout: Duration(seconds: 2),
      allowDuplicates: false,
      scanMode: ScanMode.balanced,
    );
    if ((await flutterBlue.connectedDevices).isNotEmpty) {
      await flutterBlue.connectedDevices.then(
        (value) => value.first.disconnect().then(
              (value) => Toast.show("BLE Disconnected", context),
            ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            OutlinedButton(
              onPressed: () async {
                devices.clear();
                Timer.periodic(Duration(seconds: 1), (t) {
                  setState(() {
                    if (counter > 0) {
                      counter--;
                    } else {
                      counter = 10;
                      t.cancel();
                    }
                  });
                });

                List<ScanResult> res = await flutterBlue.startScan(
                  timeout: Duration(seconds: 10),
                  allowDuplicates: false,
                  scanMode: ScanMode.balanced,
                );

                print(await flutterBlue.connectedDevices);

                res.forEach((element) {
                  print(element.device);
                  devices.add(element.device);
                });

                // try {
                //   await bluetooth.getBondedDevices().then((value) {
                //     setState(() {
                //       devices = value;
                //     });
                //   });
                // } on PlatformException {
                //   print("Can't connect");
                // }
              },
              child: Text(
                'Start Scannig Devices',
              ),
            ),
            Text(
              "$counter",
            ),
            Text(
              'Available Devices:',
            ),
            Expanded(
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: devices.length,
                itemBuilder: (context, index) => InkWell(
                  onTap: () async {
                    // try {
                    //   if (bleConn == null) {
                    //     bleConn = await BluetoothConnection.toAddress(
                    //         devices[index].address);
                    //     setState(() {});

                    //     if (bleConn.isConnected) {
                    //       var data = await bleConn.output.allSent;
                    //       // bleConn.input.forEach((element) {
                    //       //   print(element.first.toString());
                    //       // });
                    //       Toast.show(
                    //         'BLE Connected. \nData incoming: ${data.toString()}',
                    //         context,
                    //         duration: 10,
                    //       );
                    //       // Toast.show("BLE Connected", context);
                    //     }
                    //   } else {
                    //     bleConn.finish();
                    //     bleConn = null;
                    //     setState(() {});
                    //     Toast.show("BLE Disconnected", context, duration: 4);
                    //   }
                    // } on PlatformException {
                    //   Toast.show(
                    //       "Cannot connect due to: Platform Exception", context,
                    //       duration: 4);
                    // }
                    await devices[index].connect(autoConnect: false).then(
                          (value) => Toast.show("BLE Connected", context),
                        );

                    services = await devices[index].discoverServices();
                    for (BluetoothService s in services) {
                      //Would recommend to convert all to lowercase if comparing.
                      print(s.uuid);
                    }
                    print(await services[2].characteristics[0].read());
                    print(String.fromCharCodes(
                        await services[2].characteristics[0].read()));
                    // for (BluetoothService s in services) {
                    //   //Would recommend to convert all to lowercase if comparing.
                    //   print(s.uuid);
                    //   if (s.uuid.toString().toLowerCase() ==
                    //       "00002a53-0000-1000-8000-00805f9b34fb") {
                    //     myImportantService = s;
                    //     print(myImportantService.uuid);
                    //     characteristics = myImportantService.characteristics;
                    //     for (BluetoothCharacteristic c in characteristics) {
                    //       //Would recommend to convert all to lowercase if comparing.
                    //       // if(c.uuid.toString().toLowerCase() == CHARACTERISTIC_UUID)
                    //       //   myImportantCharacteristic = c;
                    //       print(c);
                    //     }
                    //   }
                    // }
                  },
                  onLongPress: () async {
                    // try {

                    // } catch (e) {
                    //   Toast.show(
                    //       "Cannot Disconnect due to: ${e.toString()}", context);
                    // }
                    await devices[index].disconnect().then(
                          (value) => Toast.show("BLE Disconnected", context),
                        );
                  },
                  child: Text(
                    "${devices[index].name}",
                    style: Theme.of(context).textTheme.headline4,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Dependencies used in flutter

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  flutter_blue: ^0.8.0
  flutter_bluetooth_serial: ^0.2.2
  flutter_reactive_ble: ^3.1.0
  toast: ^0.1.5


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source