Home
> Focus > Full Story
Programming
for Bluetooth
By
Anurag Phadke
With
more and more computing devices featuring Bluetooth connectivity,
it's about time you provide some serious thought to programming
for Bluetooth devices. Here are some snippets of code that
will get you started
As
per expert estimates, come 2002 and over 100 million mobile
phones and mobile computing devices all over the world will
be Bluetooth enabled. What we all are striving for today
is a world without wires, full of various frequencies establishing
connection between various handheld devices. Though, Captain
John Lu Picard's Starship seems to be somewhere in the distant
future, Bluetooth and the SIG developers are certainly moving
at warp speed to turn some of the futuristic sci-fi fantasies
into reality.
Using
a 2.4GHz ISM frequency band Bluetooth targets mainly small
mobile devices that require very little power to sustain.
The technology is much similar to our TV Remote Control
using IR (Infra Red Rays) or our cordless phones that use
a particular frequency. Also, this technology is well accepted
as an industry standard with companies such as Nokia, Motorola,
Toshiba, etc developing products based of this wireless
connectivity platform.
So,
what actually does a Bluetooth device comprise of? Does
it make use of any programming language? To understand Bluetooth
is to understand its architecture.
Bluetooth
System Architecture
The
Bluetooth Architecture comprises of a number of layers interlinked
to one another.
Radio
stack at the bottom of the layer is the interface responsible
for the physical connection. Baseband and Link Manager protocol
(LMP) are used to establish control links between bluetooth
devices. The firmware/hardware comprises of three layers,
the Host Control Layer is used to interface the Bluetooth
hardware to the upper L2CAP (Logical Link Control and Adaptation
Control Protocol). Presence of host controller is a requisite
only if L2CAP resides in software in host. (Read Bluetooth:
Connectivity without wires for details on various layers
in the Bluetooth Stack)
Get
down to coding
Once
you have familiarized yourself with the different layers
in the Bluetooth stack and their functionality, you can
make use of the various stacks and then build your own customized
programs. Stacks are like functions/programs created
by other programmers and prove helpful as it does away with
the need for tedious redundant programming. They are similar
to #include files that come with "C" language.
Lets
begin with a small program in ActiveX used for finding various
Bluetooth enabled devices in the vicinity of 10m. Once a
device is found it is assigned a value and then functions
such as File Transfer, Viewing Directory Contents and other
similar things can be performed.
Used
to convert a byte into a HEX string with zero padding
Option
Explicit
'Global
Variable
Dim
intCmdresult As Integer
Function
ByteHex(ByVal byteI As Byte) As String
ByteHex
= Hex(byteI)
If
Len(ByteHex) < 2 Then
ByteHex
= "0" & ByteHex
End
If
End
Function
Opening
the Com Port for Communication.
Function
OpenComPort()
objBT.ComPort
= 2 'Opens COM2 port
objBT.ComPortBaudrate
= 3 'Baud Rate of 57600
Call
objBT.API_OpenComPort(intCmdresult)
If
(intCmdresult < 0) Then
MsgBox
("Could not open com port no 2. If this is not the
port number your device is connected to, connect it to com
port number 2 or open the visual basic project and change
the com port number to the correct one, then recompile the
project. (error: " & intCmdresult & ")")
End
If
End
Function
The
following code is used to start a stack. Once a stack is
started it needs to be reset and then initialized before
further
use.
Function
StartStack()
'Start
the Bluetooth stack
Call
objBT.API_StartStack(1, intCmdresult)
If
(intCmdresult < 0) Then
MsgBox
("Could not start the stack. (Internal Error: "
& intCmdresult & ")")
End
If
End
Function
Function
ResetStack()
'Reset
the Bluetooth stack
Call
objBT.API_Reset(intCmdresult)
If
(intCmdresult < 0) Then
MsgBox
("Could not reset the stack. You probably do not have
a bluetooth module connected to this PC. (error: "
& intCmdresult & ")")
End
If
End
Function
Function
InitStack()
'Initialise
the Bluetooth stack
Call
objBT.API_Init(intCmdresult)
If
(intCmdresult < 0) Then
MsgBox
("Could not initialise the stack. (Internal Error:
" & intCmdresult & ")")
End
If
End
Function
Function
AllowScan()
'Variables
Dim
byteSwitch As Byte
byteSwitch
= 3
'Request
scanning to be enabled
Call
objBT.WriteScanEnable_Issue(byteSwitch, intCmdresult)
intCmdresult
= -24
Do
While intCmdresult = -24
Call
objBT.WriteScanEnable_Result(intCmdresult)
Loop
'if
the WriteScanEnable failed
If
intCmdresult <> 0 Then
MsgBox
("Scanning not enabled on your device, others module
will not be able to see you (error: " & intCmdresult
& ")")
End
If
End
Function
Private
Sub Form_Load()
'The
following functions:
'OpenComPort(),
StartStack(), ResetStack(), InitStack()
'AllowScan()
are used to get the bluetooth device ready to
'communicate.
those functions are defined in GENERAL
Call
OpenComPort 'Open communication port
'If
com port open
If
intCmdresult = 0 Then
Call
StartStack 'Start Stack
End
If
'If
the stack started well
If
intCmdresult = 0 Then
Call
ResetStack 'Reset Stack
End
If
'If
the module has been reset succesfully
If
intCmdresult = 0 Then
Call
InitStack 'Init Stack
End
If
'If
the module has been initialised sucessfully
If
intCmdresult = 0 Then
Call
AllowScan 'Allow this device to be visible
End
If
End
Sub
Once
the bluetooth devices are properly initialized, the program
can then be made to search for various devices in the 10m
range. If found, a low '0' is generated else a high '1'
is generated.
Private
Sub Button_Search_Click()
'Command
to search for Bluetooth devices within range
'of
the locally connected device. This function
'send
out a general enquiry and then waits for
'responses
from bluetooth devices. All replies
'are
then added to the list box for display.
'this
define standard lap (Lower Address Part) values
'those
values should be recognised by any bluetooth device
Dim
byteLap(3) As Byte
byteLap(0)
= &H33
byteLap(1)
= &H8B
byteLap(2)
= &H9E
'other
useful variables
Dim
intI, intJ As Integer
Dim
strBDAddr As String
Dim
varNumResps As Variant
Dim
varBDAddrs As Variant
'These
variables are used by the function
'Inquiry_Result_Event()
Dim
varPSRModes As Variant
Dim
varPSPModes As Variant
Dim
varPSModes As Variant
Dim
varCoDs As Variant
Dim
varClkOffsets As Variant
'clear
the listbox of previous results
SearchResultListBox.Clear
'Send
a general enquiry so that any bluetooth
'device
in the area will respond.
Call
objBT.Inquiry_Issue(byteLap, 3, 0, intCmdresult)
If
(intCmdresult < 0) Then
MsgBox
(" error = " & intCmdresult)
End
If
'We
now wait for the result from our enquiry and
'also
for the replies from any bluetooth device
'in
the area.
intCmdresult
= -24
Do
While intCmdresult = -24
Call
objBT.Inquiry_Result(varNumResps, intCmdresult)
DoEvents
Loop
'the
inquiry is finished, all other
'devices
have responded, or the inquiry
'was
cancelled or there was an error
If
(intCmdresult < 0) Then
If
(intCmdresult = -18) Then
MsgBox
(" Inquiry cancelled ")
Else
MsgBox
(" error = " & intCmdresult)
End
If
Else
'populate
listbox with information about the devices
'get
the information about every present device
intCmdresult
= 0
Do
While intCmdresult <> -24
Call
objBT.InquiryResult_Event(varNumResps, _
varBDAddrs,
varPSRModes, _
varPSPModes,
varPSModes, _
varCoDs,
varClkOffsets, _
intCmdresult)
'if
we have received data accurately
'we
add the bluetooth addresses of the
'devices
detected to the listbox after
'converting
them to hexadecimal format
If
intCmdresult = 0 Then
'for
every bluetooth device found, add them in the
list
box
For
intI = 0 To varNumResps - 1
'set
bluetooth address to nothing
strBDAddr
= ""
'the
bluetooth address is returned
'its
format is a bidimensionnal array (x,6)
'we
convert every byte to hexadecimal
'then
we concatenate every 6 bytes
For
intJ = 0 To 5
strBDAddr
= strBDAddr & ByteHex(varBDAddrs(intI, intJ))
Next
intJ
'we
add the result to the list box
SearchResultListBox.AddItem
strBDAddr
Next
intI
End
If
Loop
End
If
End
Sub
Once
a device is found, it can be completely controlled by the
above mentioned code. This helps us to achieve wireless
communication between two or more PC's or laptops
making the task of connecting various wires a hassle
free experience.
Anurag
Phadke can be reached at cbca@mantraonline.com
Testing
your code
Be
sure that your Bluetooth device is connected to COM1 port.
Start the program and click on Search. The device shall
now attempt to search for other Bluetooth-enabled devices
in the vicinity of 10m. On a successful attempt of finding
a similar device, the corresponding "6-byte" (or
12 digit) address (in HEX format) of the "found"
device is displayed on the screen. To find more devices,
press refresh. This shall show you the address of the local
Bluetooth device in succession to the other devices found.
(The
program listed above performs the basic function of finding
Bluetooth devices. To perform File Operations there's
another program which over 1000 lines and reproducing
it in the magazine is not a viable option).
<<