Bluetooth Connectivity, Scanning & Pairing in Flutter

By Kapil Maheshwari Last Updated 9 Days Ago 3 Minutes Read Technology 0
Smart Entrepreneurs

Bluetooth has one of an integral parts of the technology, that will be enabling wireless communication between devices. We can connect the two devices with each other using the bluetooth connectivity.

Flutter provides one package for bluetooth such as flutter_blue_plus.

Must Read: Want to Know About Flutter? This One Guide is Enough!

What is flutter_blue_plus?

This package allows you to perform tasks such as searching for the nearest device, paring to devices, and reading & writing characteristics.

Getting Started:

Add the flutter_blue_plus packages to your pubspec.yml file

dependencies:
flutter:
sdk: flutter
flutter_blue_plus: ^1.33.4

Permissions:

Android:

You need to add the following permission to AndroidManifest.xml file

  • <uses-permission android:name=”android.permission.BLUETOOTH” />
  • <uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>
  • <uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
  • <uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />

BLUETOOTH: It’s required the perform the Bluetooth operation

ACCESS_FINE_LOCATION & ACCESS_COARSE_LOCATION: It’s required for scanning the devices

iOS:

You need to add the following key to info.plist file

  • NSBluetoothAlwaysUsageDescription.
  • This app requires access to Bluetooth to scan and connect to devices.
  • NSLocationWhenInUseUsageDescription.
  • This app requires access to your location for Bluetooth scanning.

NSBluetoothAlwaysUsageDescription: It describes the why app using the Bluetooth access
NSLocationWhenInUseUsageDescription: It’s used for scanning the devices

Scanning the Nearly Devices

Create a one-dart file for scanning the nearest Bluetooth devices.

import ‘package:flutter/material.dart’;
import ‘package:flutter_blue_plus/flutter_blue_plus.dart’;

void main() {

runApp(MyApp());

}

class MyApp extends StatelessWidget {

@override

Widget build(BuildContext context) {

return MaterialApp(

home: BluetoothScanner(),

);

}

}

class BluetoothScanner extends StatefulWidget {

@override

_BluetoothScannerState createState() => _BluetoothScannerState();

}

class _BluetoothScannerState extends State {

FlutterBlue flutterBlue = FlutterBlue.instance; // access the bluetooth features

List lstDevices= []; // initialize the array

@override

void initState() {

super.initState();

startScanning();

}

void startScanning() async {

await flutterBlue.startScan();

flutterBlue.scanResults.listen((results) {

for (ScanResult result in results) {

if (! lstDevices=.contains(result.device)) {

setState(() {

lstDevices=.add(result.device);

});

}

}

});

}

@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(

title: Text(‘BLE Scanner’),

),

body: ListView.builder(

itemCount: lstDevices=.length,

itemBuilder: (context, index) {

return ListTile(

title: Text(lstDevices= [index].name),

subtitle: Text(lstDevices= [index].id.toString()),

);

},

),

);

}

@override

void dispose() {

flutterBlue.stopScan();

super.dispose();

}}

  • In the above code, we have initiated the FlutterBlue.instance for access all the Bluetooth features like scanning devices, connecting, reading/writing

FlutterBlue flutterBlue = FlutterBlue.instance; // access the bluetooth features

  • We have initilized the devices array for the display the all the nearest device’s name

List lstDevices= = [];

  • startScan() method will be used for scan the devices.

await flutterBlue.startScan();

If ScanResult is not null or not empty then it will be bind all the device names in the devices array.

Using ListView.builder we can bind the devices array and show it on our app.

Connecting the Bluetooth Device:

Once you identify the devices, you need to establish the connection to communicate with

Future connectToDevice (BluetoothDevice device) async {

await device.connect();

}

You need to call the above method when you selecting the device from the list of nearby devices.

Social Media :

Join 10,000 subscribers!

Join Our subscriber’s list and trends, especially on mobile apps development.

I hereby agree to receive newsletters from Mobmaxime and acknowledge company's Privacy Policy.