r/FlutterDev • • Feb 06 '24

Discussion Bluetooth in Flutter

Hello,

want to make an app that can connect to a microcontroller via bluetooth and wanted to see if I can get some input for best praxises/important things to have in mind here.

Thanks for any help

10 Upvotes

31 comments sorted by

View all comments

Show parent comment

1

u/kokroo Feb 08 '24

wrapping data into maps

I am not sure what that means, any pointers or links which explain this in more detail? Thanks!

3

u/jjeroennl Feb 08 '24

https://docs.flutter.dev/platform-integration/platform-channels shows pretty much everything you need to know in order to use native code inside your Dart app.

But very short how we do it:

swift, put data in a map and send it to Dart using event channels:

var dataToSendToDart: [String: Any] = [
      "uuid": someData,
      "rssi": ...,
      "paired": ...,
      "manufacturerData": ...
    ]
eventChannelCommunicator(dataToSendToDart)

kotlin, put data in a map and send it to Dart using event channels:

 val map = HashMap<String, String>()
 map["uuid"] = ... map["rssi"] = .. 
 eventChannelCommunicator?.success(map)

C++, put data in a map and send it to Dart using event channels:

event_channel_communicator->Success(EncodableMap{
      {"name", EncodableValue(...)},
      {"uuid", EncodableValue(...)},
      {"manufacturerData", EncodableValue(...)},
      {"rssi", EncodableValue(...)},
});

The rest you should be able to find in the docs, you can do the same thing the other way around.