Android Tutorial Android Serial tutorial

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hi ,
I downloaded serialExemple but i can't connect to my device (BT to rs232 adapter). Can anybody tell me what's wrong ?
 

Attachments

  • Screenshot_2013-10-03-14-38-22.png
    32.4 KB · Views: 542
  • Screenshot_2013-10-03-14-38-36.png
    44.8 KB · Views: 526

StarinschiAndrei

Active Member
Licensed User
Longtime User
I try to use an BT to RS232 adapter to communicate with a scale. To return the weight i must send "W" or "P" on rs232. I tried with the code below
i checked the scale with hyperteminal and it answered if i send both characters. What's wrong in this code ?
Sub btnSend_Click
If connected Then
TextWriter1.WriteLine(txtSend.Text&Chr(10)&Chr(13))
'TextWriter1.WriteLine(AsciiToHex(txtSend.Text))
TextWriter1.Flush
txtSend.Text = ""
End If
End Sub

Sub AsciiToHex (a As String) As String
Dim bc As ByteConverter
Return bc.HexFromBytes(a.GetBytes("ASCII"))
End Sub

Sub Timer1_Tick
If connected Then
If TextReader1.Ready Then 'check if there is any data waiting to be read
txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF
txtLog.SelectionStart = txtLog.Text.Length
End If
End If
End Sub
i checked the scale with hyperteminal and it answered if i send both characters.
 
Last edited:

StarinschiAndrei

Active Member
Licensed User
Longtime User
It is always better to check the logs for the full error message.

Are you sure that the rs232 adapter is listening on the standard UUID?
Previous i used to test this application a samsung galaxy tab2 ..... than i tried with another device samsung galaxy S3 and with this work fine. i don't know why the tab got me the error message.
 

BluSky76

Member
Licensed User
Longtime User
Library Serial 1.23

Android 4.1.1
Kernel 3.4.0

Error:
Serial1.Initialize("Serial1")
java.io.IOException:[JSR82] accept: Connection is not created (failed or aborted)

OK:
Serial1.Initialize("Serial1")
Android 4.0.4
Kernel 2.6.32.9

On Android 4.1.1 gives me error while working on android 4.0.4.
Any idea?
 

BluSky76

Member
Licensed User
Longtime User
Erel, if I connect two devices with androit 4.0.4 or 4.2.1 there are no problems but if the dispoostivo uses Android 4.1, 4.1.1 or 4.2.0 above error occurs. When it comes to the command:

Serial1.Initialize ("Serial1")

returns error

java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted)

I believe that the serial 1.23 to 1.20 are not compatible androit 4.1 and 4.2 or Android, and has problems to manage bluetooth devices
 

BluSky76

Member
Licensed User
Longtime User
Sub Activity_Create(FirstTime AsBoolean)If FirstTime Then
Serial1.Initialize("Serial1")



Timer1.Initialize("Timer1", 200)EndIf
Activity.LoadLayout("1")
Activity.AddMenuItem("Connect", "mnuConnect")
Activity.AddMenuItem("Disconnect", "mnuDisconnect"
[FONT=verdana, geneva, lucida, lucida grande, arial, helvetica, sans-serif]End Sub



Sub Serial1_Connected (Success As Boolean)
If Success Then
ToastMessageShow("connected", False)
TextReader1.Initialize(Serial1.InputStream)
TextWriter1.Initialize(Serial1.OutputStream)
Timer1.Enabled = True
connected = True
Else
connected = False
Timer1.Enabled = False
Log(LastException.Message) *
End If
End Sub
[/FONT]

*

--------------------------------------------------------------------------------------------------

[FONT=verdana, geneva, lucida, lucida grande, arial, helvetica, sans-serif]** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted).
java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted).
java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted).
java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted).
java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted).
java.io.IOException: [JSR82] accept: Connection is not created (failed or aborted).
** Activity (main) Pause, UserClosed = true **
----------------------------------------------------------------------------------------------------

This error occurs on devices with Android 4.1 and 4.1.1 do not show up on devices with Android 4.0.4 and 4.2.1 i tested

[/FONT]
 

BluSky76

Member
Licensed User
Longtime User
Erel,

after updating the device firmware from 4.1.1 to 4.1.2 the problem does not occur.

I have a problem with Astreams_NewData. I receive the data correctly, but I have trouble sending multiple strings.

sub ButtonSend_Click

(first string)
send.astreams.Write (strWrite.GetBytes ("UTF8"))

do while msg.Contains ("TKZ01") = FALSE
DoEvents
loop

if the program executes the DO WHILE loop is not running Astreams_NewData.
Before sending a second string I want to know if I have received a particular string.


(second string)
send.astreams.Write (strWrite.GetBytes ("UTF8"))
end Sub
 

BluSky76

Member
Licensed User
Longtime User
Erel,

you have a solution?

because sending a command astreams.Write the program waits for the end of subsequent instructions before activating Astreams_NewData.

I have to send various commands astreams.Write and each one corresponds to a specific response. How do I read the response before sending the next command?
 

Felix Maria

Member
Licensed User
Longtime User
Hi Erel,

I have a bluetooth RFID card reader.
This program connects perfectly. But when the RFID card is flashed , the program hangs or stops responding
in the below line

B4X:
txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF

I tried replacing the .ReadLine with the others like .ReadAll etc... But same effect. Any Advice on how to read these RFID Cards? These card may or may not have a chArACTER(10) appended to the string, But the client has already printed the cards. Please advice on how to proceed.. Thank You. Felix Maria
 

BluSky76

Member
Licensed User
Longtime User
Ernel,
There are big problems with the command, even with a module service active,

astreams.Write (strWrite.GetBytes ("UTF8"))

Using this command the sub:

Sub Astreams_NewData (Buffer () As Byte)

is not performed if after the command astreams.Write (strWrite.GetBytes ("UTF8")) there are other instructions.
So before you can make all the instructions and then activates Astreams_NewData.

Do you have a solution?
 

BluSky76

Member
Licensed User
Longtime User
Erel,

I try to explain.

I have to click a button and has to start a cycle of exchanging data with a network device or bluetooth. When I send the first command then follow automatically exchanges. the exchange is based on the response of the device.

sub send_click

first shipment
astreams.Write (writeSend.GetBytes ("UTF8"))

I check the response response of the device and sending a new string
(does not work here Astreams_NewData is activated after executing all the commands of the cycle button)

select case msg
case "AT"
astreams.Write ("OK". GetBytes ("UTF8"))
case "ATZ"
------------------
astreams.Write ("AZR". GetBytes ("UTF8"))
end select
end sub

astreams.Write ("ATZQ". GetBytes ("UTF8"))

msgbox ("Result:" & msg)

after the first shipment jumps directly to msgbox ("Result:" & msg) after pressing OK on the msgbox comes into operation Astreams_NewData.

So how to send multiple commands in a row and wait for the answer to each one?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Don't show modal dialogs when using AsyncStreams. It may change the order of the received messages.

2. Please use [ code ] [ /code ] tags (without spaces) when posting code.

3. You will need to manage the current state.
You should check the response in Astreams_NewData

After you read the response you should send the next message. You can add the messages to a List and then send the first, read the response and send the next one.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…