Android Question List add problem

giada

Member
Hi,
inside my Starter Ble service i add an element to a list every time i receive a ble data during a connection, sometimes i find duplicate element in the list and overwritten element.
What i'm doing wrong?

B4X:
Sub BLETemp_Ev_DataAvailable (ServiceId As String, Characteristics As Map)
    
    Dim bc As ByteConverter
    Dim datastring As String = ""
    Dim dataarray() As Byte
    Dim temperatura As Double
    Dim s As String
    Dim st1 As String
    Dim t As Int
    'Dim line As String
    'Dim t2 As TuplaTemp

    For Each k As String In Characteristics.Keys
        'B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,k)
    Next   
    If (k = BLEBatteryCharacteristic) Then       
        datastring = bc.HexFromBytes(Characteristics.Get(BLEBatteryCharacteristic))
        dataarray = bc.HexToBytes(datastring)
        t = CnvUnsignedByteToInt(dataarray(0))
        B4XPages.MainPage.ShowBatterytemp(t)
        B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,"Batteria Temp: " & t)
        B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,"BLETemp Set Notify Service=" & BLETempService & " Characteristic=" & BLETempCharacteristic)
        BLETemp.SetIndication(BLETempService,BLETempCharacteristic,True)       
    Else
        CounterTemp = 0
        datastring = bc.HexFromBytes(Characteristics.Get(BLETempCharacteristic))
        B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,"Data available " & datastring)
        dataarray = bc.HexToBytes(datastring)       
        'aggiungo controllo sulla lunghezza dei byte ricevuti perchè a volte crash e sembra che ne arrivino meno
        If dataarray.Length >= 3 Then       
            PrimaMisuraTemp = False
            temperatura = (CnvUnsignedByteToInt(dataarray(2)) *256 + CnvUnsignedByteToInt(dataarray(1))) / 100
            temperatura = Round2(temperatura,1)
            B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,"BLETemp ID =" & BLETempCharacteristic & "Data=" & datastring & " Temperatura = "  & temperatura)
            t = temperatura*10
            st1 = t
            s = temperatura
            'DataTempToWrite = "T," & (DateTime.Now/1000) & "," & st1
            DataTempToWrite.tipo = "T"
            DataTempToWrite.ts = DateTime.Now/1000
            DataTempToWrite.m1 = t
            DataTempToWrite.inviata = False
            NewTempMeasure = True           
            B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,"utente: " & B4XPages.MainPage.UtenteAttuale )
            B4XPages.MainPage.TemperatureList(B4XPages.MainPage.UtenteAttuale-1).Add(DataTempToWrite)
            B4XPages.MainPage.MyLog(Main.LIVELLO_DEBUG_BLE,"TEMP ADD  " & DataTempToWrite.ts)
            B4XPages.MainPage.UltimaMisuraDisponibile(B4XPages.MainPage.UtenteAttuale-1) = DataTempToWrite.ts*1000
            'aggiungo qui il redraw del grafico se sono su pagina grafici
            If B4XPages.GetManager.GetTopPage.id = "pgrafici" Then
                CallSub(B4XPages.MainPage.pag_grafici,"reloadGrafici")
            End If
            CallSub3(B4XPages.MainPage, "TmpDataAvailable", s, getOra)
            'aggiungo qui il codice per mandare le misure realtime
            If B4XPages.MainPage.VideocallInCorso = True Then
                B4XPages.MainPage.SendMisRealTime(DataTempToWrite)
            End If
        End If       
    End If   
End Sub

If i read here the list i find:

1.620306070526E9 T 361
1.620629141228E9 T 361
1.620760749928E9 T 352
1.620793453715E9 T 357
1.620847175604E9 T 352
1.620880162242E9 T 363
1.620886769528E9 T 356
1.62088748362E9 T 353
1.620896098243E9 T 361
1.620896304943E9 T 360
1.620911161558E9 T 365
1.620911161558E9 T 365
1.620911161558E9 T 365


But in the log only 1 add is present:

Discovering services.
13/05/21 15:05:53_969 BLETemp Connected
13/05/21 15:05:53_971 BLETemp Show Services
13/05/21 15:05:53_972 00001800-0000-1000-8000-00805f9b34fb
13/05/21 15:05:53_974 0000180a-0000-1000-8000-00805f9b34fb
13/05/21 15:05:53_975 0000fff0-0000-1000-8000-00805f9b34fb
13/05/21 15:05:53_976 00001809-0000-1000-8000-00805f9b34fb
13/05/21 15:05:53_977 0000180f-0000-1000-8000-00805f9b34fb
13/05/21 15:05:54_479 BLETemp EV Connected - ReadBattery
13/05/21 15:05:54_634 Batteria Temp: 100
13/05/21 15:05:54_635 BLETemp Set Notify Service=00001809-0000-1000-8000-00805f9b34fb Characteristic=00002a1c-0000-1000-8000-00805f9b34fb
Setting descriptor. Success = true
writing descriptor: true
13/05/21 15:05:56_487 Controllo appuntamenti
13/05/21 15:05:57_490 Reinit pop
13/05/21 15:05:57_492 Controllo mail
13/05/21 15:05:57_944 POP_ListCompleted FlagErroreMail=false
13/05/21 15:05:58_942 OXY stop-start scan
13/05/21 15:05:59_159 PRESS stop-start scan
13/05/21 15:05:59_397 TEMP stop-start scan
13/05/21 15:06:01_552 Data available 04450E00FE02
13/05/21 15:06:01_553 BLETemp ID =00002a1c-0000-1000-8000-00805f9b34fbData=04450E00FE02 Temperatura = 36.5
13/05/21 15:06:01_558 utente: 1
13/05/21 15:06:01_559 TEMP ADD 1.620911161558E9
 

giada

Member
i solved it: the problem was that i save data-available in a global var, now i have a local var (defined in the data-available event) and all is ok.
 
Upvote 0
Top