MnlPhlp / blec

simple cross-platform ble client library based on btleplug
Apache License 2.0
0 stars 1 forks source link

examples and documentation? #1

Open Gibbz opened 1 month ago

Gibbz commented 1 month ago

Hi,

Do you have any examples of documentation on using this?

MnlPhlp commented 1 month ago

I mainly used this for a university project that I can't share. What do you plan on using this for ? For linux and windows just using the library should be enough. For use on android you need the jni-utils and droidplug java packages on the android side (see bleplug readme) .

I will try to add some basic usage description to the readme and a flutter example using this.

The basic usage is as follows:

// 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()

Feel free to ask any questions that come up so I can start writing down some useful documentation

MnlPhlp commented 1 month ago

I just created the esp_wifi_setup_app repository as example. This will have part of the project used to setup wifi credentials on a esp32 as example of using this app.

Gibbz commented 1 month ago

Im writing an android app that connect sto an ebike controller I am building. My android app is using rust/slint/async so im looking for a simple way to get up and running. Was using btleplug anyway, but this looks like it does what i need. Sending some settings from my phone to the device. Reading values from the device when they are notified of change.

Is there a way to connect from device name instead of address? Or if the device is already paired, skip the connection part?

My mobile app is here: https://github.com/bit-shift-io/bike-aid/tree/main/bike-aid-app I just need to get it to work with the async.

MnlPhlp commented 1 month ago

In that case I can't really help you with how you get the java part of blec/btleplug working. I only used this with flutter on Android. If you already got btleplug working with android you should be able to just call the blec::init() function and use the other library calls. Does slint have a way to include Java code for the android app?

MnlPhlp commented 1 month ago

Sounds like a really interesting project

Gibbz commented 1 month ago

I've had it running on android. But as nothing is connected to the gui I haven't been able to check if it was working. I'm trying to figure out if I go with slint, or flutter.

Is there a way to use android logcat with rust?

Also it never asked for Bluetooth permissions. So I assume that needs to be setup somewhere too?

MnlPhlp commented 1 month ago

I don't know about android logcat. I always used flutter_logger to get logs from the log crate to flutter and log them there.

In Flutter you have to setup permissions in the AndroidManifest. I then used a flutter library that asks for those permissions when the app is started. I will push a working example with flutter to esp_wifi_setup_app in about an hour. Maybe it helps you to figure out how it could work with slint.

Gibbz commented 1 month ago

Okay thanks 👍

MnlPhlp commented 1 month ago

FYI The example is up

Gibbz commented 1 month ago

Ive tried running it on my android app. I get the following error when running ble::init(); error: Java vm not initialized

I assume I need to do this. But don't know how to do it with the crate. Do you have some info on this step?

https://github.com/deviceplug/btleplug?tab=readme-ov-file#android
MnlPhlp commented 1 month ago

For flutter you have an android folder with native code for the android build. There you have to load the rust library in the MainActivity.kt file

package com.example.esp_server_app

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    init {
        System.loadLibrary("rust_lib");
    }
}

You also need to include the android portion of btleplug and jni-utils. In Flutter this can be done by include the code in the java portion of the app. see here I don't know slint, but I think you will need somethin similar where you adjust the native Java/Kotlin part of the app