Hello,
I use a HTC Desire HD. I can send data from the phone but when the app recieves data the app closes with an unexpected error.
I try to send/recieve via Bluetooth serial port
Hope someone see can see some error in my code. Im am stucked.
I have tries to 'rem' the content of Sub AStreams_NewData (Buffer() As Byte) out.
The app still does down when the data recieved
I use a HTC Desire HD. I can send data from the phone but when the app recieves data the app closes with an unexpected error.
I try to send/recieve via Bluetooth serial port
Hope someone see can see some error in my code. Im am stucked.
I have tries to 'rem' the content of Sub AStreams_NewData (Buffer() As Byte) out.
The app still does down when the data recieved
B4X:
'Activity module
Sub Process_Globals
Dim Serial1 As Serial
Dim AStreams As AsyncStreams
Dim connected As Boolean
End Sub
Sub Globals
Dim btnSend As Button
Dim txtLog As EditText
Dim txtSend As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Serial1.Initialize("Serial1")
End If
Activity.LoadLayout("1")
Activity.AddMenuItem("Connect", "mnuConnect")
Activity.AddMenuItem("Disconnect", "mnuDisconnect")
End Sub
Sub Activity_Resume
If Serial1.IsEnabled = False Then
Msgbox("Please enable Bluetooth.", "")
Else
Serial1.Listen 'listen for incoming connections
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))
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 Serial1_Connected (Success As Boolean)
If Success Then
ToastMessageShow("Connected successfully", False)
AStreams.InitializePrefix(Serial1.InputStream, False, Serial1.OutputStream, "AStreams")
connected = True
Else
connected = False
Msgbox(LastException.Message, "Error connecting.")
End If
End Sub
Sub mnuDisconnect_Click
Serial1.Disconnect
connected = False
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
AStreams.Close
Serial1.Disconnect
End If
End Sub
Sub btnSend_Click
If AStreams.IsInitialized = False Then Return
If txtSend.Text.Length > 0 Then
Dim buffer() As Byte
buffer = txtSend.Text.GetBytes("UTF8")
AStreams.Write(buffer)
txtSend.SelectAll
txtLog.Text = txtSend.Text
Log("Sending: " & txtSend.Text)
End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
txtLog.Text = txtLog.Text & msg
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
End Sub
Last edited: