Android Question AsyncStreams with bluetooth bugs

mevial

Member
Licensed User
Longtime User
I want to interact android device with some hardware via bluetooth(hc-05 dongle). The hardware has text interface like a modem. I tried all 3 methods, but all not stable.

With variant from http://www.b4x.com/android/forum/threads/android-serial-tutorial.6908/#post-39900 I have hanging app, and dead bluetooth after hang.

While i use AsyncStreams, commands from phone mostly cutted to one byte, only somtime full command sends to hardware. AsyncStreamsText works same as AsyncStreams.

Last code is:

B4X:
Sub Process_Globals
    Dim Serial1 As Serial
    Dim TimerCmd As Timer
    Dim Connected As Boolean=False
    Dim AST As AsyncSteamsText
    Dim BAdmin As BluetoothAdmin
    Dim Ok As Boolean=False
    Dim Cmd As String
    Dim Cells(3),OldCells(3) As Int
    Dim CV(3,13) As Float
    Dim BC(3) As Int
    Dim BT(3) As Float

    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Label6 As Label
    Private ListView1 As ListView
    Private Progressbars(32) As ProgressBar
    Private Labels(32) As Label
    Private EditText1 As EditText
    Private EditText2 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim i As Int
    'Do not forget to load the layout file created with the visual designer. For example:
    If FirstTime Then
        Serial1.Initialize("Serial1")
        TimerCmd.Initialize("TimerCmd",1000)
        TimerCmd.Enabled=False
        For i=0 To 2
          Cells(i)=0:OldCells(i)=0
        Next
    End If
    Activity.LoadLayout("main")
    Activity.AddMenuItem("Connect","mnuConnect")
    Activity.AddMenuItem("Disconnect","mnuDisconnect")
End Sub

Sub Activity_Resume
    If Serial1.IsEnabled = False Then
        Msgbox("Please enable Bluetooth.", "")
    Else
        Serial1.StopListening
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
  If UserClosed Then
    Serial1.Disconnect
    ToastMessageShow("Disconnected", False)
  End If
End Sub

Sub mnuConnect_Click
Dim PairedDevices As Map
    PairedDevices = Serial1.GetPairedDevices
Dim L As List
    L.Initialize
    For i = 0 To PairedDevices.Size - 1
        L.Add(PairedDevices.GetKeyAt(i)) 'add the friendly name to the list
    Next
Dim res As Int
    res = InputList(L, "Choose device", -1) 'show list with paired devices
    If res <> DialogResponse.CANCEL Then
        Serial1.Connect3(PairedDevices.Get(L.Get(res)),1) 'convert the name to mac address
    End If
End Sub

Sub mnuDisconnect_Click
    Serial1.Disconnect
    ToastMessageShow("Disconnected", False)
    Connected=False
    TimerCmd.Enabled=False
    EditText2.Enabled=False
End Sub

Sub Serial1_Connected (Success As Boolean)
    If Success Then
        AST.Initialize(Me,"AST",Serial1.InputStream, Serial1.OutputStream)
        Connected = True
        EditText2.Enabled=True
        ToastMessageShow("Connected successfully", False)

        AST.Write("sbss 0" & Chr(13))
        'TimerCmd.Enabled = true

    Else
        Connected = False
        TimerCmd.Enabled = False
        Msgbox(LastException.Message, "Error connecting.")
    End If
End Sub

Sub EditText2_EnterPressed
    If Connected Then
        AST.Write(EditText2.Text & Chr(13))
    End If
End Sub

Sub TimerCmd_Tick
  Dim CellsAll,OldCellsAll,i,x,y As Int
    If Ok AND Connected Then
        Cmd="sbst 0" & Chr(13)
        AST.Write(Cmd)
        If (Cells(0)>0) AND (OldCells(0)=0) Then
          CellsAll=Cells(0)+Cells(1)+Cells(2)
          OldCellsAll=OldCells(0)+OldCells(1)+OldCells(2)
          If CellsAll>OldCellsAll Then
            x=0:y=0
            For i=OldCellsAll To CellsAll-1
              Progressbars(i).Initialize("ProgressBars")
              Activity.AddView(Progressbars(i),x*160dip+40dip,y*16dip,110dip,16dip)
              Progressbars(i).Progress=Rnd(1,100)
              Labels(i).Initialize("Labels")
              Activity.AddView(Labels(i),x*160dip,y*16dip,40dip,16dip)
              Labels(i).Text="Cell" & (i+1)
              y=y+1
              If y*16dip>Activity.Height-16dip Then
                y=0
                x=x+1
              End If
            Next
          End If
        End If
    End If
End Sub

Sub AST_NewText (Text As String)
    If Connected Then 
      If EditText1.Text.Length>1024 Then EditText1.Text=EditText1.Text.SubString(128)
      EditText1.Text = EditText1.Text & Text & CRLF
      EditText1.SelectionStart = EditText1.Text.Length
  End If
End Sub

Sub AST_Terminated
    ToastMessageShow("Stream terminated", False)
End Sub

Sub BAdm_StateChanged (NewState As Int, OldState As Int)
    If NewState = BAdmin.STATE_ON Then Serial1.ListenInsecure(BAdmin,1)
    ToastMessageShow("BT state changed", False)
End Sub

How can I stable sending commands to the hardware?
 

mevial

Member
Licensed User
Longtime User
Input from device is Ok, my parcer in the AStreams_NewData, or yor AST_NewText works the same. Don't works output.
Command
B4X:
AST.Write("sbss 0" & Chr(13))
frequently sends only "s" withour \r, and only sometime it sends full command. I can record the video.
 
Upvote 0

mevial

Member
Licensed User
Longtime User
Sorry, that was bad power wires between device and dongle, now all works excellent. Thank You for support and AST class, it's more useful than my parcer.
 
Upvote 0

mevial

Member
Licensed User
Longtime User
Yet another question. If app hangs with serial connected, serial connection will be inaccessible without reboot phone. How can I disconnect it?
 
Upvote 0

mevial

Member
Licensed User
Longtime User
For example - the app will be paused by other app, and after killed by memory manager. May be I need disconnect on each pause and reconnect after resume? In my case - killing app by pressing stop in the b4a debugger. May be I can make hook for this case, service for example, which can access to connected serial?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The Bluetooth should stay stable after the process is killed. However Bluetooth is not really stable on some devices.

If the process is killed by a task manager then there is nothing that you can do about it.

May be I need disconnect on each pause and reconnect after resume?
This is possible.

Another thing to try is to disable and reenable the Bluetooth with BluetoothAdmin.
 
Upvote 0

mevial

Member
Licensed User
Longtime User
Manual disable and reenable bluetooth don't helps in my 2 phones. I doubt something will be changed if I will use BluetoothAdmin for this.
 
Upvote 0
Top