Connect to a Bluetooth Serial Device with Xamarin.Android

Posted April 27, 2014 8:48 AM Categories: .NET | Android | Bluetooth | Hardware | Xamarin

Xamarin channelI have been exploring the Xamarin tools recently and decided to try my hand at connecting some Bluetooth hardware to Android for a project that will be released soon.  I have learned quite a bit about Bluetooth on Android and decided to write up a short description on how to connect to a Bluetooth serial device via RFCOMM/SPP.

First, grab an instance of the default BluetoothAdapter on the Android device and determine if it is enabled:

BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
if(adapter == null)
    throw new Exception("No Bluetooth adapter found.");

if(!adapter.IsEnabled)
    throw new Exception("Bluetooth adapter is not enabled.");

Next, get an instance of the BluetoothDevice representing the physical device you’re connecting to.  You can get a list of currently paired devices using the adapter’s BondedDevices collection.  I use some simple LINQ to find the device I’m looking for:

BluetoothDevice device = (from bd in adapter.BondedDevices 
                          where bd.Name == "NameOfTheDevice" select bd).FirstOrDefault();

if(device == null)
    throw new Exception("Named device not found.");
Finally, use the device’s CreateRfCommSocketToServiceRecord method, which will return a BluetoothSocket that can be used for connection and communication. Note that the UUID specified below is the standard UUID for SPP:
_socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
await _socket.ConnectAsync();

Now that the device is connected, communication occurs via the InputStream and OutputStream properties which live on the BluetoothSocket object  These properties are standard .NET Stream objects and can be used exactly as you’d expect:

// Read data from the device
await _socket.InputStream.ReadAsync(buffer, 0, buffer.Length);

// Write data to the device
await _socket.OutputStream.WriteAsync(buffer, 0, buffer.Length);

That’s it!  Now to get things working with iOS…

Visual Studio Live! – Orlando

Posted September 17, 2013 2:33 PM Categories: Hardware | VSLive Orlando | Windows 8.1

Update: Make that 3 sessions!

imageI will be presenting 3 sessions at Visual Studio Live! in Orlando this November 18-22.  The conference is a full 5 day event and there’s still time to register and save some cash!  For this event, I’ll be presenting:

VST01 What's New in Windows 8.1 for Developers
Date:
Tuesday, November 19, 2013
Time: 9:30 am – 10:45 am
Level: Introductory

Windows 8.1 contains thousands of new APIs which can make your application even better. In this session, you will get an overview of what’s new to developers in Windows 8.1, and how to use these features to make your apps even better. From new XAML controls to PDF support to text-to-speech, you won’t want to miss this session if you’re a Windows 8 developer!

VST05 Controlling Hardware using Windows 8.1
Date:
Tuesday, November 19, 2013 
Time: 11:00 am – 12:15 pm
Level: Introductory

Windows 8.1 allows developers to communicate with a variety of devices through several interfaces. In this session, we will look at how to talk to hardware connected via USB and Bluetooth, as well as some of the new APIs which allow Windows 8.1 to talk to Point of Sale (POS) devices, scanners, and even 3D printers using real examples with real hardware!

VST09 Building Your First Windows Phone 8 Application 
Date:
Tuesday, November 19, 2013 
Time: 2:00pm – 3:15 pm
Level: Introductory

Expand your mobile horizons with Windows Phone 8! Developing applications for the phone is extremely similar to building a XAML application for the desktop. In this session, I will demonstrate the fundamentals of Windows Phone development while building a very simple application which covers all of the important points of the new platform’s architecture, including the developer tools and device emulator and touch input.

VSLSP19

Get more information on the conference at:

Pinball 2000 Software Update with Windows 7 x64

Posted August 23, 2012 10:20 PM Categories: Gaming | General | Hardware | Pinball

Warning: pb2kThis is a pretty off-topic post about pinball, but maybe it'll help someone searching for the information.

