BTEPPC: an example Pocket PC program for Winsock Bluetooth communication

Lin Zhong

Rice University
lzhong AT rice DOT edu

Download source code:  Download (BTEPPC-1.0.zip)
 

This example program was built for students in ELEC424/528 to learn how to program Bluetooth on Windows Mobile devices with the Winsock Bluetooth APIs. Remember that www.msdn.com answers most questions. This example is built as an MFC program. Read this tutorial first.

 

1. Mobile device

 

BTEPPC only runs for Pocket PC 2003 and Windows Mobile 5.0 Pocket PC. (Without touch-screen, Smartphones requires directly GUIs. Buttons in general do not work well for them.)

 

BTEPPC consists of three dialogs: Main (CBTExamplePPC), Discover (CDiscoverDlg), and ViewTraffic (BTCommDlg).

 

1. 1 Main Dialog (CBTExamplePPCDlg)

 

There are two listboxes (CListBox) in the Main dialog.  There is a Check box (CButton)  that reads “Enable BT”. Check and Uncheck it enables or disables the Bluetooth.

 

The bottom listbox shows the program output, information that tells us whether the program runs normally.

 

The top listbox shows “trusted devices,” Bluetooth devices that we would like to directly connect to. All the push buttons work for it.

 

How to add/remove trusted devices?

There are two ways. First, create or edit file “trusteddevice.dat” at the root directory of the mobile device. On your PC ActiveSyn explorer or Mobile device File manager, view “My Device”.

Following is an example content.

////////////////////////////////////////////////

1

000a3a598e0e OAKTREE

///////////////////////////////////////////////

The first line indicates how many devices are trusted. The following lines simply list them with their physical addresses and names. You can add or remove devices by editing this file.

 

When BTEPPC starts, it searches for this file (CBTExamplePPCDlg::OnInitDialog() and CBTExamplePPCDlg::LoadTrustedBTDevices()). When it exits, it saves the current trusted devices into this file (CBTExamplePPCDlg::StoreTrustedBTDevices())

Second, trusted devices can be added through the Discover dialog. Discover dialog is actived by pushing the “Discover” button. See 1.2 Discover Dialog for more information.

 

A trusted device can be removed by selecting it from the top listbox and click the “Remove” button.

 

How to connect or disconnect to a trusted device?

Simply select it from the top listbox and click the “Connect”/”Disconnect” button.

 

To view the communication between the mobile device and a trusted Bluetooth device, select the trusted device from the top listbox and click the “View BT traffic” button. This will create TrafficView Dialog. See 1.3 for more information.

 

Click “Exit” button to properly exit the program.

 

1.2 Discover Dialog (CDiscoverDlg)

 

 

Discover Dialog is created when the “Discover” button is clicked on Main Dialog. Discover Dialog also consists of two listboxes. The top listbox shows the discovered devices. If a discovered devices is selected in the listbox, we can “add” (button “Add”) it to the bottom listbox. This device is then trusted.

 

The “Discover” button creates a thread to discover Bluetooth devices in the neighborhood. As the process goes on, discovered devices will be added to the top listbox and the progress bar will be updated. The “Stop” button simply kill the thread and stops the discover process. Click “Done” to return to Main Dialog

 

1.3 TrafficView Dialog (CBTCommDlg)

 

TrafficView Dialog enables us to view what is received from the connected device and send data to it. Incoming data appear in the top listbox (Incoming traffic).  Sent data appear in the bottom listbox (Outgoing traffic).

 

When TrafficView Dialog first appears, the name of the connected device appears in the text edit box (CEdit) between the two listboxes. One can send data to the connected device by inputting characters in the edit box and push the “Send” button. The “Clear” button clears the edit box.

 

TrafficViewDialog basically creates two threads, one for sending and the other for receiving, for communicating with the connected devices.

 

Click the “Close” button to go back to Main Dialog and kill the sending and receiving threads.

 

2. Desktop PC

 

2.1 Before connect

Start-->All Programs-->Accessories-->Communications-->HyperTerminal

 

Type a name, say BT, and choose an Icon.

 

Connect using COMX. X is dependent on your PC. Mine is COM5. I found it out from the Bluetooth USB adapter configuration-->Local Services (Bluetooth Serial Port)

 

COMX properties: Port Settings

Bits per send: Whatever. It does not matter since we use Winsock APIs on the mobile device.

