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. Thanks for all your past and future contributions.
How to create a Bluetooth connection that can be used across your Windows Phone app
This article explains how to create a Bluetooth connection that can be used in any page of a Windows Phone application.
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.
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.
This code sample shows how to find peers and connect to them in several pages. File:HowToKeepBT.zip
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:
Bluetooth for Windows Phone 8 (Dev Center)
This article was only possible with super advice from my Facebook friend Olivier Bacheliez.