Backstory: My dad was a coin-op operator.  I'm the son of a coin-op operator.  As you might imagine, I have a just a tiny bit of love for video games and pinballs.  When my dad passed away in 2008, I decided to keep my favorite pins from the route:  Funhouse, Addams Family, Theatre of Magic, Tales of the Arabian Nights, and Revenge from Mars.

After sitting in storage for a couple of years, the software update sitting on Revenge from Mars would no longer checksum properly.  The game was running fine, but obviously something was wrong.  After lots of other work on the machine, it was time to update the firmware again to version 1.5.

Finding the software and ROM images was easy.  Everything is available at PlanetaryPinball.com.  Anyone can just grab the Pinball 2000 Update Manager and the appropriate software update for the machine to be updated.  However, trying to install and run the Pinball 2000 Update Manager was a different story.  The installer choked when running on my Windows 7 x64 machine.  Not surprising since this application and installer was written 13 years ago.  Luckily, I was able to install the app in a Windows XP x86 virtual machine and copy the installed files over to my laptop.

Unfortunately, this didn't immediately get me much further since the GUI updater requires some OCX files to be registered, and that's a circle of hell I didn't want to enter.  Exploring a bit further, I found the fupdate.exe command line application that actually does the flashing.  The Pinball 2000 Update Manager is just a pretty GUI wrapper.  Even more interesting, the source code for the fupdate.exe is included in the package.  It's a win32 command line app compiled against a very old version of Cygwin.  I have ripped out the important files into a separate archive.  If you're just interested in updating a Pinball 2000 machine from a modern computer and OS, you can download fupdate here.

Running this command line tool on the Windows 7 x64 laptop worked just fine.  Here's the command line to start the update process:

fupdate -p\pin2000_50070_0150_07252000_B_10000000\50070 -fpin2000_50070_0150_ COM1
  • The –p switch denotes where the software update files are.
  • The –f switch specifies a file prefix for each file in the update.  For Revenge from Mars, every ROM image is prefaced with the above string.
  • The command's final switch is the COM port to use for the update.

The USB to serial adapter I used on my laptop installed itself as COM17.  Trying to run the above command with the COM17 switch failed.  I was able to go into the properties of the COM adapter and force it on to COM1 which worked just fine.  You can do this by going into Device Manager, right-clicking on the port you're using and selecting Properties.  In the dialog that pops up, hit the Advanced button on the Port Settings tab, then select a new COM port from the drop-down as shown:

image

When you run the above fupdate.exe program, the update process runs, and you should see output in the console window as shown, along with a progress bar on the pinball's monitor:

bootdata.rom:    0 blocks of   64 (   0) sent, total bytes remaining 4980724
im_flsh0.rom:    0 blocks of 1201 (  64) sent, total bytes remaining 4947956
im_flsh0.rom:   64 blocks of 1201 ( 128) sent, total bytes remaining 4915188

...

      sf.rom: 1920 blocks of 2048 (9600) sent, total bytes remaining 65536
      sf.rom: 1984 blocks of 2048 (9664) sent, total bytes remaining 32768
************: 9728 blocks of 9728 (9728) sent, total bytes remaining 0
Update complete - total blocks 9728 (4980724 bytes)

That's it!  Once the update completes (about 15-20 minutes in my setup), the newly flashed software will be verified, and the pinball is ready to be played!

HP TouchSmart 420t Recovery Discs + Software

Posted October 8, 2011 12:53 PM Categories: HP TouchSmart | General | Hardware

