Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List Windows Mobile Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android Serial tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-15-2010, 01:26 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,859
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android Serial tutorial

This tutorial covers the Serial library. This library allows you to connect with other Bluetooth devices using virtual serial ports (RFCOMM).

An improved tutorial is available here: http://www.basic4ppc.com/forum/basic...-tutorial.html

The Serial library requires Android OS 2.0 or above (API level 5 or above).

We will build a simple chat example which allows two connected devices to send text messages.




We created a process global object named Serial1 of type Serial.
Usually it is a good idea to initialize process objects in Sub Activity_Create when FirstTime is True. This way the objects will be initialized exactly once.

Code:
Sub Activity_Create(FirstTime As Boolean)
    
If FirstTime Then
        Serial1.Initialize(
"Serial1")
        Timer1.Initialize(
"Timer1"200)
    
End If
    Activity.LoadLayout(
"1")
    Activity.AddMenuItem(
"Connect""mnuConnect")
    Activity.AddMenuItem(
"Disconnect""mnuDisconnect")
End Sub
The next Sub running is Activity_Resume:
Code:
Sub Activity_Resume
    
If Serial1.IsEnabled = False Then
        
Msgbox("Please enable Bluetooth.""")
    
Else
        Serial1.Listen 
'listen for incoming connections
    End If
End Sub
Here we are checking if the Bluetooth device is enabled. If it is not enabled we ask the user to enable it. Note that by putting this code in Activity_Resume (and not Activity_Create) this test will happen every time the activity is resumed. So if the user goes to the settings screen, enables the device and then returns to our application we will now know that the Bluetooth is enabled.

If the Bluetooth is enabled we start listening for incoming connections. This allows other devices to connect with our device. This is not required if you connect to a device that listens for connections (like external GPS for example).
Note that calling Listen more than once doesn't do anything. So we are safe calling it this way.

When the user presses on the Connect menu item we show the user the list of known paired devices. When the user clicks on a device name we fetch its MAC address from the map and connect to it:



Code:
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.Connect(PairedDevices.Get(l.Get(res))) 
'convert the name to mac address
    End If
End Sub
The connection is not established immediately. It will happen in the background. When the connection is established, the Connected event will be raised. A 'Success' parameter tells us if the connection is successful.
The Connected event can also be raised if an incoming connection is established.

Code:
Sub Serial1_Connected (Success As Boolean)
    
If Success Then
        
ToastMessageShow("Connected successfully"False)
        TextReader1.Initialize(Serial1.InputStream)
        TextWriter1.Initialize(Serial1.OutputStream)
        timer1.Enabled = 
True
        connected = 
True
    
Else
        connected = 
False
        Timer1.Enabled = 
False
        
Msgbox(LastException.Message, "Error connecting.")
    
End If
End Sub
If the connection was established successfully we can start the data transfer.
TextReader1 and TextWriter1 are process global object. We now initialize them using the serial stream. This will allow us to send and receive text over our newly created connection.
Timer1 is used to test whether there is incoming data (and read this data). Now we enable it and start listening.

If the connection is not successful we retrieve the exception and show it.

Sending messages - When the user presses on btnSend we send the text:
Code:
Sub btnSend_Click
    
If connected Then
        TextWriter1.WriteLine(txtSend.Text)
        TextWriter1.Flush
        txtSend.Text = 
""
    
End If
End Sub
'connected' is a variable that we use to know if we are currently connected.
Note that we call Flush after writing the text. This way we make sure that TextWriter doesn't buffer the text and sends it right away.

Receiving messages - Whenever the timer ticks we check if there is any data waiting to be read. If there is, we read it and add it to the large EditText:
Code:
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
TextReader.ReadLine is a blocking call. It waits till there is at least a single character to be read. Therefore we need to test TextReader.Ready if we don't want to block our application.

This application can also be used to connect with non-Android devices.
An external GPS for example:


The external GPS continuously sends its data as text.
I actually had quite an interesting conversation with the external GPS...

The program is attached.

Edit: It is recommended to use the new AsnycStreams object instead of polling the available bytes parameter with a timer. Using AsyncStreams is simpler and more reliable.
Attached Files
File Type: zip SerialExample.zip (6.3 KB, 2298 views)
Reply With Quote
  #2 (permalink)  
Old 12-15-2010, 02:57 PM
schimanski's Avatar
Senior Member
 
Join Date: Oct 2007
Location: Germany
Posts: 345
Default

Hello Erel! I think, that it is a very useful lib,thanks for that.

I have tow additional questions about that:

- The free market GPS-Software connects normally with the internal GPS- Chip. But what's happen, if I have an external GPS. Is in premium navigation-apps like copilot or navgear (I don't have one yet) an option for choosing the gps-serial-port?

- Can we only init a bluetooth-port with the serial-lib or is it possible to create two virtual ports on one device to send data from the first to the second like a virtual null modem emulator?

Rgds

