Mastering Flutter: Bluetooth use
Let’s face it, we are living in a connected world in which also physical objects can be used with software. Today we are going to dive into this world by looking at one of the most common ways to connect to devices: bluetooth.
To enable bluetooth connectivity with your Flutter app you should consider using this package as it is the most used one. You can obviously create a custom plugin if you have specific needs but this one will be ok for most cases.
To start add it to your
then add these permissions for
Also in the
For location permissions on iOS see more at: https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services
The main goal of a bluetooth connection is to connect to a device, discover its services and write/read data from it. First thing first we need to find our device so let’s do a good old scan!
To stop just
While you should not connect-disconnect so quickly you can see that doing it is a piece of cake.
When you are connected then you should discover device services this way:
Then, as each service has n characteristics, you can check all of them this way;
With characteristics then you can pretty much do whatever you want like read or write:
We can do the same with the characteristic descriptors:
Last, but not least, we can also listen to characteristics changes this way:
There you are, you can now work with bluetooth on Flutter. Be aware that Android and iOS behaviors can be a bit different, for example Android tends to cache manufacturer data so you should add “
allowDuplicates” to obtain them always. Feel free to explore and experiment!
Want to check more awesome Flutter tutorials? Click here!