Data bits: 8

Parity: None

Stop bits: 1

Flow control: None

 

When you are back the Main Window, Choose File-->Properties-->Settings-->ASCII Setup-->Check “Echo typed characters locally” so that you will see what you type in.

Check “Send line ends with line feeds” too.

 

You have to disconnect to change the COMX properties again: File-->Properties-->Configure… will bring you to the Port Settings again.

 

Make sure you change your PC Bluetooth adapter’s configuration to allow all devices to connect to the PC.

 

2.2 During connecting process

If this is the first time your mobile device is connecting to your PC, the PC may ask you for a Passkey, even if you are using BTEPPC to connect. Just input 0000. Then your mobile device may ask you for a Passkey a moment later. Just input 0000, again. From now on, you don’t need to input anything for them to get connected through BTEPPC.

 

2.3 Already connected

Now, whatever you send from the mobile device through BTEPPC’s TrafficView Dialog should show up here. Whatever you type in Hyper Terminal will show up in the “Incoming traffic” listbox on TrafficView Dialog, if there are more than 16 characters or you press “Enter” from the PC keyboard.

 

3. Source code description and reuse

 

3.1 Entry point and code organization

The entry point for BTEPPC is the declaration of an object of class CBTExamplePPCApp (CBTExamplePPCApp theApp;) in BTExamplePPC.cpp. BOOL CBTExamplePPCApp::InitInstance() is where we can start adding our own code. For example, it creates Main Dialog there (  CBTExamplePPCDlg dlg; m_pMainWnd = &dlg;INT_PTR nResponse = dlg.DoModal(); )

 

BTExamplePPC.h/cpp implements the entry point of BTEPPC

BTExamplePPCDlg.h/ccp implements Main Dialog

DiscoverDlg.h/cpp implements Discover Dialog

BTCommDlg.h/cpp implements TrafficView Dialog

BTDevice.h/cpp implements a class that describes a Bluetooth device

BTfunctions.h/cpp implements important utilities you may want to reuse for your project

 

Resourceppc.h, Resourcesp.h and stdafx.h were automatically generated by Visual Studio. The first one contains the ID definitions for Pocket PC resources, e.g. buttons, dialogs and listboxes.

 

3.2  How to find out the routine that handles a user input, e.g. button click?

 

In Visual Studio, there are three views of the projects: Solution (for source files), Class (for classes), and Resource (for resources, such as menu, dialog, buttons etc). Say if you wish to know which routine is called when the “Connect” button is clicked.

1. Go to the Resource view. Click to open BTExamplePPCppc.rc (ppc.rc indicates this is the resource for Pocket PC). In Dialog, find the Dialog in which you click the “Connect” button. In this case, IDD_BTEXAMPLEPPC_DIALOG. Right click on the “Connect” button in the dialog and choose “Properties.” (You can add your own handlers to this button click by choosing “Add Event Handler…”)  In the properties window, find the “ID” of this button. In this case, it is “IDC_BUTTON3”. Note it down.

 

Now, go back to the Solution view, click to open BTExamplePPCDlg.cpp, which implements the Dialog.  Not far from the beginning of this file, you will find two important code sections: 1) DoDataExchange, which link a GUI object to a code object and 2)  MESSAGE_MAP, which maps user interface events to their corresponding handlers. In MESSAGE_MAP, we will find ON_BN_CLICKED(IDC_BUTTON3, &CBTExamplePPCDlg::OnBnClickedConnect). That is, when the “Connect” button is clicked, CBTExamplePPCDlg::OnBnClickedConnect() will be called. You can find this routine in the same file.

 

3.3 Reuse

When one reuses the code from BTEPPC, make sure to check the following things:

 

i) Click Visual Studio-->Project-->XXX properties (XXX is your project name), a Property Pages Dialog will appear.

Configuration Properties-->Linker-->Input-->Additional Dependencies, Make sure you have bthutil.lib, bthguid.lib, btdrtstubs.lib, and btdrt.lib there.

ii) Copy btd.def/lib/pdb, btdrt.def/lib/pdb, and btdrtstubs.lib/pdb from BTEPPC’s source file directory to the corresponding directory of your new project. There files are required for some Bluetooth operations. They were copied from an evaluation version of Windows CE Platform Builder.

iii) Include proper header files.