lunedì 18 marzo 2013

RFObasic! per android: comunicazione con Bluetooth

comincio da RFObasic!
manuale pagina 181
estratto
programma tutorial:

Copyright© Paul Laughton, 2011, 2012  Page 181  De Re BASIC!


f35_bluetooth.bas 
!! 
This program demonstrates using bluetooth as both an application that listens for a connection 
or makes a connection to a listener. 

Before running this program use the Android "Settings" application to enable Bluetooth and to pair 
with the device(s) that you will talk to. 

This program will send and receive data bytes to and from a connected device. 

You can use this program to  (among many other things) set up a chat between two Android 
devices. 

Note: The UUID used by default is the standard serial port UUID. This can be changed. Read the manual. 

!! 

! Begin by opening Bluetooth 
! If Bluetooth is not enabled 
! the program will stop here. 

bt.open 

! When BT is opened, the program 
! will start listening for another 
! device to connect. At this time 
! the user can continue to wait 
! for a connection are can attempt 
! to connect to another device 
! that is waiting for a connection 

! Ask user what to do 

array.load type$[], "Connect to listener", "Continue to listen for connection", "Quit" 
msg$ = "Select operation mode" 

new_connection: 

select type, type$[], msg$ 

! If the user pressed the back 
! key or selected quit then quit 
! otherwise try to connect to 
! a listener 

if (type = 0) | (type =3) 
 print "Thanks for playing" 
 bt.close 
 end 
elseif type = 1 
 bt.connect 
endif 

! Read status until 
! a connection is made 


Do 
 bt.status s 
 if s = 1  
  print "Listening 
  elseif s =2 
   print "Connecting" 
   elseif s = 3 
    print "Connected" 
 endif 
 pause 1000 

until s =3 

! When a connection is made 
! get the name of the connected 
! device 

bt.device.name device$ 

! *** Read/Write Loop **** 

RW_Loop: 

Do 

! Read status to insure 
! that we remain connected. 
! If disconnected, program 
! reverts to listen mode. 
! In that case, ask user 
! what to do. 

 bt.status s 
 if s<> 3 
  print "Connection lost" 
  goto new_connection 
 endif 

! Read messages until 
! the message queue is 
! empty 

 Do 
  bt.read.ready rr 
  if rr 
   bt.read.bytes msg$ 
   print device$;": ";msg$ 
  endif 
 until rr = 0 

! Message Queue is empty 
! Send a message 
! Note: If no text is entered 
! then no text will be sent 

 Input "Text to send", msg$ 
 if (msg$ <> "") & (msg$<> "*") 
  bt.write msg$ 
  print "Me: "; msg$ 
 endif 

until msg$ = "*"  

! if msg$ is "*" then close connection 
! and restart 

bt.close 
bt.open 
goto new_connection 

Nessun commento:

Posta un commento