Developing Bluetooth Applications with LabVIEW

Developing Bluetooth Applications with LabVIEW - Tutorial - Developer Zone - National Instruments
view cart | help | search NI Developer Zone
Developing Bluetooth Applications with LabVIEW
Back to Document
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. The release of LabVIEW 7.1 includes a new set of
Bluetooth Compatibility 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
Creating a Bluetooth Server Application
Creating a Bluetooth Client Application
Finding Nearby Bluetooth Devices
Querying Services Available on a Bluetooth Device
Bluetooth Virtual Serial Port
Setup and Requirements
Bluetooth capabilities in LabVIEW are available for the following operating systems:
●
●
●
Windows XP SP 1 and later – LabVIEW works with Bluetooth devices that use the Microsoft Bluetooth driver on Windows XP
Service Pack 1 or later only. Refer to the Microsoft Web site for Bluetooth devices that use the Microsoft Bluetooth driver. Most
Bluetooth devices use a proprietary Bluetooth driver by default. To use the device with LabVIEW, you must switch to the Microsoft
Bluetooth driver. To switch drivers, you must obtain the Microsoft Bluetooth driver CD from the manufacturer of the device. 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.
Pocket PC 2003 – To run Bluetooth applications on Pocket PC 2003 devices, you must install Bluetooth hardware with 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 LabVIEW PDA Module User Manual 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
http://zone.ni.com/devzone/conceptd.nsf/webma...86256E7500726F15?opendocument&node=dz00000_us 第 1 頁 / 共 4 2005/5/18 上午 11:23:04
Developing Bluetooth Applications with LabVIEW - Tutorial - Developer Zone - National Instruments
LabVIEW PDA User Manual
Creating a Bluetooth Server Application
Complete the following steps to develop 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.
Figure 1. Bluetooth Server Application
Creating a Bluetooth Client Application
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.
http://zone.ni.com/devzone/conceptd.nsf/webma...86256E7500726F15?opendocument&node=dz00000_us 第 2 頁 / 共 4 2005/5/18 上午 11:23:04
Developing Bluetooth Applications with LabVIEW - Tutorial - Developer Zone - National Instruments
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.
Figure 2. Bluetooth Client Application
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
Use the Bluetooth Discover function to search for Bluetooth devices that are within the permissible range. The function returns a list of
device addresses and names. These device addresses can then be used by a client to connect to a specific Bluetooth server. The optional
input parameter, time limit (ms), specifies the length of Bluetooth inquiry. The default value is 10 ms and the maximum value is 30 s. If time
limit is less than or equal to zero, the function returns a list of installed local 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.
http://zone.ni.com/devzone/conceptd.nsf/webma...86256E7500726F15?opendocument&node=dz00000_us 第 3 頁 / 共 4 2005/5/18 上午 11:23:04
Developing Bluetooth Applications with LabVIEW - Tutorial - Developer Zone - National Instruments
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
A Bluetooth system in PalmOS and PocketPC provides serial port profile with a virtual serial driver. Bluetooth Serial Port profile defines
procedures that devices use to emulate an RS-232/serial cable connection. The profile maps virtual Serial COM ports to actual Bluetooth
channels. Bluetooth Serial Port profile enables you to use LabVIEW Serial VIs to open a connection to and exchange data with other
Bluetooth devices. Bluetooth Serial Port profile cannot be used on Windows XP SP1. Other Windows Bluetooth drivers may work with the
Serial Port profile.
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
My Profile | Privacy | Legal | Contact NI © 2005 National Instruments Corporation. All rights reserved.
http://zone.ni.com/devzone/conceptd.nsf/webma...86256E7500726F15?opendocument&node=dz00000_us 第 4 頁 / 共 4 2005/5/18 上午 11:23:04