kai-morich / SimpleBluetoothLeTerminal

Android terminal app for Bluetooth LE devices using custom serial profiles
MIT License
285 stars 111 forks source link

ESP32 SPP Support Merge Request #28

Open BrianAtDocumentedDesigns opened 3 years ago

BrianAtDocumentedDesigns commented 3 years ago

Kai Thanks for writing this. I don't know how to contact you (I am pretty new to github, etc) but I cam across your code while looking for an example BLE application so I could add BLE to my little project.

My hardware is ESP32-C3 based and your code does not support ESP32 so I managed to port it over and got it working with the esp32_spp_server.cpp demo which espressif provides. (see https://github.com/BrianAtDocumentedDesigns/SimpleBluetoothLeTerminal/tree/ESP32_C3_SPP_Support).

I am pretty sure the ESP32 spp server does not implement flow control so I set

        writeCredits = Integer.MAX_VALUE;

as a sort of a kludge.

I know I am not much of a programmer but I thought I'd let you know in case you were interested in incorporating support for the popular ESP32 family into your code.

I will probably add flow control to the ESP32 code when I make some progress on my project.

Thanks again

Brian Piccioni Documented Designs.

kai-morich commented 3 years ago

SPP is a protocol defined for bluetooth classic. For BLE there is no standard, but various vendors have predefined UUIDs for a serial like protocol. I couldn't find esp32_spp_server.cpp.

For ESP32 i know this variant https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/examples/BLE_uart/BLE_uart.ino which works with this app by simply changing UUIDs.

mrx23dot commented 1 year ago

Please add ESP32 kind of BLE SPP, it would be very useful.

AT+BLESPPCFG?
+BLESPPCFG:0,0,0,0,1

+BLESPPCFG:<tx_service_index>,<tx_char_index>,<rx_service_index>,<rx_char_index>,<auto_conn>

• <tx_service_index>: tx service’s index. It can be fetched with command AT+BLEGATTCPRIMSRV=<conn_index> and AT+BLEGATTSSRV?.
• <tx_char_index>: tx characteristic’s index. It can be fetched with command AT+BLEGATTCCHAR=<conn_index>,<srv_index> and AT+BLEGATTSCHAR?.
• <rx_service_index>: rx service’s index. It can be fetched with command AT+BLEGATTCPRIMSRV=<conn_index> and AT+BLEGATTSSRV?.
• <rx_char_index>: rx characteristic’s index. It can be fetched with command AT+BLEGATTCCHAR=<conn_index>,<srv_index> and AT+BLEGATTSCHAR?.

I think this fully describes ESP32 server:
AT+BLEGATTSSRV?
+BLEGATTSSRV:<srv_index>,<start>,<srv_uuid>,<srv_type>
+BLEGATTSSRV:1,1,0xA002,primary service

+BLEGATTSSRV:2,1,0xA003,primary service

image image

In Bluetooth LE client, the property of tx characteristic must be write with response or write without response, and the property of rx characteristic must be indicate or notify. [ESP32-AT][v2.4.0.0]User-Guide.pdf Hardcoding those 4numbers is better than nothing.

kai-morich commented 1 year ago

does it work as 'Custom' device in https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal app?

mrx23dot commented 1 year ago

wow, I manage to get it working with their default AT firmware. It can send and receive in transparent mode.

Service 
    0000a002-0000-1000-8000-00805f9b34fb
Write 
    0000c304-0000-1000-8000-00805f9b34fb
Read 
    0000c306-0000-1000-8000-00805f9b34fb
    uses indicate method

could we add this as known profile? These don't change, I have tested on a 2nd different esp32.

On ESP32 side it requires ESP-AT SW and:

AT+BLEINIT=2
AT+BLEGATTSSRVCRE
AT+BLEGATTSSRVSTART
AT+BLEADDR?
AT+BLESPPCFG=1,1,7,1,5
AT+BLEADVSTART
//connect
AT+BLESPP
>hello
thanhphongK19 commented 11 months ago

hi Kai-morich, I am using esp32 with platform idf. I run gatt-server on esp32 and run your source app. Your app display debug terminal: "discoverServices() - device: 48:55:19:BD:D7:A6 (My mac address)" But my device can not connect with your app because it face to trouble "no serial profile found"

mrx23dot commented 11 months ago

Try the custom profile I wrote above, it works for me.

thanhphongK19 commented 11 months ago

Sorry, but i don't understand. You can show me how you add custom profile in source code android. Thank

mrx23dot commented 11 months ago

There is an option in app's menu to add custom profiles, then you connect and it selects the profile.

Not in source code. But if you have access to that you could add it as built in profile for us, just search for other existing profiles.

thanhphongK19 commented 11 months ago

Thank you

Luxin812 commented 6 months ago

@mrx23dot ESP-AT should have flow control function. You can set it by setting the AT+UART_CUR command.