flutterdartbluetoothbluetooth-lowenergy

Flutter Bluetooth - How can i take file from device with bluetooth?


I want to take a file from the sleep tracking device we developed as a company to our application that we developed with flutter via bluetooth. The file is saved to the device in .bin format. Is this possible with the flutter_blue_plus package? or any different method? I would be glad if you help


Solution

  • Yes this is possible but not straightforward. You're going to have to implement a file transfer protocol both on your sleep tracking device and on the application that you have developed with flutter. This is because there isn't a default BLE file transfer protocol and a custom one that fits your use case will need to be developed from scratch. It will not be trivial, but below are a few pointers to help you get started:-

    Sleep Tracking Device

    You need to implement a GATT server on the sleep tracking device that exposes data that can be read/notified. The device needs to be a peripheral that is constantly advertising and willing to accept incoming connections from remote central devices.

    Flutter Application

    The flutter application needs to be a GATT client device that can subscribe (receive notifications) and write data to the sleep tracking device. It also needs to be a central so that it can scan and connect to the sleep tracking device.

    Implementation

    When your device needs to read data from the sleep tracking device, it needs first needs to find it using the scanning function below:-

    // Start scanning
    flutterBlue.startScan(timeout: Duration(seconds: 4));
    
    // Listen to scan results
    var subscription = flutterBlue.scanResults.listen((results) {
        // do something with scan results
        for (ScanResult r in results) {
            print('${r.device.name} found! rssi: ${r.rssi}');
        }
    });
    
    // Stop scanning
    flutterBlue.stopScan();
    

    If the sleep tracking device was found as part of the scanned list, the flutter application needs to connect to it using the function below:-

    // Connect to sleep tracking device
    await device.connect();
    

    Then, notifications need to be enabled using the funciton below:-

    await characteristic.setNotifyValue(true);
    characteristic.value.listen((value) {
        // do something with new value
    });
    

    You then need to write to a characteristic on the sleep tracking device to tell it that you are ready to receive data from it. You can do this with the funciton below:-

    // Writes to a characteristic
    await c.write([0x10, 0x01])
    

    Note that the above is just an example. The characteristic will probably have a different handle and therefore both values above will be different.

    On the sleep tracking device, once the event is triggerred that the flutter app is ready to receive the file, the sleep tracking device needs to chunk the .bin data and transfer it over BLE as BLE notifications. The implementation of the notifications will be dependant on the OS/SW running on the sleep tracking device, so I wont be sharing code here.

    Finally, please note that this is a very simplified implementation of file transfer between two devices over BLE. The implementation can be improved with throughput optimisations and proper flow control mechanism but this is beyond the scope of this question.

    You can find more information at the links below:-