Recently, after being inspired by this video of Arduino Bluetooth communication with Windows Phone 8 by Marcos Pereira, I got myself some new devices to play with:
The thing is: I don’t have a Windows Phone 8 yet and the Windows Phone 7.8 APIs do not support this kind of Bluetooth communication.
But: I have a Surface RT and with Windows 8.1 the RFCOMM API can be used to establish a serial link to the Arduino.
Arduino and Visual Studio
It happens that my developing skills are quite limited to C# and when I had to develop some Kinect software with Processing in early 2012, I almost freaked out. Arduino code is written in C++ and the standard Arduino IDE is derived from the Processing IDE. Fortunately there’s the Visual Studio extension VisualMicro which brings a very intuitive Arduino support for VS2010-VS2013. (not available for Express versions)
You can get VisualMicro for free at http://visualmicro.codeplex.com/. They also offer a pro-version (>$20) with support for Breakpoint-Debugging.
The Arduino IDE should be installed as well and you have to tell the VisualMicro extension where to find it.
Setting up the Arduino board
For the setup I’ll show here, I set up two LEDs, a potentiometer and of course the Bluetooth module (Fig.1).
Preparing the Code
The Arduino has to be loaded with some code to send and receive Bluetooth messages. I created a Visual Studio solution with a blank Windows 8.1 app and a new Arduino sketch and added a new header file “SoftwareSerial.h” (Fig.2). The code of SoftwareSerial.h can be found here.
The actual Arduino code can be studied in the sample application: http://code.msdn.microsoft.com/Bluetooth-communication-7130c260
For the Windows 8.1 app we build a class, BluetoothConnectionManager to simplify enumerating, connecting, send & receive and state management. The code of the BluetoothConnectionManager and the actual application can also be studied in the sample application.
For the Windows 8.1 app it’s very important to declare the right Bluetooth capabilites. The following snippet should be included in Package.appxmanifest:
<Capabilities> <m2:DeviceCapability Name="bluetooth.rfcomm"> <m2:Device Id="any"> <m2:Function Type="name:serialPort" /> </m2:Device> </m2:DeviceCapability> </Capabilities>
As soon as you have everything set up, the Arduino code can be deployed via DebugStart new instance and the Windows 8.1 app can be launched.
In the sample application I implemented broadcast of the potentiometer value and switching the LEDs on/off. Additionally the red LED automatically turns on/off depending on the analog value.
One problem that this implementation brings, which I did not yet solve, is that simultaneous sending of a message by either party causes the other one to crash or get an exception. But due to my lack of experience, I do not know how to lock the streams to prevent this from happening..
Anyways, have fun =)