Android Question BLE - ESP32 Mongoose OS

Josias

Member
Licensed User
Longtime User
Hi Guys,
I've been playing with B4A, ESP32, using Mongoose OS and BLE.

According to Mongoose OS I need to write the length of the packet that I want to transmit into a specific characteristic. The length should be a 32bit integer. I am not sure how to format the length correctly. I've attached Java code that I know works. Can someone tell me how to format it in B4A?

byte b[] = {0x00, 0x00, 0x00, ((byte) Length)};
length_tx_Characteristic.setValue(b);
// Write the length characteristic
mBluetoothLEService.writeCharacteristic(length_tx_Characteristic);

I guess that I will be using the following basic code to send it:
manager.WriteData("5f6d4f53-5f52-5043-5f53-56435f49445f", "5f6d4f53-5f52-5043-5f74-785f63746c5f", <4 bytes length>)
 

Josias

Member
Licensed User
Longtime User
For those interested, the following code will successfully build a message and call a remote procure call (RPC) in Mongoose OS:

Mgos OS function definition:
mg_rpc_add_handler(c, "Control", "{state: %d}", cb_BleButton, NULL);



Sub Globals
Dim bc As ByteConverter

' Service ID
Dim Serv_UUID As String = "5f6d4f53-5f52-5043-5f53-56435f49445f" '_mOS_RPC_SVC_ID_ -
' Attributes for above Service ID
Dim Data_Atrib As String = "5f6d4f53-5f52-5043-5f64-6174615f5f5f" ' _mOS_RPC_data___ - a r/w attribute used to submit frames for tx to the device and read out frames from the device.
Dim TxCtl_Atrib As String = "5f6d4f53-5f52-5043-5f74-785f63746c5f" ' _mOS_RPC_tx_ctl_ - a write-only attribute. Before sending a frame expected length of the frame Is submitted As a big-endian 32-Bit number (so, For a 100 byte frame bytes 00 00 00 64
Dim RxCtl_Atrib As String = "5f6d4f53-5f52-5043-5f72-785f63746c5f" ' _mOS_RPC_rx_ctl_ - a read/notify attribute. It returns the length of the frame that device wishes To transmit As a big-endian 32-Bit number.
End Sub

' Build the message in the expected format and send it.
Public Sub BLE_SendMsg(RpcName As String, ArgsName As String, Val As Int) As Byte()
Dim res() As Byte
Dim PacketLen As Int
Dim Ints(0) As Int
Dim Bytes(0) As Byte
Dim Packet As String
Dim TxLen As Int
Dim Indx_S As Int
bc.LittleEndian = False 'big-endian 32-Bit number (so, For a 100 byte frame bytes 00 00 00 64)
'Example Str = {"method":"Control","args":{"state":"0"}}
Packet = "{""method"":""" & RpcName & """,""args"":{""" & ArgsName & """:""" & Val & """}}"
Msgbox(Packet & ":" & Packet.Length, "BLE TxMsg")
PacketLen = Packet.Length

Indx_S = 0
TxLen = 0
' Write Message Length 32 bits to the TX_CONTROL Attribute
' byte b[] = {0x00, 0x00, 0x00, ((byte) Length)};
' length_tx_Characteristic.setValue(b);
Ints = Array As Int(PacketLen)
Bytes = bc.IntsToBytes(Ints)
Starter.manager.WriteData(Serv_UUID, TxCtl_Atrib, Bytes)
' Write Message in 20 byte chunks
Do While Indx_S <> PacketLen
If Indx_S + 20 < PacketLen Then
'more packets to send
TxLen = 20
Else
' last packets
TxLen = PacketLen - Indx_S
End If
'Msgbox(Packet.Substring2(Indx_S,TxLen + Indx_S),"Start: " & Indx_S & " : " & TxLen)

' Write to the DATA attribute
Dim msg As String
msg = Packet.Substring2(Indx_S,TxLen + Indx_S)
Dim data() As Byte = bc.StringToBytes(msg, "UTF8")
Starter.manager.WriteData(Serv_UUID, Data_Atrib, data)

' calculate next index
Indx_S = Indx_S + TxLen
Loop


Return res 'need to return something useful here
End Sub
 
Upvote 0
Top