matfire / bluetooth_classic

MIT License
14 stars 12 forks source link

Sending HEX command #6

Open nmiglio opened 1 year ago

nmiglio commented 1 year ago

Hi!

What is the proper way to send an hex sequence after connecting to the device?

I get the connection and something is sent, but I get no reply from the device. Also when sending 5 bytes I get 8 bytes out in the logs: D/BluetoothSocket(11952): write out: android.net.LocalSocketImpl$SocketOutputStream@fdb975e length: 8

I guess I am doing something wrong.. This is the write:

var conf_cmd = String.fromCharCodes([ 0xfe, 0xfe, 0x42, 0x4c, 0xff]);
await _bluetoothClassicPlugin.write((conf_cmd));

Same sequence via serial terminal is working fine.

Thanks!

matfire commented 1 year ago

Hi, Do you mean the connected device does not interpret the message correctly? If so, I'll try to reproduce this with an esp32 as soon as I'm able to. As far as how the plugin works; it converts the string into a bitearray before sending it to the connected device (might explain the 8 bytes length, though it still seems weird)

nmiglio commented 1 year ago

I need to comunicate with the device using hex sequences. I'm not sure I'm preparing the messagein the right way. the write method takes a string as parameter and I need to send a sequence of hex values. I conver the message using fromCharCodes() but as the size of the sent message is differentfrom the expected (sending 5 bytes I get 8 as length) I guess there is something wrong in the conversion.. I'm clearly not a dart expert :-)

matfire commented 1 year ago

Mmmh, looks like I'd need to directly send bitearray to the native interface; that would probably solve your issue but would require a bit of an architecture overhaul, but it seems feasible. Don't have an ETA for it, though

nmiglio commented 1 year ago

Might have to be because String in Dart are represented as UTF-16 codes? Converting the string back to bytes would then add some extra values?

Maybe a version of write that can take raw bytes is easier to implement?

haoth2 commented 11 months ago

I encounter a similar issue; I need to send an array of bytes to the Bluetooth module, but there is no function available for me to accomplish that

matfire commented 10 months ago

Hey all, as you might have noticed from my responsiveness, I'm afraid I don't enough time to work on this library. I'll maintain it (for now), so I'm open to Pull Requests if anyone wants to implement this method :)