Effortlessly integrate L2CAP communication into your Flutter apps with the L2CAP Flutter package. Streamline Bluetooth device communication with a simplified and intuitive interface.
To use this package, add l2cap_ble
as a dependency in your pubspec.yaml
file:
dependencies:
l2cap_ble: ^latest_version
Scan for Bluetooth Devices: Use your preferred Bluetooth scanning package to find and obtain the deviceId of the target device.
final ble = L2capBle();
Subscribe to the connection state stream to track L2CAP connection progress.
final connectionStream = ble.getConnectionState();
Establish a connection to the device using connectToDevice, and await the 'connected' state.
await ble.connectToDevice(deviceId);
Use createL2capChannel to establish the L2CAP channel, providing the required psm.
final channelCreated = await ble.createL2capChannel(psm);
Send and receive messages seamlessly via the L2CAP channel with sendMessage.
final response = await ble.sendMessage(myMessage);
Disconnect using disconnectFromDevice. The L2CAP channel closes automatically.
ble.disconnectFromDevice(deviceId);
Explore the example directory for a complete usage scenario, demonstrating L2CAP communication.
Contributions are welcome! If you encounter issues, have feature suggestions, or want to improve the package, feel free to open an issue or submit a pull request. Please read our Contribution Guidelines for more information.
This project is licensed under the MIT License.