Bluetooth Functionality in Flutter
Bluetooth is a type of functionality that provides access in and for other electronic devices, In the app world it is the most reliable wireless tool which provides access to other devices and it also gives free cost service because it is an inbuilt feature of the most mobile and other electronic devices. However, now some devices like mobile phones are out there without Bluetooth because their developers prefer to rely on the internet instead of Bluetooth for any kind of connectivity. In this era of digitalization when the internet is cheaper than food and every electronic device is becoming internet processed device but Bluetooth is still reliable functionality for connectivity.
In this example, we are going to use flutter_blue package to implement Bluetooth functionality in our app
Implementation
Adding FlutterBlue package into in the Flutter. First, you need to add this package in pubspec.yaml
flutter_blue:
Importing FlutterBlue Dependency package into the flutter Dart Code:
import 'package:flutter_blue/flutter_blue.dart';
Now when you are using this flutter plugin then you need to access it from the library and for this, you need to create an instance of it, so we do this by writing this code
FlutterBlue flutterBlueInstance = FlutterBlue.instance;
there are different parts while you are making a connection with the device and theses steps are scanning device, discovering device, connecting device, reading, and writing characteristic and also implementing notification setting into your app.
for all these processes there is a set of code and it is a kind of boilerplate because you need to write exactly as it is for different steps mentioned above
so let’s see how can we implement these steps in our app
Scanning Device
// this line will start scanning bluetooth devices
var scanDevices = flutterBlue.scan().listen((scanResult) {
});
// this line will stop scanning bluetooth devices
scanDevices.cancel();
after scanning all the devices you need to connect your device with the scanned device and you can do this by using this
Connect to a Bluetooth device
// need to create connection for device
var deviceConnection = flutterBlue.connect(device).listen((s) {
if(scan == BluetoothDeviceState.connected) {
// now device is connected you can perform your action
}
});
// it will disconnect your device
deviceConnection.cancel();
Searching Bluetooth services
List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
// perform action with services
});
Reading and writing characteristics
// Reads all characteristics
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
List<int> value = await device.readCharacteristic(c);
print(value);
}
// Writes to a characteristic
await device.writeCharacteristic(c, [0x12, 0x34])
Reading and writing descriptors
// Reads all descriptors
var descriptors = characteristic.descriptors;
for(BluetoothDescriptor d in descriptors) {
List<int> value = await device.readDescriptor(d);
print(value);
}
// Writes to a descriptor
await device.writeDescriptor(d, [0x12, 0x34])
Setting notifications
await device.setNotifyValue(characteristic, true);
device.onValueChanged(characteristic).listen((value) {
// do something with new value
});
so these are one of the key methods and by using these you can make your app rich in functionality.
This Flutter blue plugin is having some functionalities which are platform-specific for example
Read the MTU and request a larger size
final mtu = await device.mtu.first;
await device.requestMtu(512);
Note that iOS will not allow requests of MTU size, and will always try to negotiate the highest possible MTU (iOS supports up to MTU size 185)
Flutter_blue has some references these references indicate which functionality is available for Android, iOS, or for both and it has been clearly instructed by the Flutter_blue team so before going over implementation you must go through this section.
while implementing all these methods you must set your minimum SDK version 19
References
Scanning for service UUID’s doesn’t return any results
One thing which you need to focus on the device is advertising which type of service UUID’s support, there are some advertisement packets like UUID 16 bit and UUID 128 bit.
here in the given images, you can see how at first step device is scanning all available devices and how it is showing all its notification.
you can find the full source code here
flutter-devs/flutter_bluetooth
A new Flutter application. This project is a starting point for a Flutter application. A few resources to get you…github.com
Conclusion
This Flutter_blue package provides a good milieu to implement all your required functionalities whether it is connecting with mobile devices, electronic devices, or IoT devices, It fits in all works for all and is compatible with all. You will get an awesome experience with its implementation.
Note: In some blogs, it has been written that you need to make some changes to info.plist(for iOS) and in the manifest file(for android) but you don’t need to do that. It works fine with any changes in these files.
Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.💙
If this article has helped you a bit and found interesting please clap!👏
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.