I'm connecting to an arduino via Adafruit's Bluefruit UART device, using distriqt's bluetooth ANE.
I can connect, find the UART service, find the characteristics etc.
However, when I send data to the device's tx characteristic, I don't get the expected responses - all updates give me zero length for the length of the rx characteristic's value.
I have the Bluefruit app and can see the correct responses when I send the commands via the UART messaging tool in there. I only get a zero length response if I send it a nonsense command.
This makes me wonder whether the data is being sent in the correct format. The AS3 code doing that is:
var value:ByteArray = new ByteArray();
value.writeUTFBytes( msg );
sendUpdateMsg("trying to write to tx - value: " + msg);
var readSuccess:Boolean = _peripheral.readValueForCharacteristic(_rxChannel);
sendUpdateMsg("trying to read from rx before tx - outcome: " + readSuccess);
var success:Boolean = _peripheral.writeValueForCharacteristic( _txChannel, value );
sendUpdateMsg("trying to write to tx - outcome: " + success);
The sendUpdateMsg
function simply writes messages to a log on the screen. When the rx characteristic is updated, the event hander is:
private function peripheral_characteristic_updatedHandler( event:CharacteristicEvent ):void
{
sendUpdateMsg( "peripheral characteristic updated: " + event.characteristic.uuid );
sendUpdateMsg( "length="+ event.characteristic.value.length.toString() );
sendUpdateMsg( "value="+ event.characteristic.value.readUTFBytes( event.characteristic.value.length ) );
}
This always outputs zero for the length and nothing for the value.
If I send "!D", for example, do I need to treat it differently from:
var value:ByteArray = new ByteArray();
value.writeUTFBytes( "!D" );
in order for it to be correctly received by the UART service?
I don't have immediate access to the arduino side of things - I can ask the developer to add some debugging for me - to echo back what it feels to be nonsense messages - but that's not going to be quick.
The issue was that I was missing a LF character (0x0A) on the end of the message.
This works:
var value:ByteArray = new ByteArray();
value.writeUTFBytes( msg );
if(value[value.length-1] != 0x0A) {
value.writeByte(0x0A);
}
... and allows me to only add the 0x0A if it's needed - dependent on whether the msg content has come from a text input or a coded String.