Android Question BLE2 + writedata

dpetrinic

Member
Licensed User
Hi to all,
trying to send string message to micro:bit "ALARM ON\n", or any other message message written in EditText.
According to documentation command should be
.writedata(serviceID ,characteristicID, byte)
micro:bit is working OK, it is tested with app inventor application but I wish to have app running as service, so I decide to do it with B4A.

here is a part of code which should send data over BTLE, but nothing happen.

B4X:
SUB
   Dim slovo_b(1) As Byte   
   poruka="ALARM ON"
    duzina=poruka.Length
    Dim serviceID As String="6e400001-b5a3-f393-e0a9-e50e24dcca9e"
    Dim RX_char As String="6e400002-b5a3-f393-e0a9-e50e24dcca9e"
   
    For data_byte= 0 To duzina-1
        slovo = poruka.SubString2(data_byte,data_byte+1)
        slovo_b(0)=slovo.GetBytes("UTF8")
       
        Starter.manager.WriteData(serviceID ,TX_char,slovo_b)
   
    Next 
       slovo_b()=CRLF
       Starter.manager.WriteData(serviceID ,TX_char,slovo_b) 
end sub
 

DonManfred

Expert
Licensed User
you are defining RX_char but you are using TX_char!?
 
Upvote 0

dpetrinic

Member
Licensed User
Hi DonManfred,
thanks for reply. Part of software is just copied as small routine to be able to send it here on forum and I forget to put TX_char too.

B4X:
Dim TX_char As String="6e400003-b5a3-f393-e0a9-e50e24dcca9e"
 
Upvote 0

dpetrinic

Member
Licensed User
Hi Erel,
thanks for answer.
There is nothing received on MICRO:BIT. In debug log I got retries:4 when I sand on the way in my program, but with data created on the way you wrote, nothing. micro:bit is paired and connected with my phone as I can read all characteristics from the unit.

thanks for helping me,
Damir
 
Upvote 0

dpetrinic

Member
Licensed User
Hi Erel,
after I change bluetooth received new line as "#" in microbit program I can send a message and commands to microbit.
Please find link for documentation about bluetooth interface for micro:bit board.

https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html

B4X:
        poruka="ALARM ON" & "#"
        duzina=poruka.length
   
      For data_byte= 0 To duzina-1
        slovo = poruka.SubString2(data_byte,data_byte+1)
        slovo_b=slovo.GetBytes("UTF8")
        Starter.manager.WriteData(serviceID ,TX_char,slovo_b) ' slovo.GetBytes("UTF8")
         Next
This is command to armed alarm. Purpose of overall program is to show kids in school how to use mobile and cheap hardware to control small alarm, and maybe control of LED light. I will need also to receive STATUS of unit by sending STATUS?# command. I got this unit last week, so I am still looking for possibilities.
Thanks,
Damir
 
Upvote 0

dpetrinic

Member
Licensed User
Hi to all,

write code from phone to micro:bit is ok now, I can send message and commands. There is stil problem with end of line decoding on micro:bit but I found waz around, and instead of CRLF 0x13,0x10 or LF=0x10 I use #.

There is no SET Notify for UART, so I put timer to tick each 2 seconds and read characteristics . Than I found that READ2(UUID,RX_char) not working properly, there is error message, but READ(Rx_char) working and I receive some data in SUB manager_dataavailable, but that data I can not convert to data I sending from unit.
On micro:bit I receive STATUS?# , and answer should be STATUS=1# or STATUS=0# depend about status of ALARM on micro:bit.

after this part of program:
B4X:
Sub DataAvailable (Service As String, Characteristics As Map)
    Dim p As String
    Dim data1() As Byte
     
    For i = 0 To Characteristics.Size - 1
            Log("Value: " & Characteristics.GetValueAt(0))
    Next

    Log("MAIN DATAAVAILABLE : " & Service & "," &  Characteristics)
       For Each id As String In Characteristics.Keys
                  Log("Value: " & Characteristics.GetValueAt(0))
       Next
End Sub
i got this lines in log:
MAIN DATAAVAILABLE : 6e400001-b5a3-f393-e0a9-e50e24dcca9e,(MyMap) {6e400002-b5a3-f393-e0a9-e50e24dcca9e=[B@30ff5cfb, 6e400003-b5a3-f393-e0a9-e50e24dcca9e=[B@10314118}
Value: [B@30ff5cfb

Then I found on https://www.b4x.com/android/forum/threads/ble2-read-characteristics.74417/
some explanation about that conversion, but after I try to implement same it is not working.

How to convert data to be understandable?
I receive correct data from micro:bit on APP inventor aplication, so I am sure that micro:bit sending correct data.

Thanks,
Damir
 
Upvote 0

dpetrinic

Member
Licensed User
Hi Erel,
thanks for your answer.
this is log :

main data available ------------------START-------------------------------
6e400002-b5a3-f393-e0a9-e50e24dcca9e:
6e400003-b5a3-f393-e0a9-e50e24dcca9e:
main data available ------------------STOP-------------------------------

and this is sub:
B4X:
Sub DataAvailable (Service As String, Characteristics As Map)
    Dim p As String
    Dim data1() As Byte
   
    Dim bc As ByteConverter 'ByteConverter library
        Log("main data available ------------------START-------------------------------")
   
   
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log(key & ": " & bc.HexFromBytes(b))
        Log(key & ": " & BytesToString(b, 0, b.Length, "UTF8"))
    Next
    Log("main data available ------------------STOP-------------------------------")
   
End Sub
Looks that bc.library (ByteConverter (version 1.10)) not produce data properly. (which I doubt!)

Thanks,
Damir

I also notice that sometimes after reset micro:bit UART service not run at all (still searching info on net)
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Upvote 0

dpetrinic

Member
Licensed User
Hi Erel,
you right, after a days of searching, I found that without RX_characteristic SetIndication(UART_serviceID ,RX_char,True) there is no data. I was expecting data on Notify, but they not use it.
Is is OK now. I got data from and to unit in order.
What I done is small project with micro:bit to be alarm unit with communication to phone by sending status and to be able to receive command ON/OFF as well as display short message from PHONE to micro:bit matrix. Alarm condition is when someone move or just shake micro:bit.
Now I need to clean project from not necessary data and I will publish it here for anybody else to use it.
once again THANKS EREL and everyone else for helping me.
Damir
 
Upvote 0

dpetrinic

Member
Licensed User
Where does serviceidb and tx_char came from?

Hi BarryW,
those two variable strings are in Globals , and description for them come from MICRO:BIT Bluetooth documentation.

Program is working now, but there is still problem with characteristics discover for UART SERVICE. Same problem I have with APP INVENTOR, so I am still waiting for answer from BBC BT developer.

Regards,
Damir
P.S. sorry I did not respond earlier. I was out on sea without internet.
 
Upvote 0
Top