An MVVM friendly tap+send and Bluetooth connection helper for Windows Phone 8
Early this year I blogged about TtcSocketHelper, an MVVM friendly helper object that allowed you connect two Windows Phone 8 devices to each other using a socket.I made two games with it– Pull the Rope and 2 Phone Pong, head-to-head style games that are played over two phones, exchanging JSON data to keep things in sync. A lot of things happened the last 10 months: super cheap devices entered the market – most notably the Nokia Lumia 520. It is a full fledged Windows Phone 8, but it’s super cheap and as a consequence it does not support NFC (and thus no tap+send). That apparently is of no concern to buyers, as according to AdDuplex stats, it took off like a bat out of hell. In just six months it grabbed what is now almost 33% of the Windows Phone 8 market. In other words, by limiting my app to NFC enabled phones I cut myself off from a significant – and growing – part of the market.
PhonePairConnectionHelper – supporting Bluetooth browsing
So I rewrote TtcSocketHelper into PhonePairConnectionHelper – basically the same class, but it now supports connecting phones using tap+send as well as by using the tried and tested way of Bluetooth pairing. Since October 21st 2 Phone Pong 1.3.0 is in the store, proudly powered by this component, now enabling my game to be played by 50% more people!
In this article I am going to concentrate on connecting via Bluetooth, as connecting via tap+send is already described in my article from January 2013.
The start of the object is simple enough again, and actually looks a lot like the previous incarnation:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; using GalaSoft.MvvmLight.Messaging; using Windows.Foundation; using Windows.Networking.Proximity; using Windows.Networking.Sockets; using Windows.Storage.Streams; namespace Wp7nl.Devices { public class PhonePairConnectionHelper { private ConnectMethod connectMode; public PhonePairConnectionHelper() { Messenger.Default.Register<NavigationMessage>(this, ProcessNavigationMessage); PeerFinder.TriggeredConnectionStateChanged += PeerFinderTriggeredConnectionStateChanged; PeerFinder.ConnectionRequested += PeerFinderConnectionRequested; } private void ProcessNavigationMessage(NavigationMessage message) { Debug.WriteLine("PhonePairConnectionHelper.ProcessNavigationMessage " + message.NavigationEvent.Uri); if (message.IsStartedByNfcRequest) { Start(); } } } }The most notable is now that the setup is being done in the constructor. The class still listens to the NavigationMessage and if so, it still calls the start method (this is for the tap+send route, that still works). The PeerFinder is set up to listen to TriggeredConnecState events (i.e. events that happen after a tap+send) but also - and this is important – to plain ole Bluetooth requests (ConnectionRequested event). The start method now, is pretty much changed:
public void Start(ConnectMethod connectMethod = ConnectMethod.Tap, string displayAdvertiseName = null) { PeerFinder.Stop(); connectMode = connectMethod; if (!string.IsNullOrEmpty(displayAdvertiseName)) { PeerFinder.DisplayName = displayAdvertiseName; } PeerFinder.AllowBluetooth = true; PeerFinder.Start(); // Enable browse if (connectMode == ConnectMethod.Browse) { PeerFinder.FindAllPeersAsync().AsTask(). ContinueWith(p => FirePeersFound(p.Result)); } }The ConnectMethod enumeration, as you can already see from the code, enables you to indicate which method you want to use:
namespace Wp7nl.Devices { public enum ConnectMethod { Tap, Browse } }The Start method has an optional parameter that gives you the ability to choose a connection method, and if necessary add a display name. The display name only applicable for the Bluetooth method. The interesting thing is – if you don’t provide a name, like I did in the sample, it will just use the phone’s name as you have provided it (by giving it a name via Explorer if you connect if via a USB cable to your computer) - abbreviated to 15 characters. Why this is only 15 characters – your guess is as good as mine.
If you call this method without any parameters, it uses default values and will try to connect using tap+send, just like before.
Before selecting a method, you might want to check what connection methods the device support by using the two helper properties in the object:
public bool SupportsTap { get { return (PeerFinder.SupportedDiscoveryTypes & PeerDiscoveryTypes.Triggered) == PeerDiscoveryTypes.Triggered; } } public bool SupportsBrowse { get { return (PeerFinder.SupportedDiscoveryTypes & PeerDiscoveryTypes.Browse) == PeerDiscoveryTypes.Browse; } }Mind you – the helper is not so intelligent that it refuses to connect in a non-supported mode. And I don’t check for it in the sample app either. That is up to your app. But anyway – back up a little. If you try to connect via Bluetooth, the component tries to find all peers – that is, other phones running the same app, using FindAllPeersAsync, and fires the PeersFound event when it is done:
private void FirePeersFound(IEnumerable<PeerInformation> args) { if (PeersFound != null) { PeersFound(this, args); } } public event TypedEventHandler<object, IEnumerable<PeerInformation>> PeersFound;Your app has to subscribe to this event, and it then gets a list of “PeerInformation” objects. And PeerEvent has a number of properties, but the only one you will need to concern yourself is the “DisplayName” property. You display that in a list, a dropdown or whatever in your app, and after the user has made a selection, you call the Connect method:
public void Connect(PeerInformation peerInformation) { DoConnect(peerInformation); } private void DoConnect(PeerInformation peerInformation) { PeerFinder.ConnectAsync(peerInformation).AsTask().ContinueWith(p => { socket = p.Result; StartListeningForMessages(); PeerFinder.Stop(); FireConnectionStatusChanged(TriggeredConnectState.Completed); }); }And boom. You have a connection to the phone you have selected. The only thing that’s now missing is that the phone you have connected to, does not have a connection to you yet. But as you made the connection, the PeerFinder fired the ConnectionRequested event. Remember we set up a listener “PeerFinderConnectionRequested” to that in the constructor? Well guess what, part of the payload of the arguments of that event method gets is a PeerInformation object as well, containing the information to connect back to the phone that just connected to you :-). Which makes connecting back pretty easy:
private void PeerFinderConnectionRequested(object sender, ConnectionRequestedEventArgs args) { if (connectMode == ConnectMethod.Browse) { DoConnect(args.PeerInformation); } }And done. We know have a two-way socket connection using Bluetooth and we can call the SendMessage method just like before, and listen to result by subscribing to MessageReceived event. The only thing I did break in this helpers’ interface with regard to it predecessor is the ConnectionStatusChanged event type. This used to be
public event TypedEventHandler<object, TriggeredConnectionStateChangedEventArgs> ConnectionStatusChanged;and now is
public event TypedEventHandler<object, TriggeredConnectState> ConnectionStatusChanged;So if you subscribe to this event, you don’t have to check for args.State, but just for args itself. This was necessary to be able to fire connect events from DoConnect.
The rest of the class is nearly identical to the old TtcSocketHelper, and I won’t repeat myself here.
To use this class
First, decide if you are going to for for the tap+send route or the Bluetooth route. My apps check the SupportsTap and SupportsBrowse properties. If NFC (thus tap+send) is supported, I offer both options and give the users a choice to select a connect method. If is not supported, I offer only Bluetooth.
For the NFC route, the way to go still is:
- Make sure you app does fire a NavigationMessage as described here
- Make a new PhonePairConnectionHelper .
- Subscribe to its ConnectionStatusChanged event
- Subscribe to its MessageReceived event
- Call Start
- Wait until a TriggeredConnectState.Completed comes by
- Call SendMessage – and see them appear in the method subscribed to MessageReceived on the other phone.
- Make a new PhonePairConnectionHelper .
- Subscribe to its ConnectionStatusChanged event
- Subscribe to its MessageReceived event
- Subscribe to its PeersFoundevent
- Call Start with ConnectMethod.Browse and a display name
- Wait until a PeersFound event comes by
- Select a peer to connect to in your app
- Call the Connect method with the selected peer
- Wait until a TriggeredConnectState.Completed comes by
- Call SendMessage – and see them appear in the method subscribed to MessageReceived on the other phone.
So basically, there isn’t that much difference ;-)
Changes in original sample
The fun thing is, you don’t even have to change that much to the existing demo solution. I added to the viewmodel the following code:
#region Bluetooth stuff private bool useBlueTooth; public bool UseBlueTooth { get { return useBlueTooth; } set { if (useBlueTooth != value) { useBlueTooth = value; RaisePropertyChanged(() => UseBlueTooth); } } } private PeerInformation selectedPeer; public PeerInformation SelectedPeer { get { return selectedPeer; } set { if (selectedPeer != value) { selectedPeer = value; RaisePropertyChanged(() => SelectedPeer); } } } public ObservableCollection<PeerInformation> Peers { get; private set; } private void PeersFound(object sender, IEnumerable<PeerInformation> args) { Deployment.Current.Dispatcher.BeginInvoke(() => { Peers.Clear(); args.ForEach(Peers.Add); if (Peers.Count > 0) { SelectedPeer = Peers.First(); } else { ConnectMessages.Add("No contacts found"); } }); } public ICommand ConnectBluetoothContactCommand { get { return new RelayCommand(() => { connectHelper.Connect(SelectedPeer); Peers.Clear(); }); } } #endregionBasically:
- A property to determine whether you are using Bluetooth or not (not = tap+send)
- A property holding the selected Peer
- A list of available peers
- The callback for the helper’s PeersFound event. Not that everything here happens within a Dispatcher. As the PeersFound event comes back from a background thread, it has no access to the UI – but since it updates several bound properties, it needs that access – hence the Dispatcher.
- and a command to allow the user to select a certain peer. Notice the Peers.Clear after the Peer is selected. That is because I use my HideWhenCollectionEmptyBehavior to display the UI for showing and selecting peers. This behavior is in the sample solution as code as I kind of forgot to add this to the last edition of my wp7nl library on codeplex.*ahem*
connectHelper = new PhonePairConnectionHelper();
connectHelper.ConnectionStatusChanged += ConnectionStatusChanged;
connectHelper.MessageReceived += TtsHelperMessageReceived;
connectHelper.PeersFound += PeersFound; // Added for Bluetooth support
And there’s also a little change in the StartCommand to accommodate the fact that if the user selects Bluetooth, the connectHelper should be called in a slightly different way.public ICommand StartCommmand
{
get
{
return new RelayCommand(
() =>
{
ConnectMessages.Add("Connect started...");
CanSend = false;
CanInitiateConnect = false;
// Changed for Bluetooth.
if(UseBlueTooth)
{
connectHelper.Start(ConnectMethod.Browse);
}
else
{
connectHelper.Start();
}
});
}
}
<RadioButton Content="tap+send" IsChecked="{Binding UseBlueTooth, Converter={StaticResource BooleanInvertConvertor}, Mode=TwoWay}" IsEnabled="{Binding CanInitiateConnect, Mode=OneWay}" /> <RadioButton Content="bluetooth" IsChecked="{Binding UseBlueTooth, Mode=TwoWay}" HorizontalAlignment="Right" Margin="0" IsEnabled="{Binding CanInitiateConnect, Mode=OneWay}" />which is not quite rocket science, and some more code to give a user a simple UI to view and select found peers:
<Grid x:Name="bluetoothconnectgrid" VerticalAlignment="Bottom">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<i:Interaction.Behaviors>
<behaviors:HideWhenCollectionEmptyBehavior Collection="{Binding Peers}"/>
</i:Interaction.Behaviors>
<toolkit:ListPicker x:Name="OpponentsListPicker" Margin="12,0" VerticalAlignment="Top"
ExpansionMode="FullScreenOnly"
ItemsSource="{Binding Peers}" SelectedItem="{Binding SelectedPeer, Mode=TwoWay}" />
<Button Content="select contact" Height="72" Grid.Row="1"
Command="{Binding ConnectBluetoothContactCommand, Mode=OneWay}"/>
</Grid>
Also not very complicated – a listpicker displays the peers and selects a peer, and the button fires the command to do the actual connecting. I rather like the use of HideWhenCollectionEmptyBehavior here – this automatically shows this piece of UI as there are peers in the list, and hides it when they are not. It’s rather elegant, if I may say so myself. On the picture right you can see it shows the name of my 920 – abbreviated to 15 characters.After the connection has been made – either by Bluetooth or by tap+send – you can use this app to chat just like the previous version.
Important things to know
- If you are a developer who wants protect your customers from disappointments, you have checked the “NFC” as requirement in the WMAppManifest.xml when you built an app on top of my previous TtcSocketHelper. Remove that checkmark now. Or else your app still won’t be available for phones like the 520 that don’t have NFC. And that quite defies the point of this whole new class.
- I have found out Bluetooth browsing on Windows Phone 8 has some peculiar things to take into account
- You still have to be quite close together – somewhere within the meter range – to find peers. Once the connection is made, you can move further apart
- The first phone to start searching for a peer, usually finds nothing. The second one finds one opponent, the third one two. The search process usually doesn’t last very long – in most cases just a few seconds – before the PeerFinder either gives up or comes back with a peer. It’s advisable not to call Connect automatically if you find only one peer – that might not be the one the user was looking for. Always keep the user in control.
- If you connect via tap+send, you will only need to start the app on one of the phones and press the connect button – the other phone will automatically fire up the app (or even download it if it is not there) when the devices are tapped together.
- For the Bluetooth route, the app needs to be started on both phones and on both phones you will need to press the connect button.
Conclusion
The app market is always in flux, that’s true for all platforms but that sure goes for Windows Phone. It’s important to stay in touch with those developments. There’s a lot of growth in the Windows Phone market – especially on the low end of the phones. Walking the extra mile for those groups is important – cater for as much users as you can. I hope I helped you a little with that, both by general example and actual code. Demo solution, as always, can be downloaded here.