Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to create a Bluetooth connection that can be used across your Windows Phone app

From Wiki
Jump to: navigation, search

This article explains how to create a Bluetooth connection that can be used in any page of a Windows Phone application.

WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
Devices(s): Lumia 820, Lumia 620
Compatibility
Platform(s): Windows Phone 8.0 and later
Windows Phone 8
Article
Created: ceriboo (06 May 2013)
Last edited: hamishwillee (04 Jul 2013)

Contents

Overview

The Dev Center documentation shows how to create and use a Bluetooth connection using a new StreamSocket. However in a real app you won't create a connection every time, but instead create a single instance to be used across your whole app.

This article shows how you can create a connection that works across all pages. Note that Bluetooth stream sockets only work with Windows Phone 8 and later.

Solution

The solution is to create a static StreamSocket as part of your App class. This can then be referenced throughout your app using its full scope.

Declare the static StreamSocket in App.xaml.cs as shown:

public static StreamSocket Socket;

To use this socket in a page you can reference it using its full scope App.Socket. The line below shows how you'd create the socket.

App.Socket = new StreamSocket();


Bluetooth for Windows Phone 8 shows in more detail how to find a Bluetooth device using PeerFinder, connect to it using Socket::ConnectAsync(), and send and receive information using App.Socket.OutputStream.WriteAsync and App.odbSocket.InputStream.ReadAsync as appropriate.

Code Sample

This code sample shows how to find peers and connect to them in several pages. File:HowToKeepBT.zip

Summary

This article showed how to create a static Bluetooth connection that can be used through your app.

This approach has been verified in the test app, and also my commercial apps:

Reference

Bluetooth for Windows Phone 8 (Dev Center)

Acknowledgements

This article was only possible with super advice from my Facebook friend Olivier Bacheliez.

This page was last modified on 4 July 2013, at 01:45.
107 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×