flutter_blue_classic 0.0.6

SDKflutter
Platformandroid

A flutter plugin for communicating with bluetooth classic devices

flutter_blue_classic

Flutter_blue_classic is a flutter plugin for communicating with bluetooth classic devices. If you want to use an Bluetooth low energy (BLE) device, you might want to consider flutter_blue_plus.

A note on iOS

This plugin is currently only compatible with Android. iOS does not provide a similar interface for Bluetooth Classic like Android does. On iOS your BL device must be MFi (Made for iPod/iPhone) certified and you will have to use the External Accessory Framework. Since this is out of the scope of this package, you will have to implement this yourself.

Getting Started

minSdkVersion

This package is only compatible with Android SDK 21 or higher. Please set your minSdkVersion in the android/app/build.gradle accordingly.

Permissions

As the Android permission requirements can differ from case to case, this package does not define any permissions in it's Manifest. It is therefore necessary to declare them yourself. For detailed information, take a look at the Android Developer Documentation on Permissions.

Below are two common presets:

Without location access

In the android/app/src/main/AndroidManifest.xml add:

<!-- Permissions for Android 12 or above -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />

    <!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />

With location access

In the android/app/src/main/AndroidManifest.xml add:

<!-- Permissions for Android 12 or above -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

    <!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />

Then pass the accessFineLocation parameter when initializing the plugin:


final blueClassic = FlutterBlueClassic(usesFineLocation: true);

Reference

FlutterBlueClassic

Description
isSupportedChecks whether the device supports Bluetooth
isEnabledChecks whether Bluetooth is enabled
adapterStateNowCurrent state of the bluetooth adapter
adapterStateStream of on, off and intermediary states of the bluetooth adapter
bondedDevicesReturns the list of bonded (paired) devices
startScanStarts a scan for Bluetooth devices
stopScanStop an existing scan for Bluetooth devices
isScanningNowChecks whether the Bluetooth adapter is currently scanning.
isScanningStream whether the device is scanning for BL devices.
scanResultsStream of found devices during scan
turnOnRequests to turns the bluetooth adapter on
bondDeviceRequests to create a bond with a bluetooth device
connectTries to connect with a bluetooth device

BluetoothConnection

Description
inputThe stream of data received from the connected BL device
outputA stream sink to send byte data to the connected BL device
writeStringA helper to send utf-8 encoded strings to the remote device
isConnectedIndicates whether the connection is still open.
disposeThis should be called, when the Connection is no longer needed and will call finish (see below).
finishThis will wait for any ongoing writes to be finished and gracefully close the connection.
closeThis will immediately close the connection.

Acknowledgement

flutter_blue_classic is loosely based on flutter_bluetooth_serial.