Crates.io | blec |
lib.rs | blec |
version | 0.3.4 |
source | src |
created_at | 2023-07-17 15:50:17.273731 |
updated_at | 2024-07-25 15:17:34.631157 |
description | simple cross-platform ble client library based on btleplug |
homepage | https://github.com/MnlPhlp/blec |
repository | https://github.com/MnlPhlp/blec |
max_upload_size | |
id | 918619 |
size | 38,103 |
A simple cross-platform BLE Client library based on the awesome btleplug library
This mainly wraps btleplug methods in way that is easier to use if you just need one active connection. It also deals with setup on the different platforms. Most of that setup was taken from flutter_btleplug.
My use case and the reason I created this is for simple communication in an Android app created in Flutter with business logic in Rust.
// initialize library
blec::init();
// scan for available devices
// either use channel to receive devices when discovered
let (tx,rx) = mpsc::channel(1);
blec::discover(tx,1000);
// or use discover_blocking() or discover_async() to receive Vec with devices after timeout
let devices = blec::discover_blocking(1000);
// get address of wanted device and call connect
// you also have to pass the wanted service and characteristics UUIDs
// a callback called on disconnect is optional
let adr = devices[0].address;
blec::connect(adr,<service UUID>, <charac UUIDs>, None / Some(disconnect callback));
// after this you can send/receive data to/from the characteristics
// send
blec::send_data(<charac UUID>, <data>);
// read
let data = blec::recv_data(<charac UUD>);
// listen for notification
// the callback gets called with the notification data when a notification is received
blec::subscribe(<charac UUID>, <callblack>);
// at the end you can disconnect
blec::disconnect()
In order to use this on android you need the Java part of jni-utils-rs and droidplug. Some more Information for Android setup is provided by the btleplug library this is based on.
See esp_wifi_setup_app for an example that uses this library in a flutter app to setup wifi credentials on a esp32.
The app part of that example can be used as basis for a flutter app using blec.