Android Question How to change the SelectedValue fields in B4XPlusMinus Using a Byte From An Array

fredeady

Member
Licensed User
Longtime User
I am constructing a B4A BLE app that needs a number of B4XPlusMinus SelectedValue fields (5 total) to be updated from a byte array (5 bytes in length) that is sent from the BLE peripheral. I am having trouble converting the incoming bytes to objects that are acceptable to replace the current B4XPlusMinus SelectedValue fields. For instance, the first B4XPlusMinus entry is set to "default" 50 by my code (pmFoo.SelectedValue = 50). I receive a byte with the value of 0x40 from the BLE peripheral that needs to replace the "default" SelectedValue of 50 with 64. The idea is to display to the user updated values that are currently active in the BLE peripheral within the B4XPlusMinus view (< 50 > would change to < 64 > as displayed to the user).

The basic question is: How do I convert a byte to an object that I can place into the B4XPlusMinus SelectedValue field?

Thanks in advance for any pointers and/or suggestions.
 

fredeady

Member
Licensed User
Longtime User
B4X:
pmFoo.SelectedValue = YourByte

I guess that you tried it and it didn't work. Post more code.
Hi, Erel
Yes.. I tried that. I get an out of bounds exception error and the log bleeds red. pmFoo.SelectedValue = b(0) does not work and fires the exception.
Experimenting, I found that if I define a string (Global here, but I don't think that matters), such as Dim vals As String, and assign a value to it, vals = "64", I enter pmFoo.SelectedValue = vals and it works.
So, that tells me I need to feed it a string. The answer lies in converting my incoming byte to a string that holds the decimal value. For instance, I need to convert 0x40 to "64".
I'll work at that. Here's the current code that grabs the BLE data and pushes it into an array called b. The nus_xxx stuff is Nordic Uart Service Service and Characteristic definitions.

Thanks for the assistance, Erel.

B4X:
Sub DataAvailable (Service As String, Characteristics As Map)
    If Service = Starter.nus_sid Then
        For Each key As String In Characteristics.Keys
            If key = Starter.nus_txc Then
                b = Characteristics.Get(key) 'this works and loads array b with 5 hex bytes
                vals = "64" 'spawned in Process Globals - Dim vals As String
                pmSensitivity.SelectedValue = vals 'this works
                'The following array statements do not work and cause an out of bounds exception
                'pmSensitivity.SelectedValue = b(0)     '0x40
                'pmBarks.SelectedValue = b(1)            '0x03
                'pmBarkTime.SelectedValue = b(2)        '0x05
                'pmSprayDuration.SelectedValue = b(3)    '0x0E
                'pmToneDuration.SelectedValue = b(4)    '0x02
                Log (" ")
                Log("Key: " & key &", value: " & bc.HexFromBytes(b)) 'logs-> value: 4003050E02  (5 correct bytes)
                Log (" ")
            End If
        Next
    End If
End Sub
 
Upvote 0

fredeady

Member
Licensed User
Longtime User
HI, Erel
I figured it out.. It's a bit Caveman, but it works. To avoid the length=0 exception for vals, it is necessary to make sure the 5 bytes of Characteristic data is really there.
I'm open to suggestions as to how to slim up the parsing code.


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim bc As ByteConverter
    Dim b() As Byte
    Dim vals As String
    Dim s As String
    Dim v As Byte
End Sub

Sub DataAvailable (Service As String, Characteristics As Map)
    If Service = Starter.nus_sid Then
        For Each key As String In Characteristics.Keys
            If key = Starter.nus_txc Then
                b = Characteristics.Get(key)             'get 5 bytes from BLE peripheral
                vals = bc.HexFromBytes(b)                 'convert the 5 bytes to a string called vals
                If vals.Length > 0 Then                    'check for valid data from the BLE peripheral
                    s = vals.SubString2(0,2)            'extract 2 string characters - representing one byte
                    v = Bit.ParseInt(s,16)                'convert 2 hex string characters to decimal representation
                    pmSensitivity.SelectedValue = v        'load the B4XPlusMinus decimal string value shown to user
                    
                    s = vals.SubString2(2,4)
                    v = Bit.ParseInt(s,16)
                    pmBarks.SelectedValue = v
                    
                    s = vals.SubString2(4,6)
                    v = Bit.ParseInt(s,16)
                    pmBarkTime.SelectedValue = v
                    
                    s = vals.SubString2(6,8)
                    v = Bit.ParseInt(s,16)
                    pmSprayDuration.SelectedValue = v
                    
                    s = vals.SubString2(8,10)
                    v = Bit.ParseInt(s,16)
                    pmToneDuration.SelectedValue = v
                End If
            End If
        Next
    End If
End Sub
 
Upvote 0
Top