Developing Bluetooth Applications with LabVIEW
Overview
Bluetooth is a wireless protocol that uses a 2.4 GHz radio frequency to communicate among devices within a 30 to 40-foot range. Initially developed to communicate wirelessly with cellular phones, PDAs, and laptop computers, the Bluetooth simple protocol and ease of implementation make it ideal for wireless communication across a diverse set of products in many industries. Engineers are now using Bluetooth to develop remote data acquisition and instrument control applications. LabVIEW 7.1 and later include Bluetooth VIs with which LabVIEW developers can build custom Bluetooth applications. This application note demonstrates how to build a LabVIEW application to communicate with Bluetooth-enabled devices. Refer to the Bluetooth Web sites at www.microsoft.com and www.bluetooth.com (linked below) for more information about Bluetooth technology.
Creating Bluetooth server and client applications in LabVIEW is similar to creating server and client applications for TCP communication. A Bluetooth server uses the Service Discovery Protocol (SDP) to broadcast the availability of the services contained and listens for inbound connections. A client creates an outbound RFCOMM connection to a server. Once the client and server connect to each other, they exchange data until the client or server terminates the connection or until the connection is lost. There is no inherent security built into the Bluetooth protocol, although many devices include settings that require clients to log in before accessing Bluetooth services.
The following steps outline how to configure your computer or PDA for Bluetooth communication and demonstrate how to use LabVIEW to build a Bluetooth server application and a Bluetooth client application.
Table of Contents
Setup and Requirements
Bluetooth capabilities in LabVIEW are available for the following operating systems: - Windows XP SP 2 and later – LabVIEW works with Bluetooth devices that use the Microsoft Bluetooth driver included with Windows XP Service Pack 2 and later. Refer to the Microsoft Web site for Bluetooth devices that are supported by the Microsoft Bluetooth driver. Most Bluetooth devices come packaged with a proprietary Bluetooth driver. To use the device with LabVIEW, the Bluetooth adapter must be using the Microsoft Bluetooth driver. A Microsoft Bluetooth driver for Windows XP SP1 was also available. Refer to Microsoft Knowledge Base Article 323183: Availability of Windows XP Service Pack 1 Support for Bluetooth Wireless Devices for more information about Bluetooth-compatible operating systems.
- Windows 2000 – Bluetooth applications for a PDA target can be developed in Windows 2000. However, Bluetooth communication cannot be utilized since Windows 2000 does not have a compatible Bluetooth driver. Applications developed in Windows 2000 can be built for a target that supports Bluetooth and run on that system.
- Pocket PC 2003 – To run Bluetooth applications on Pocket PC 2003 devices they must be using the WIDCOMM BTW-CE driver version 1.4 or later. Additional installation of Bluetooth DLLs is also required. Refer to the Installing the WIDCOMM Bluetooth DLLs on Pocket PC 2003 Devices section of the Getting Started with LabVIEW PDA Module for more information.
- Palm OS 5.0 and later – To run Bluetooth applications on Palm devices, you must have Palm OS 5.0 and later with installed Bluetooth hardware.
See Also:
Microsoft Knowledge Base
Getting Started with LabVIEW PDA Module
Creating a Bluetooth Server Application
1. Create a Bluetooth service – Use the Bluetooth Create Listener function to create a Bluetooth service identified by a Bluetooth uuid. This function returns a listener ID which refers to this server through your LabVIEW application. The Bluetooth Create Listener function also returns a reserved Bluetooth channel that the server can use to listen for inbound connections. A Bluetooth channel is a global resource with only 30 channels available on any Bluetooth device. If no server channel is available the function returns an error.
2. Wait for incoming connection request – Use the Bluetooth Wait on Listener function to wait for and accept an incoming connection request from a client. This function returns a connection ID that is used to exchange data with the client.
3. Read and Write data – Use Bluetooth Read and Bluetooth Write functions to exchange data with the client.
4.Close connection – Use Bluetooth Close Connection function to close connection to the client and to stop listening for incoming connections.
The block diagram of a typical Bluetooth server application looks similar to Figure 1.
Complete the following steps to develop a Bluetooth client application.1. Request a connection to Bluetooth server – Use the Bluetooth Open Connection function to connect to a service on a Bluetooth server. Set the channel number to zero and specify a Bluetooth uuid to identify which service to connect to. The Bluetooth Open Connection function performs an SDP query to make a connection to the first service found with matching uuid. Internally, the result of an SDP query is an RFCOMM channel number to connect to. The SDP query is a tool to "translate" uuid to a channel number.
If you know the channel number associated with the service in advance, use the channel number instead of zero. Specifying a nonzero channel number bypasses the internal SDP query operation thus reducing the amount of time it takes to connect to the service. If the channel number is nonzero, LabVIEW ignores the uuid input parameter.
Note: You can use the Bluetooth RFCOMM Service Discovery VI to search for a valid channel number associated with a service on a remote Bluetooth device. This VI performs an SDP RFCOMM service discovery resulting in a channel number you can use to connect to the corresponding service on the remote Bluetooth device.
2. Read and Write data – Use Bluetooth Read and Bluetooth Write functions to exchange data with the server.
3. Close connection – Use Bluetooth Close Connection function to close connection to the server.
The block diagram of a typical Bluetooth client application looks similar to Figure 2.
Refer to the following VIs for examples of creating a simple Bluetooth server and client applications:
- Simple Bluetooth Server VI: labview\examples\comm\Bluetooth.llb
- Simple Bluetooth Client VI: labview\examples\comm\Bluetooth.llb
- Simple Bluetooth Server (Palm OS) VI: labview\examples\PDA\pdacomm\PDABluetooth.llb
- Simple Bluetooth Client (Palm OS) VI: labview\examples\PDA\pdacomm\PDABluetooth.llb
- Simple Bluetooth Server (Pocket PC) VI: labview\examples\PDA\pdacomm\PDABluetooth.llb
- Simple Bluetooth Client (Pocket PC) VI: labview\examples\PDA\pdacomm\PDABluetooth.llb
Finding Nearby Bluetooth Devices
Note: Bluetooth discovery is a slow operation because of the communication involved. A Bluetooth device address is a fixed address that is usually printed on the actual device and you can query the address from the device control setting. This address is unique to each device. If you know the Bluetooth address of the specific device you want to connect to, you can skip the discovery process and use the Bluetooth Open Connection function to connect to the device directly.
Querying Services Available on a Bluetooth Device
Use the Bluetooth RFCOMM Service Discovery VI to search for available RFCOMM services on a local/remote Bluetooth device. This function returns a list of available services along with the associated channel numbers. A channel number in the list can be used as an input parameter of the Bluetooth Open Connection function to make a connection to the corresponding service on the remote Bluetooth device.

