|
| 1 | +[](https://pub.dartlang.org/packages/flutter_blue_plus) |
| 2 | + |
| 3 | +<br> |
| 4 | +<p align="center"> |
| 5 | +<img alt="FlutterBlue" src="https://github.com/boskokg/flutter_blue_plus/blob/master/site/flutterblue.png?raw=true" /> |
| 6 | +</p> |
| 7 | +<br><br> |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +FlutterBluePlus is a bluetooth plugin for [Flutter](https://flutter.dev), a new app SDK to help developers build modern multi-platform apps. Note: this plugin is continuous work from FlutterBlue since maintaince stoped. |
| 12 | + |
| 13 | +## Alpha version |
| 14 | + |
| 15 | +**This package must be tested on a real device.** |
| 16 | + |
| 17 | +## Cross-Platform Bluetooth LE |
| 18 | +FlutterBluePlus aims to offer the most from both platforms (iOS and Android). |
| 19 | + |
| 20 | +Using the FlutterBluePlus instance, you can scan for and connect to nearby devices ([BluetoothDevice](#bluetoothdevice-api)). |
| 21 | +Once connected to a device, the BluetoothDevice object can discover services ([BluetoothService](lib/src/bluetooth_service.dart)), characteristics ([BluetoothCharacteristic](lib/src/bluetooth_characteristic.dart)), and descriptors ([BluetoothDescriptor](lib/src/bluetooth_descriptor.dart)). |
| 22 | +The BluetoothDevice object is then used to directly interact with characteristics and descriptors. |
| 23 | + |
| 24 | +## Usage |
| 25 | +### Obtain an instance |
| 26 | +```dart |
| 27 | +FlutterBluePlus flutterBlue = FlutterBluePlus.instance; |
| 28 | +``` |
| 29 | + |
| 30 | +### Scan for devices |
| 31 | +```dart |
| 32 | +// Start scanning |
| 33 | +flutterBlue.startScan(timeout: Duration(seconds: 4)); |
| 34 | +
|
| 35 | +// Listen to scan results |
| 36 | +var subscription = flutterBlue.scanResults.listen((results) { |
| 37 | + // do something with scan results |
| 38 | + for (ScanResult r in results) { |
| 39 | + print('${r.device.name} found! rssi: ${r.rssi}'); |
| 40 | + } |
| 41 | +}); |
| 42 | +
|
| 43 | +// Stop scanning |
| 44 | +flutterBlue.stopScan(); |
| 45 | +``` |
| 46 | + |
| 47 | +### Connect to a device |
| 48 | +```dart |
| 49 | +// Connect to the device |
| 50 | +await device.connect(); |
| 51 | +
|
| 52 | +// Disconnect from device |
| 53 | +device.disconnect(); |
| 54 | +``` |
| 55 | + |
| 56 | +### Discover services |
| 57 | +```dart |
| 58 | +List<BluetoothService> services = await device.discoverServices(); |
| 59 | +services.forEach((service) { |
| 60 | + // do something with service |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +### Read and write characteristics |
| 65 | +```dart |
| 66 | +// Reads all characteristics |
| 67 | +var characteristics = service.characteristics; |
| 68 | +for(BluetoothCharacteristic c in characteristics) { |
| 69 | + List<int> value = await c.read(); |
| 70 | + print(value); |
| 71 | +} |
| 72 | +
|
| 73 | +// Writes to a characteristic |
| 74 | +await c.write([0x12, 0x34]) |
| 75 | +``` |
| 76 | + |
| 77 | +### Read and write descriptors |
| 78 | +```dart |
| 79 | +// Reads all descriptors |
| 80 | +var descriptors = characteristic.descriptors; |
| 81 | +for(BluetoothDescriptor d in descriptors) { |
| 82 | + List<int> value = await d.read(); |
| 83 | + print(value); |
| 84 | +} |
| 85 | +
|
| 86 | +// Writes to a descriptor |
| 87 | +await d.write([0x12, 0x34]) |
| 88 | +``` |
| 89 | + |
| 90 | +### Set notifications and listen to changes |
| 91 | +```dart |
| 92 | +await characteristic.setNotifyValue(true); |
| 93 | +characteristic.value.listen((value) { |
| 94 | + // do something with new value |
| 95 | +}); |
| 96 | +``` |
| 97 | + |
| 98 | +### Read the MTU and request larger size |
| 99 | +```dart |
| 100 | +final mtu = await device.mtu.first; |
| 101 | +await device.requestMtu(512); |
| 102 | +``` |
| 103 | +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) |
| 104 | + |
| 105 | +## Getting Started |
| 106 | +### Change the minSdkVersion for Android |
| 107 | + |
| 108 | +flutter_blue_plus is compatible only from version 19 of Android SDK so you should change this in **android/app/build.gradle**: |
| 109 | +```dart |
| 110 | +Android { |
| 111 | + defaultConfig { |
| 112 | + minSdkVersion: 19 |
| 113 | +``` |
| 114 | +### Add permissions for Bluetooth |
| 115 | +We need to add the permission to use Bluetooth and access location: |
| 116 | + |
| 117 | +#### **Android** |
| 118 | +In the **android/app/src/main/AndroidManifest.xml** let’s add: |
| 119 | + |
| 120 | +```xml |
| 121 | + <uses-permission android:name="android.permission.BLUETOOTH" /> |
| 122 | + <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> |
| 123 | + <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
| 124 | + <application |
| 125 | +``` |
| 126 | +#### **IOS** |
| 127 | +In the **ios/Runner/Info.plist** let’s add: |
| 128 | + |
| 129 | +```dart |
| 130 | + <dict> |
| 131 | + <key>NSBluetoothAlwaysUsageDescription</key> |
| 132 | + <string>Need BLE permission</string> |
| 133 | + <key>NSBluetoothPeripheralUsageDescription</key> |
| 134 | + <string>Need BLE permission</string> |
| 135 | + <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> |
| 136 | + <string>Need Location permission</string> |
| 137 | + <key>NSLocationAlwaysUsageDescription</key> |
| 138 | + <string>Need Location permission</string> |
| 139 | + <key>NSLocationWhenInUseUsageDescription</key> |
| 140 | + <string>Need Location permission</string> |
| 141 | +``` |
| 142 | + |
| 143 | +For location permissions on iOS see more at: [https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services](https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services) |
| 144 | + |
| 145 | + |
| 146 | +## Reference |
| 147 | +### FlutterBlue API |
| 148 | +| | Android | iOS | Description | |
| 149 | +| :--------------- | :----------------: | :------------------: | :-------------------------------- | |
| 150 | +| scan | :white_check_mark: | :white_check_mark: | Starts a scan for Bluetooth Low Energy devices. | |
| 151 | +| state | :white_check_mark: | :white_check_mark: | Stream of state changes for the Bluetooth Adapter. | |
| 152 | +| isAvailable | :white_check_mark: | :white_check_mark: | Checks whether the device supports Bluetooth. | |
| 153 | +| isOn | :white_check_mark: | :white_check_mark: | Checks if Bluetooth functionality is turned on. | |
| 154 | + |
| 155 | +### BluetoothDevice API |
| 156 | +| | Android | iOS | Description | |
| 157 | +| :-------------------------- | :------------------: | :------------------: | :-------------------------------- | |
| 158 | +| connect | :white_check_mark: | :white_check_mark: | Establishes a connection to the device. | |
| 159 | +| disconnect | :white_check_mark: | :white_check_mark: | Cancels an active or pending connection to the device. | |
| 160 | +| discoverServices | :white_check_mark: | :white_check_mark: | Discovers services offered by the remote device as well as their characteristics and descriptors. | |
| 161 | +| services | :white_check_mark: | :white_check_mark: | Gets a list of services. Requires that discoverServices() has completed. | |
| 162 | +| state | :white_check_mark: | :white_check_mark: | Stream of state changes for the Bluetooth Device. | |
| 163 | +| mtu | :white_check_mark: | :white_check_mark: | Stream of mtu size changes. | |
| 164 | +| requestMtu | :white_check_mark: | | Request to change the MTU for the device. | |
| 165 | +| readRssi | :white_check_mark: | :white_check_mark: | Read RSSI from a connected device. | |
| 166 | + |
| 167 | +### BluetoothCharacteristic API |
| 168 | +| | Android | iOS | Description | |
| 169 | +| :-------------------------- | :------------------: | :------------------: | :-------------------------------- | |
| 170 | +| read | :white_check_mark: | :white_check_mark: | Retrieves the value of the characteristic. | |
| 171 | +| write | :white_check_mark: | :white_check_mark: | Writes the value of the characteristic. | |
| 172 | +| setNotifyValue | :white_check_mark: | :white_check_mark: | Sets notifications or indications on the characteristic. | |
| 173 | +| value | :white_check_mark: | :white_check_mark: | Stream of characteristic's value when changed. | |
| 174 | + |
| 175 | +### BluetoothDescriptor API |
| 176 | +| | Android | iOS | Description | |
| 177 | +| :-------------------------- | :------------------: | :------------------: | :-------------------------------- | |
| 178 | +| read | :white_check_mark: | :white_check_mark: | Retrieves the value of the descriptor. | |
| 179 | +| write | :white_check_mark: | :white_check_mark: | Writes the value of the descriptor. | |
| 180 | + |
| 181 | +## Troubleshooting |
| 182 | +### When I scan using a service UUID filter, it doesn't find any devices. |
| 183 | +Make sure the device is advertising which service UUID's it supports. This is found in the advertisement |
| 184 | +packet as **UUID 16 bit complete list** or **UUID 128 bit complete list**. |
0 commit comments