This is a second article about Bluetooth connectivity using USB Host library for Arduino. Previous article described Android connectivity; today, I will show how to connect an Arduino to a Windows machine using USB Host Shield ( available at the store ) and USB Bluetooth dongle.
The biggest issue with Bluetooth on a Windows machine is third-party Bluetooth stacks. They all work well but behave slightly differently and it is not possible to write a step-by-step walkthrough valid for any Windows PC. My setup is 64-bit Windows 7 with Broadcomm Bluetooth stack; other Windows versions and Bluetooth stacks will be slightly different.If you have any difficulties following the text, leave a comment and I’ll try to help. Also, Bluetooth protocols time out very quickly – 5-10 minutes, sometimes less. If you get a timeout at any step simply start over again; sometimes, resetting Arduino may be necessary.
First thing that needs to be done is to make a working Arduino setup. Previous article on the topic gives plenty of information about necessary gear, compatible Bluetooth dongles and expected terminal output. Once this is done, install Bluetooth on Windows. On Win7, the installation consists of plugging in a Bluetooth dongle to USB port and watching the progress of installation process which will start automatically. At the end of the installation system will give you the warning that your PC is not discoverable – ignore it. If installation is successful a Bluetooth icon will appear in the system tray.
The next step is to pair Arduino to a PC. At this point, an Arduino-USB Host Shield-Bluetooth dongle combo shall be put together as described earlier, tested, and set up as follows:
- Powered from external supply – optional but highly recommended, at least for the first attempt. If you decide to use external supply, connect it before USB cable
- Connected to a PC with USB cable. It could be the same PC to which you are going to pair your Arduino via Bluetooth
- Terminal to Arduino opened. It can be Serial Monitor in Arduino IDE or third-party terminal program such as Putty or Teraterm. If everything is wired correctly and Bluetooth dongle is good you should see the following output in the terminal window:
SPP Bluetooth Library Started Bluetooth Dongle Initialized No response to HCI Reset HCI Reset complete Local Bluetooth Address: 00:17:9A:2B:63:DA The name is set to: Arduino Wait For Incoming Connection Request
Now click on the Bluetooth icon in the system tray. “Device and Printers” dialog will open. Click on “Add a device”. After a short pause Arduino Bluetooth dongle will be detected and you will see the following screenshot. Sometimes a numerical address will be printed instead of a name, it will be the same address that is printed in Arduino terminal window (see above).
Select Arduino device and click “Next”. The system will try to probe the Arduino Bluetooth. The following output should appear in the Arduino terminal (the device address and remote name will be different on your machine):
Incoming Connection Request Remote Name: DESK Connected to Device: 91:68:19:C4:36:28 SDP Incoming Connection Request SDP Configuration Request SDP Successfully Configured Wait For Incoming Connection Request Disconnected SDP Channel HCI Disconnected from Device Wait For Incoming Connection Request
On the PC, the following dialog will open. Click on “Enter the device’s pairing code”.
On the following screen type ‘1234’ and click “Next”. You’ll get a screen stating that the device is added successfully.
In the mean time, this is what should appear in the Arduino terminal window.
Bluetooth pin is set too: 1234 Disconnected SDP Channel SDP Incoming Connection Request SDP Configuration Request SDP Successfully Configured Disconnected SDP Channel HCI Disconnected from Device Wait For Incoming Connection Request
Both devices are now configured and are able to pass data to each other. “Devices and Printers” dialog should still be open with Arduino device in it. Right click on it, select “Properties” and then click on “Services” tab. You should see a screen similar to the title picture (click on it to make it larger). The screen shows SPP service on Arduino and corresponding virtual COM port on a PC, COM50 in my case. Open a terminal session to this port. The following screenshot shows Putty configuration screen – select serial connection type and enter the port number. Connection speed can be left to whatever it was initially – it won’t affect the actual speed of the link. Click “Open”.
What happens next will depend on the Bluetooth stack and system security settings. You may get a notification about connection attempt followed by pin entry dialog. In this case, enter 1234 again. The Putty window will eventually open with “Hello from Arduino” string displayed on the line 1. From this point it should be possible to type in one windows and observe the output in another as shown on the following screenshot of two terminal windows side by side.
As we can see, the steps to establish a connection between an Arduino and a PC are pretty standard:
- One device needs to start looking for devices and another one must be willing to answer. In our case, a PC is the one that is looking looking and Arduino is the one that is willing
- After they find each other, a key exchange needs to happen. This is the step where we entered ‘1234’.
- After authentication, a service request needs to be made by one party and accepted by the other. In our case this is happening when a terminal session is open on a PC and is indicated by RFCOMM diagnostic messages in the Arduino window.
- Once service request is accepted data flow can begin
One can notice the same sequence when a Bluetooth headset is connected to a phone or phone is connected to a PC or any Bluetooth device is connected to any other Bluetooth device. The only difference is that any other Bluetooth service is more popular than SPP, therefore in our case the process is more involving. It is still quite simple on Windows 7 machines comparing to older OSes, like Windows XP, where the biggest difficulty is to actually find out the virtual COM port numbers created by the driver and which one of the two shall be used for outbound SPP connections.
This is it for today. Enjoy the article and please do not hesitate to ask questions if you have any issues.