Figure 3. Bluetooth Discovery Function
Refer to the following VIs for examples of Bluetooth device and service discovery:
- Advanced Bluetooth Server.vi: labview\examples\comm\Bluetooth.llb
- Advanced Bluetooth Client.vi: labview\examples\comm\Bluetooth.llb
- Advanced Bluetooth Client – PDA.vi: labview\examples\PDA\pdacomm\PDABluetoothllb
Bluetooth Virtual Serial Port
The driver recognizes only one active serial channel at a time. You can open the serial channel as an outbound (client) port or as an inbound (server) port. When you open the port as an outbound (client) port, the driver prompts you to select a Bluetooth device (server) to connect to. When you open the port as an inbound (server) port, the driver waits to receive a connection request from the client. Bluetooth serial port is based on RFCOMM, which is different from a physical serial port. RFCOMM requires roles of a server and a client when establishing Bluetooth serial connection.
Refer to KnowledgeBase 32GEGGTP: Does LabVIEW for PDA Support Bluetooth? (linked below) for more information on Bluetooth Virtual Serial Port and the LabVIEW PDA Module.
Related Links:
KnowledgeBase 32GEGGTP: Does LabVIEW for PDA Support Bluetooth?
LabVIEW Development System
LabVIEW PDA Module
Microsoft Website
Bluetooth Website
LabVIEW PDA User Manual
Reader Comments | Submit a comment »
How to obtain uuid? What it is? How do we
know that?
- Alex Nguyen, qweq. freewinduts@gmail.com - May 29, 2007
The bluetooth support should be
extended to CVI also.
- srinath.bharadwaj@gmail.com - Oct 17, 2006
What about CVI and Measurement Studio?
The support for Bluetooth applications is
great? Why have you not put this
capabability into CVI or Measurement Studio?
- Steve Macha, Mikrokraft, Inc.. smacha@alltel.net - Mar 21, 2005
Bluetooth VI are good.
Okay, I changed my mind. I can see that
having Labview start and stop the Bluetooth
services to the "virtual serial port" is a
good thing. It saves on the manual effort
of discovering the Bluetooth services,
starting and stopping, etc. through
Windows. Especially if the computer is
unattended.
- Craig Hange, NASA Ames Research Center. craig.e.hange@nasa.gov - Jul 23, 2004
Windows Bluetooth Serial Port Service also works.
I was wondering if someone could explain
the advantages of putting the Bluetooth
access within Labview? On my Bluetooth
equipped computers I can choose to use the
Bluetooth as a virtual serial port through
Windows XP and "Discover Bluetooth
Services". It then appears as COM6 or COM7.
Nothing needs to be changed in the Labview
VI, except the serial port number. I can
still run code that was written years ago
for the serial port. This seems easier, but
I may be missing some advantage.
- Craig Hange, NASA Ames Research Center. craig.e.hange@nasa.gov - Jul 23, 2004
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).


