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! – Chicago

Posted April 8, 2014 4:04 AM Categories: .NET | Async | Events | VSLive Chicago

I will be presenting 2 sessions and speaking on the closing panel at Visual Studio Live! in Chicago this May 5-8.  The conference is a full 4 day event and there’s still time to register and save $200 if you register before 4/16/14!  For this event, I’ll be presenting:

TH10 Performance and Diagnostics Hub in Visual Studio 2013
Date:
Thursday, May 8, 2014
Time: 11:00am – 12:15pm
Level: Intermediate

Visual Studio 2013 has introduced a new Performance and Diagnostics hub which encompasses a variety of new and old tools to profile your Windows Store applications. In this session, we will take a detailed look at all of the tools that the Performance and Diagnostics Hub has to offer with both XAML and HTML/JavaScript Windows Store apps.

TH20 Writing Asynchronous Code Using .NET 4.5 and C# 5.0
Date:
Thursday, May 8, 2014
Time: 3:00pm – 4:15pm
Level: Introductory

A variety of new features were added to the .NET 4.5 Framework, including support for easily writing asynchronous code. This session will introduce the concepts of asynchronous programming, .NET Tasks, how to ensure your UI remains responsive, and how to do all of this properly in your own applications using the new "async" and "await" keywords. If you're looking for ways to provide a better, smoother experience for your users, don't miss this session!

I’ll also be speaking on the Conference Wrap-Up Panel at 4:30pm on Thursday.  Come and ask questions!

CHSPK23

Get more information on the conference at:

Visual Studio Live! – Las Vegas

Posted January 10, 2014 11:18 AM Categories: .NET | Bing | Events | Visual Studio | VSLive Vegas | Windows 8.1

imageI will be presenting 3 sessions at Visual Studio Live! in Las Vegas this March 10-14.  The conference is a full 5 day event and there’s still time to register and save $500 if you register before 1/22/14!  For this event, I’ll be presenting:

LT20 Controlling Hardware using Windows 8.1
Date:
Tuesday, March 11, 2014
Time: 4:15pm – 5:30pm
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!

LT27 Performance and Diagnostics Hub in Visual Studio 2013
Date:
Wednesday, March 12, 2014
Time: 1:15pm – 2:30pm
Level: Intermediate

Visual Studio 2013 has introduced a new Performance and Diagnostics hub which encompasses a variety of new and old tools to profile your Windows Store applications. In this session, we will take a detailed look at all of the tools that the Performance and Diagnostics Hub has to offer with both XAML and HTML/JavaScript Windows Store apps.

LW30 Overview of the Bing Platform
Date:
Wednesday, March 12, 2014
Time: 4:30pm – 5:45pm
Level: Introductory

Bing has a variety of controls, APIs, and web services designed to enhance your application and provide users with an interactive way to explore the world around them. In this session we will take a look at several of these items and how they can be used from a Windows desktop, Store or Phone application.

LSPK35

Get more information on the conference at:

Visual Studio Live! - Chicago

Posted May 6, 2013 2:26 PM Categories: .NET | Events | VSLive Chicago | Windows 8 | Windows Phone

imageI will be presenting a session at Visual Studio Live! in Chicago this May 13-17 at the Hilton Chicago.  The conference is a full 5 day event and there’s still time to register and save $400!

W01 Building Your First Windows Phone 8 Application
Date:
Wednesday, May 15, 2013
Time: 9:15 am - 10:30 am
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 platforms architecture, including the developer tools and device emulator and touch input.

W13 Windows Store Application Contracts and Extensibility
Date:
Wednesday, May 15, 2013
Time: 3:00 pm – 4:15 pm
Level: Intermediate

In this session, we will explore these contracts and how they can be used in any Windows Store application to provide a seamless, integrated experience for users.  Contracts are a new feature of Windows 8 Store applications that allow interaction with the operating system and other Store applications in a standard way. With contracts, applications can implement interfaces to allow searching, sharing, settings, app-to-app file picking, and more. In this session, we will explore these contracts and how they can be used in any Windows Store application to provide a seamless, integrated experience for users.

Get more information on the conference at:

I will also be speaking at Visual Studio Live! in Redmond this coming August.  More details on that soon, but you can register now and save $400 on this conference as well!

RDSPK26

Async Goodness at Tech Valley .NET User Group

Posted March 5, 2012 9:14 AM Categories: .NET | Async | C# | Events | TVUG

imageI’ll be giving a session to the Tech Valley .NET Users Group (TVUG) Tuesday, March 13th at 6:30pm on the new async goodness that exists in .NET 4.5 and C# 5.0, as well as the Visual Studio Async CTP which can be used with Visual Studio 2010.  Here's the description:

Title: Writing Asynchronous Code Using .NET 4.5 and C# 5.0
Where: Tyler Technologies, Latham, NY
When: Tuesday, March 13th, 2012, 6:30-8:30PM

A variety of new features were added to the .NET 4.5 Framework, including support for easily writing asynchronous code. This session will introduce the concepts of asynchronous programming, .NET Tasks, how to ensure your UI remains responsive, and how to do all of this properly in your own applications using the new async and await keywords. If you’re looking for ways to provide a better, smoother experience for your users, don’t miss this session!

You will learn:

  • Learn about the pros/cons of asynchronous code
  • Learn how to write and use asynchronous code in .NET 4.5
  • Learn how to maintain a constantly available and responsive user interface

Hope to see you all there!

My Info

  • View Brian Peek's profile on LinkedIn

Sponsored Ad

My Book

Sponsored Ad