ITPNYU / p5.ble.js

A Javascript library that enables communication between BLE devices and a p5 sketch using Web Bluetooth API. https://itpnyu.github.io/p5ble-website.
MIT License
41 stars 29 forks source link

Writing string to characteristic? #11

Closed Zander-M closed 3 years ago

Zander-M commented 3 years ago

Hi, I'm wondering if you could provide a function to write a string to a characteristic? On line 104 you use the Uint8Array.of() function to parse the input value, in that case we can't write a string to a characteristic. Perhaps it can be changed to something like:

const bufferToSend = new Uint8Array(toUTF8Array(inputValue));

so we can send value to the device? If it's a design choice, could you please let me know the reason for this? Thank you!

ragrag commented 3 years ago

Faced this issue as well and used

const bufferToSend = new TextEncoder("utf-8").encode(inputValue);
yining1023 commented 3 years ago

Hi @Zander-M and @ragrag, thank you so much for your suggestions!

I updated the library to support writing strings from p5 sketch to Arduino. You can use p5.ble.js@0.0.7 to send string to Arduino:

<script src="https://unpkg.com/p5ble@0.0.7/dist/p5.ble.js" type="text/javascript"></script>

On the Arduino side, you might need to add this line to read the string: https://github.com/ITPNYU/p5.ble.js/blob/9de8a6b3e8153af9538e454fa436af23f7f4716c/examples/writeOneChar/arduino-sketches/write-one-char-ArduinoBLE/write-one-char-ArduinoBLE.ino#L70

Here is a demo video about how to send strings to Arduino: https://www.loom.com/share/7a179dea008c420dace27900baa9c444 Here are the p5 sketch and the Arduino code used in the demo video.

Thank you both for using p5.ble.js and please let us know if you have other feedback!

ragrag commented 3 years ago

@yining1023 Thank you so much for that!

Zander-M commented 3 years ago

Thank you so much! This helps a lot! <3