schimanski
__________________
schimanski
--------------------------------------
Device: Motorola Defy, Samsung Galaxy Tab P1000, Motorola Razr, Dekstop: Asus Eee PC
Reply With Quote
  #3 (permalink)  
Old 12-15-2010, 04:18 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,859
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
But what's happen, if I have an external GPS. Is in premium navigation-apps like copilot or navgear (I don't have one yet) an option for choosing the gps-serial-port?
I don't know.

Quote:
Can we only init a bluetooth-port with the serial-lib or is it possible to create two virtual ports on one device to send data from the first to the second like a virtual null modem emulator?
You cannot use it this way.
Reply With Quote
  #4 (permalink)  
Old 12-23-2010, 07:59 AM
Cor Cor is offline
Senior Member
 
Join Date: May 2007
Posts: 499
Default Error: discovery failed java error

When trying to connect to a device which is not available but showed in the list as a device

i get an java error on device:
java.io.IOException:Service discovery failed
because it's trying to connect in background

try catch will not help

How to catch the exception?

Code:
Try
  Serial1.Connect(PairedDevices.Get(l.Get(res))) 
'convert the name to mac address
Catch
Msgbox(LastException.Message, "Error connecting.")
End Try
__________________
Best regards,
Cor de Visser, Netherlands, HTC Magic 2.2.1

Amazing Guitar Chords for Android
http://android.ready4music.com
Reply With Quote
  #5 (permalink)  
Old 12-23-2010, 08:23 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,859
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Are you sure that it is not the msgbox displaying the error?
Reply With Quote
  #6 (permalink)  
Old 12-23-2010, 08:53 AM
Cor Cor is offline
Senior Member
 
Join Date: May 2007
Posts: 499
Default

Your are correct, i was fooled by the java message?

It's is a messagebox

grCor
__________________
Best regards,
Cor de Visser, Netherlands, HTC Magic 2.2.1

Amazing Guitar Chords for Android
http://android.ready4music.com
Reply With Quote
  #7 (permalink)  
Old 01-31-2011, 02:12 PM
Newbie
 
Join Date: Jan 2011
Location: Vilnius
Posts: 1
Default Terminal sample works not good

Hi, I tested your Serial terminal.
Program finds bluetooth devices and connects to them.
The Hyper terminal on other computer shows received data, but
when i sending data to HTC, the "Serial termilal" software are a halts.
Yes sometimes some data are received, but after time apears timeout Error.

Firs time i instaled test software it was workinq fast about 3 minutes, but later halted.

where is the problem?
The paltform:
HTC desire, Android 2.2

Thank you
Reply With Quote
  #8 (permalink)  
Old 02-01-2011, 05:41 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,859
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Code:
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
This code reads all text until it finds an end of line character (chr(10)). Maybe you are sending a string without an end of line character?
Reply With Quote
  #9 (permalink)  
Old 02-03-2011, 12:26 PM
Newbie
 
Join Date: Jan 2011
Posts: 6
Default Unicode characters in TextReader/TextWriter

Hi,

I'm trying to send/recieve Greek characters (Unicode characters) with Serial1 object via TextReader1 and TextWriter1 but the app stop working on phone.

I used the Erel's example taken from this thread.
I have made the following changes in "Sub Serial1_Connected()"
From :
TextReader1.Initialize(Serial1.InputStream)
TextWriter1.Initialize(Serial1.OutputStream)
To:
TextReader1.Initialize2(Serial1.InputStream ,"UNICODE")
TextWriter1.Initialize2(Serial1.OutputStream, "UNICODE")

Code:
Sub Serial1_Connected (Success As Boolean)
    
If Success Then
        
ToastMessageShow("Connected successfully"False)
        TextReader1.Initialize2(Serial1.InputStream ,
"UNICODE")
        TextWriter1.Initialize2(Serial1.OutputStream, 
"UNICODE")
        timer1.Enabled = 
True
        connected = 
True
    
Else
        connected = 
False
        Timer1.Enabled = 
False
        
Msgbox(LastException.Message, "Error connecting.")
    
End If
End Sub
At the Encoding parametr of Initialize2, I tested values like UTF8,UTF7 and ANSI and all goes ok. But when i change to "UNICODE" the app halts (the previus Toastmessage remaines infinitely).

The only I want is to send Unicode characters via BlueTooth.
Any idea?
Reply With Quote
  #10 (permalink)  
Old 02-03-2011, 12:48 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,859
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should probably use UTF8. It is a Unicode encoding. I don't think that "UNICODE" is a valid value.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
android NDK support? Cor Bugs & wishlist 10 01-24-2011 03:32 AM
android update moster67 Basic4android Updates and Questions 1 12-07-2010 12:39 PM
Basic4ppc for Android, what do you think? Erel Chit Chat 61 10-09-2010 10:03 AM
Android me1... linum Chit Chat 4 06-16-2010 07:12 PM
Support for Android epsharp Basic4ppc Wishlist 3 05-14-2009 07:44 PM


All times are GMT. The time now is 09:38 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0