Flutter basic implementation for Classical Bluetooth (only RFCOMM for now), and now also BLE. I hacked out parts of SimpleBluetoothLeTerminal and glued them to flutter_bluetooth_serial and now BluetoothConnection.toAddress
accepts a second parameter, type
, defaulting to AUTO
, giving the behavior of trying to connect with the usual BT Classic mechanism first, then switching to BLE if that fails. (Note this makes the default behavior slow to connect to a BLE device.) It should work out of the box, a drop-in replacement of the previous version, aside from the addition of two "_ble"s to the imports. Hasn't been tested very rigorously yet, sorry. I do note that BLE connection fails the first time I try after installing the app, and is fine after that - probably some permissions thing in the wrong place. Check permissions if you have trouble.
The first goal of this project, started by @edufolly was making an interface for Serial Port Protocol (HC-05 Adapter). Now the plugin features:
-
Adapter status monitoring,
-
Turning adapter on and off,
-
Opening settings,
-
Discovering devices (and requesting discoverability),
-
Listing bonded devices and pairing new ones,
-
Connecting to multiple devices at the same time,
-
Sending and receiving data (multiple connections).
The plugin (for now) uses Serial Port profile for moving data over RFCOMM, so make sure there is running Service Discovery Protocol that points to SP/RFCOMM channel of the device. There could be max up to 7 Bluetooth connections.
For now there is only Android support.
# Add dependency to `pubspec.yaml` of your project.
dependencies:
# ...
flutter_bluetooth_serial_ble: ^0.5.0
# With pub manager
pub get
# or with Flutter
flutter pub get
import 'package:flutter_bluetooth_serial_ble/flutter_bluetooth_serial_ble.dart';
You should look to the Dart code of the library (mostly documented functions) or to the examples code.
// Some simplest connection :F
try {
BluetoothConnection connection = await BluetoothConnection.toAddress(address);
print('Connected to the device');
connection.input.listen((Uint8List data) {
print('Data incoming: ${ascii.decode(data)}');
connection.output.add(data); // Sending data
if (ascii.decode(data).contains('!')) {
connection.finish(); // Closing connection
print('Disconnecting by local host');
}
}).onDone(() {
print('Disconnected by remote request');
});
}
catch (exception) {
print('Cannot connect, exception occured');
}
Note: Work is underway to make the communication easier than operations on byte streams. See #41 for discussion about the topic.
Check out example application with connections with both Arduino HC-05 and Raspberry Pi (RFCOMM) Bluetooth interfaces.
Main screen and options | Discovery and connecting | Simple chat with server | Background connection |
---|---|---|---|
- Add some utils to easier manage
BluetoothConnection
(see discussion #41), - Allow connection method/protocol/UUID specification,
- Listening/server mode,
- Recognizing and displaying
BluetoothClass
of device, - Maybe integration with
flutter_blue
one day ;)
You might also want to check milestones.
- Eduardo Folly
- Martin Mauch
- Patryk Ludwikowski
- https://github.com/kai-morich/SimpleBluetoothLeTerminal for BLE code
After version 0.3.0 we have a lot of collaborators. If you would like to be credited, please send me an email.