imageMy mom desperately needed a new computer (she's still running XP on a very old Dell), so I decided to pick up one of HP's new TouchSmart 420t machines for her, thinking it'll also be a great machine for Windows 8 when it arrives.

HP, in their infinite wisdom, decided that, among other annoyances, one can only create a single set of recovery media from the included Recovery Media Creation application.  Once you make the set, that's it.  If you lose the media, or if you want to make a second set on USB instead of DVDs, you're out of luck, short of ordering the media from HP for a price.

If you are stuck in this situation, there is an easy fix, assuming you still have the Recovery partition available on the hard drive.

  1. Open up an Administrator command prompt window
  2. Change to the D drive (D:)
  3. Change directories to D:\HP\CDCreatorLog (cd \hp\CDCreatorLog)
  4. Delete the ResumeSnapshot.xml file (del ResumeSnapshot.xml)
  5. Restart the Recovery Media Creation application

Alternatively, you could try downloading, saving, and running this batch file as an Administrator: HPRecoveryReset.bat .

A second issue I have is that they don't have their TouchSmart software and other client-side pieces on the driver and software support page on their site.  So, if you want to do a clean install of Windows and then selectively add back certain features, such as the HP TouchSmart shell, or the HP client utilities, you are, once again, out of luck.  However, there is a bit of a workaround.  On the C: drive in the SWSETUP directory, you will find a number of these applications in their installer state so they can be put back on the machine.  Simply copy this directory off somewhere safe, clean install Windows 7, restore this directory, and then install the software and drivers you need/want.

This may (and probably will) work for other models of machines from HP that use the same recovery software and drive setup.  Of course, this is a YMMV fix, but it has worked here without issue.  However, it does make me question purchasing another HP machine in the future.  If anyone from HP happens to stumble upon this, please take this into consideration.  Don't make it difficult for users to restore their machine and/or do a clean install of Windows.  You're not winning people over by making things cumbersome.  Then again, you're out of the hardware business so I suppose it doesn't much matter.

I hope to throw Windows 8 on the machine for a quick spin before I remove it and hand it over to my mom and will report back here if I find anything noteworthy in the install/usage of Win8 on the machine.

Kinect for Windows SDK is here!

Posted June 16, 2011 8:47 AM Categories: Gaming | .NET | Coding4Fun | Hardware | C/C++ | C# | NUI | Kinect

image

Hooray!  I can finally talk about this!  As I've alluded to previously, I had a hand (one of many) in the managed portion of the SDK and what was originally shown at MIX. Since then the SDK has changed a bit but it's finally ready to go!

The Kinect for Windows SDK is now available for download on the Microsoft Research site.  We have also launched several samples over at Coding4Fun that you can begin using immediately:

Coding4Fun Kinect Toolkit

You definitely want to download this one when you get started.  This toolkit contains a variety of extension methods and controls to make using the Kinect for Windows SDK even easier to use.  Some of my code appears in this one.  Smile

Kinect Mouse Cursor

This sample is entirely mine.  Kinect Mouse Cursor is a demo application that uses the Kinect for Windows SDK and its skeletal tracking features to allow a user to use their hands to control the Windows mouse cursor.  Use your right hand to move the cursor, and raise your left hand to press the left mouse button.  Use the check box to switch hands…
image

Kinect Paint

Kinect Paint is a skeleton tracking application that allows you to become the paint brush!  IdentityMine built this for us.

imageimageimage

imageimage

Kinect for Windows SDK Quickstarts

This is a series of quick start videos starring the lovely and talented Dan Fernandez, who walks you through the basics of Kinect development from the very beginning.  Don't miss these!

Coding4Fun's Kinect for Windows SDK Blog

Add a bookmark to this now.  Coding4Fun will be tracking awesome projects using the new SDK here.  Have something to show off?  Tell us!

Kinect Hack-a-thon

In coordination with the Kinect launch, developers were invited out to the Microsoft campus to develop applications in a 24 hour "code-a-thon".  Some health issues prevented me from attending this event, but I'm looking forward to seeing what these people came up with…

We will have more samples and fun projects at Coding4Fun soon, so be sure to check back there (and here) regularly for more Kinect goodness.  Until then, enjoy the new SDK, our new samples, and see what you can build!  I'd love to hear about any projects you create with these tools…

My Info

  • View Brian Peek's profile on LinkedIn

Sponsored Ad

My Book

Sponsored Ad