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 B4P 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 Bluetooth / BluetoothAdmin Tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2012, 02:40 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android Bluetooth / BluetoothAdmin Tutorial

This tutorial is an improved version of the previous Serial tutorial.
We will create a chat example between two Android devices.

Using BluetoothAdmin we search for in-range devices and then allow the user to connect to a device. If the device were not paired before then a dialog will show to confirm the pairing process.

The first activity includes two buttons:



"Search for devices" button will start a discovery process that takes about 12 seconds.

"Make device discoverable" button does two things. First it sends an Intent that makes the device discoverable for 300 seconds. It then calls Serial1.Listen to listen for incoming connections.

If the connection is established successfully then we start the second activity:



In the second activity we use AsyncStreams to send and receive messages.
As we use AsyncStreams in both sides of the connection we can easily use "prefix mode". In this mode we always receive complete messages.

The code is attached.
Attached Files
File Type: zip Bluetooth.zip (8.8 KB, 458 views)
Reply With Quote
  #2 (permalink)  
Old 02-09-2012, 12:09 AM
Junior Member
 
Join Date: Sep 2007
Posts: 23
Default Two different colours for received and sent text

That looks very nice.
Is it possible to show "txtLog" with two different colours, for instance received text in red colour and sent text in blue colour?
Is it also possible to show a time stamp ("12:34:56.789" or similar) before the text "You" and "Me"?
Reply With Quote
  #3 (permalink)  
Old 02-09-2012, 06:00 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

It is better to start a new thread for these questions. You can use the RichString library to change the colors. Adding the time stamp should be pretty simple. You should add it in Sub LogMessage.
Reply With Quote
  #4 (permalink)  
Old 02-22-2012, 12:33 AM
walterf25's Avatar
Basic4ppc Veteran
 
Join Date: Sep 2011
Posts: 209
Default Bluetooth

Hi Erel, i tried your example for bluetooth communication, and i must say is very good, however i'm using a RN-42 bluetooth i bought from sparkfun electronics, i'm able to connect to it via my HTC Evo shift i wrote a small program in visual studio to receive and send the text strings, i can receive the strings ok sent from my phone to my program written in visual studio, but when i send a string back to the bluetooth module, your program shuts down giving me a foreclosed error, i have tried this before with app inventor and it should work ok, the module is connected to my computer through a serial port, i need help figuring out where this is going wrong! can you maybe give me some tips.

Thanks,
Walter
Reply With Quote
  #5 (permalink)  
Old 02-22-2012, 12:00 PM
Newbie
 
Join Date: Jan 2012
Posts: 4
Default

I guess you dont send the prefix characters from you VS program which is needed when using prefix mode. Try not using prefix mode and you will see everyting will work!

/J
Reply With Quote
  #6 (permalink)  
Old 02-22-2012, 06:08 PM
walterf25's Avatar
Basic4ppc Veteran
 
Join Date: Sep 2011
Posts: 209
Default

Quote:
Originally Posted by Jonas View Post
I guess you dont send the prefix characters from you VS program which is needed when using prefix mode. Try not using prefix mode and you will see everyting will work!

/J
Hi Jonas, thanks for your reponse, i'm not sure what you mean when you say prefix mode, can you elaborate a little more?



thanks,
Walter
Reply With Quote
  #7 (permalink)  
Old 02-22-2012, 08:47 PM
Newbie
 
Join Date: Jan 2012
Posts: 4
Default

When you initialize the Asyncstream in this exemple the following code is

Code:
AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
But if you instead use

Code:
AStreams.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
The program wont be halted beacuse it cant find the prefix in the incoming data stream.

When the AStreams_NewData event triggers it might just hold a few bytes of the whole message you want to receive so you have to append the Buffer data into a variable and manipulate the data later.
Or just update txtLog.Text = txtLog.Text & the buffer data

/J

Last edited by Jonas : 02-22-2012 at 09:07 PM.
Reply With Quote
  #8 (permalink)  
Old 02-22-2012, 11:13 PM
walterf25's Avatar
Basic4ppc Veteran
 
Join Date: Sep 2011
Posts: 209
Default bluetooth com

Quote:
Originally Posted by Jonas View Post
I guess you dont send the prefix characters from you VS program which is needed when using prefix mode. Try not using prefix mode and you will see everyting will work!

/J
thanks Jonas that worked like a charm, thanks much buddy!

cheers,
Walter
Reply With Quote
  #9 (permalink)  
Old 03-13-2012, 11:37 PM
Junior Member
 
Join Date: Sep 2007
Posts: 23
Default

Quote:
Originally Posted by raphaelcno View Post
Is it possible to show "txtLog" with two different colours, for instance received text in red colour and sent text in blue colour?
Is it also possible to show a time stamp ("12:34:56.789" or similar) before the text "You" and "Me"?
The solution for text color and time stamp is shown in this thread:
http://www.basic4ppc.com/forum/basic...ichstring.html
Reply With Quote
  #10 (permalink)  
Old 03-25-2012, 11:46 AM
Knows the basics
 
Join Date: Apr 2009
Posts: 52
Default

i tried compiling the provided sample with b4a 1.6 and am having error.
is this sample not compatible with 1.6? or im just missing something?

thanks
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 FTP tutorial Erel Basic4android Getting started & Tutorials 104 Yesterday 05:51 PM
Android Serial tutorial Erel Basic4android Getting started & Tutorials 78 05-20-2012 05:39 AM
Android Network Tutorial Erel Basic4android Getting started & Tutorials 56 03-22-2012 07:26 AM
Android FTP Tutorial klaus German Tutorials 1 02-04-2012 04:01 PM
Serial library v1.20 - BluetoothAdmin and insecure connections Erel Additional libraries and official updates 8 11-18-2011 12:42 PM


All times are GMT. The time now is 10:07 AM.


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