Android Question data hangs when reading and writing using ble2 library

djalltra

Member
hi everyone please I need your help with this code I'm currently using esp32 to send and receive data using ble2 library, I'm able to obtain the data but when I send the data it stops receiving but the esp32 is receiving the data and I'm able to toggle some less on the esp32 here is my code

ble code:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private btnDisconnect As B4XView
    Private btnScan As B4XView
    Private lblDeviceStatus As B4XView
    Private lblState As B4XView

    #if B4A
    Private manager As BleManager2
    Private rp As RuntimePermissions
    #else if B4i
    Private manager As BleManager
    #end if
    Private currentStateText As String = "UNKNOWN"
    Private currentState As Int
    Private connected As Boolean = False
    Private ConnectedName As String
    Private ConnectedServices As List
    Private pbScan As B4XLoadingIndicator
    Private Btnled As B4XView
    Private btnFan As B4XView
    Private btnLight As B4XView
    Private Service_uuid, ReadChar , WriteChar As String
    Private messagesToSend As List
    Private LabelTemp As B4XView
    Private LabelHum As B4XView
    Private tmr1 As Timer
    Dim BleData() As Byte
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

    Service_uuid = "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
    ReadChar = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
    WriteChar = "0193855f-c513-7120-a04d-68eda1788772"
    messagesToSend.Initialize
    ConnectedServices.Initialize
    tmr1.Initialize("Timer1", 300)
    tmr1.Enabled = True
End Sub


'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("1")
    'B4XPages.SetTitle(Me, "BLE ESP32")
    manager.Initialize("manager")
    StateChanged
End Sub


'when page is first loaded we check the state of the bluetooth
Public Sub StateChanged
    lblState.Text = currentStateText
    If connected Then
        lblDeviceStatus.Text = "Connected: " & ConnectedName
    Else
        lblDeviceStatus.Text = "Not connected"
    End If
    btnDisconnect.Enabled = connected
    
    btnScan.Enabled = Not(connected)
    pbScan.Hide  'hide progressbar
    btnScan.Enabled = (currentState = manager.STATE_POWERED_ON) And connected = False
End Sub


'WE REQUEST PHONE PERMISSIONS WHEN SCAN BUTTONIS CLICKED
Sub btnScan_Click
    #if B4A
    'Don't forget to add permission to manifest
    Dim Permissions As List
    Dim phone As Phone
    'we check the android sdk for the device
    If phone.SdkVersion >= 31 Then    'CHECK SDK VERSION
        Permissions = Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", rp.PERMISSION_ACCESS_FINE_LOCATION)
    Else
        Permissions = Array(rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If
    For Each per As String In Permissions        'Loop through all permissions
        rp.CheckAndRequest(per)                        'check the specific permission has been granted
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)        'wait for permission result
        If Result = False Then        'if the permission waasnot granted
            ToastMessageShow("No permission: " & Permission, True)        'inform the user
            Return
        End If
    Next
    #end if
    pbScan.Show            'show the Loading Indicator
    StartScan                'start scanning for ble devices
End Sub


'we disconnect the BLE device in our case ESp32
Sub btnDisconnect_Click
    manager.Disconnect
    Manager_Disconnected
End Sub



Sub Timer1_Tick
    'Handle tick events
    If connected = True Then
        manager.ReadData2(Service_uuid , ReadChar)  'read service and charateristics uuid
    Else

    End If
End Sub


'MANAGES AND GIVES INFORMATION ON THE BLE DEVICE
Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    currentState = State
    StateChanged
End Sub

'Fires when a ble device is found is provide the device name ,id ,data and rssi
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    'display log for debugging purposes
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
    
    ConnectedName = Name  'assign connected name

    If ConnectedName <>Null And ConnectedName ="BossFlex_BLE" Then  'check if the name is not empty and it matches
        manager.StopScan        'stop the scan process
        Sleep(1000)        'wait for 1 sec
        manager.Connect2(Id,False)  'connect to the specific ID without auto connect
    End If       
    Log("connecting")            'show connection status

End Sub

'we check the state of the bluetooth ex on or off
Public Sub StartScan
    If manager.State <> manager.STATE_POWERED_ON Then
        MsgboxAsync("Not powered on.","Turn on Bluetooth")
    Else
        manager.Scan2(Null, False)        'start scan process     with scan2
    End If
End Sub


'when a BLE device is connected
Sub Manager_Connected (Services As List)
        Log("Connected")
    connected = True            'Set connection flag   
    ConnectedServices = Services  'we can get the services
    StateChanged
    'manager.ReadData2(Service_uuid , ReadChar)  'Get the data service and charateristics uuid

End Sub

Sub Manager_Disconnected
    Log("Disconnected")
    connected = False        'Set connection flag
    LabelTemp.Text=""
    LabelHum.Text=""
    StateChanged
End Sub


'handle data to be read
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    Dim sdata As String       
    Try
        BleData = Characteristics.Get(ReadChar)        'get data in the charateristics
        sdata  = BytesToString(BleData, 0, BleData.Length, "UTF8")    'decodes the given byte as a string
        LabelTemp.Text =sdata .SubString2(0,4)
        LabelHum.Text =sdata .SubString2(6, 10)
    Catch
        Log(LastException)
    End Try

End Sub


'Handle data Write
Sub Write(Mydata As String)
    Try
        manager.WriteData(Service_uuid, WriteChar , Mydata.GetBytes("UTF8") )
    Catch
        Log(LastException)
    End Try
End Sub


Private Sub Btnled_Click
    If Btnled.Text="Led off" Then
        Btnled.Text="Led on"
        Write("AON")
        Else
        Btnled.Text="Led off"
        Write("AOFF")
    End If
    
End Sub

Private Sub btnFan_Click
    If btnFan.Text="Fan off" Then
        btnFan.Text="Fan on"
        Write("BON")
    Else
        btnFan.Text="Fan off"
        Write("BOFF")
    End If
End Sub

Private Sub btnLight_Click
    If btnLight.Text="Lights off" Then
        btnLight.Text="Lights on"
        Write("CON")
    Else
        btnLight.Text="Lights off"
        Write("COFF")
    End If
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…