Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Finally got the basic pebble app to connect on WP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Bost committed Sep 26, 2014
1 parent 52564df commit e33ca6b
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Flint.Core/IBluetoothConnection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading.Tasks;

namespace Flint.Core
{
public interface IBluetoothConnection
{
event EventHandler<BytesReceivedEventArgs> DataReceived;
void Open();
Task OpenAsync();
void Close();
void Write( byte[] data );
}
Expand Down
2 changes: 1 addition & 1 deletion Flint.Core/Pebble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task ConnectAsync()
using (IResponseTransaction<PhoneVersionResponse> responseTransaction =
_responseManager.GetTransaction<PhoneVersionResponse>())
{
_PebbleProt.Connect();
await _PebbleProt.ConnectAsync();

response = responseTransaction.AwaitResponse(ResponseTimeout);
}
Expand Down
9 changes: 5 additions & 4 deletions Flint.Core/PebbleProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Flint.Core
{
Expand All @@ -24,7 +25,7 @@ public PebbleProtocol( IBluetoothConnection connection )
{
_blueToothConnection = connection;

_blueToothConnection.DataReceived += serialPortDataReceived;
_blueToothConnection.DataReceived += SerialPortDataReceived;
//TODO: Push this on to the clients.... do we even care if there is an error?
//_BlueToothPort.ErrorReceived += serialPortErrorReceived;
}
Expand All @@ -38,9 +39,9 @@ public IBluetoothConnection Connection

/// <summary> Connect to the Pebble. </summary>
/// <exception cref="System.IO.IOException">Passed on when no connection can be made.</exception>
public void Connect()
public async Task ConnectAsync()
{
_blueToothConnection.Open();
await _blueToothConnection.OpenAsync();
}

public void Close()
Expand Down Expand Up @@ -75,7 +76,7 @@ public void SendMessage( ushort endpoint, byte[] payload )
_blueToothConnection.Write(Util.CombineArrays(payloadSize, endPoint, payload));
}

private void serialPortDataReceived( object sender, BytesReceivedEventArgs e )
private void SerialPortDataReceived( object sender, BytesReceivedEventArgs e )
{
lock ( _byteStream )
{
Expand Down
53 changes: 29 additions & 24 deletions Pebble.WP/Bluetooth/PebbleScanner.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using System;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Storage.Streams;
using Flint.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking;
using Windows.Networking.Proximity;
using Flint.Core;
using Windows.Networking.Sockets;

namespace Pebble.WP.Bluetooth
{
Expand All @@ -13,51 +20,49 @@ public static class PebbleScanner
/// Detect all Pebble bluetooth connections that have been paired with this system.
/// </summary>
/// <returns></returns>
public static Task<IList<Flint.Core.Pebble>> DetectPebbles()
public static async Task<IList<Flint.Core.Pebble>> DetectPebbles()
{
return Task.FromResult<IList<Flint.Core.Pebble>>(new[] { new Flint.Core.Pebble(new PebbleBluetoothConnection(null), "Test") });

// Configure PeerFinder to search for all paired devices.
//PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
//return (await PeerFinder.FindAllPeersAsync()).Select(
// x => new Flint.Core.Pebble(new PebbleBluetoothConnection(x), x.DisplayName)).ToList();

// Select a paired device. In this example, just pick the first one.
//PeerInformation selectedDevice = pairedDevices[0];
// Attempt a connection
//StreamSocket socket = new StreamSocket();
// Make sure ID_CAP_NETWORKING is enabled in your WMAppManifest.xml, or the next
// line will throw an Access Denied exception.
// In this example, the second parameter of the call to ConnectAsync() is the RFCOMM port number, and can range
// in value from 1 to 30.
//await socket.ConnectAsync(selectedDevice.HostName, "1");
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
return (await PeerFinder.FindAllPeersAsync()).Select(
x => new Flint.Core.Pebble(new PebbleBluetoothConnection(x), x.DisplayName)).ToList();
}

private class PebbleBluetoothConnection : IBluetoothConnection
{
public event EventHandler<BytesReceivedEventArgs> DataReceived;

private readonly PeerInformation _PeerInformation;
private readonly StreamSocket _socket = new StreamSocket();

public PebbleBluetoothConnection(PeerInformation peerInformation)
{
//if (peerInformation == null) throw new ArgumentNullException("peerInformation");
if (peerInformation == null) throw new ArgumentNullException("peerInformation");
_PeerInformation = peerInformation;
}

public void Open()
public async Task OpenAsync()
{
throw new NotImplementedException();
try
{
await _socket.ConnectAsync( _PeerInformation.HostName, "1" );

Debug.WriteLine("Success");
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}

public void Close()
{
throw new NotImplementedException();
_socket.Dispose();
}

public void Write(byte[] data)
{
throw new NotImplementedException();
_socket.OutputStream.WriteAsync(data.AsBuffer());
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Pebble.WP/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="Pebble.WP.App">
<m3:VisualElements DisplayName="Pebble.WP" Square150x150Logo="Assets\Logo.png" Square44x44Logo="Assets\SmallLogo.png" Description="Pebble.WP" ForegroundText="light" BackgroundColor="transparent">
<m3:VisualElements DisplayName="Simply Pebble" Square150x150Logo="Assets\Logo.png" Square44x44Logo="Assets\SmallLogo.png" Description="A simple app for managing pebble watches." ForegroundText="light" BackgroundColor="transparent">
<m3:DefaultTile Wide310x150Logo="Assets\WideLogo.png" Square71x71Logo="Assets\Square71x71Logo.png">
</m3:DefaultTile>
<m3:SplashScreen Image="Assets\SplashScreen.png" />
Expand All @@ -25,6 +25,10 @@
</Applications>
<Capabilities>
<Capability Name="internetClientServer" />
<DeviceCapability Name="proximity" />
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="name:serialPort" />
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>
5 changes: 5 additions & 0 deletions Pebble.WP/Pebble.WP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
<Name>Flint.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="InTheHand.Phone.Bluetooth">
<HintPath>..\..\32feet\InTheHand.Phone.Bluetooth\InTheHand.Phone.Bluetooth\Bin\Debug\InTheHand.Phone.Bluetooth.dll</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '12.0' ">
<VisualStudioVersion>12.0</VisualStudioVersion>
</PropertyGroup>
Expand Down
6 changes: 5 additions & 1 deletion Pebble.WP/PivotPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ private void SecondPivot_Loaded(object sender, RoutedEventArgs e)
/// </summary>
/// <param name="e">Provides data for navigation methods and event
/// handlers that cannot cancel the navigation request.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var pebble = e.Parameter as Flint.Core.Pebble;
if (pebble != null)
{
await ViewModel.SetPebbleAsync(pebble);
}

_NavigationHelper.OnNavigatedTo(e);
}
Expand Down
4 changes: 3 additions & 1 deletion Pebble.WP/ViewModel/ConnectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Windows.UI.Xaml.Controls;
using Pebble.WP.Bluetooth;
using Pebble.WP.Common;
using Pebble = Flint.Core.Pebble;

namespace Pebble.WP.ViewModel
{
Expand Down Expand Up @@ -50,6 +49,9 @@ public async Task ScanForPairedDevicesAsync()
_pebbles.Clear();
foreach (var pebble in await PebbleScanner.DetectPebbles())
_pebbles.Add(pebble);

if (_pebbles.Count == 1)
SelectedPebble = _pebbles[0];
}

private void OnConnect()
Expand Down
27 changes: 25 additions & 2 deletions Pebble.WP/ViewModel/InfoViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
namespace Pebble.WP.ViewModel
using System;
using System.Threading.Tasks;
using Flint.Core.Responses;

namespace Pebble.WP.ViewModel
{
public class InfoViewModel : ViewModelBase
{

private const string TIME_FORMAT = "";

private string _watchTime;
public string WatchTime
{
get { return _watchTime; }
set { Set(ref _watchTime, value); }
}

public async Task SetPebbleAsync(Flint.Core.Pebble pebble)
{
if (pebble == null) throw new ArgumentNullException("pebble");

if (pebble.Alive)
{
TimeResponse timeResponse = await pebble.GetTimeAsync();
if (timeResponse.Success)
WatchTime = timeResponse.Time.ToString("G");
}
}
}
}
14 changes: 14 additions & 0 deletions Pebble.WP/ViewModel/PivotPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

using System;
using System.Threading.Tasks;

namespace Pebble.WP.ViewModel
{
public class PivotPageViewModel : ViewModelBase
Expand All @@ -14,5 +17,16 @@ public InfoViewModel Info
get { return _info; }
private set { Set(ref _info, value); }
}

public async Task SetPebbleAsync(Flint.Core.Pebble pebble)
{
if (pebble == null) throw new ArgumentNullException("pebble");
if (pebble.Alive == false)
{
await pebble.ConnectAsync();
}

await Info.SetPebbleAsync(pebble);
}
}
}
4 changes: 3 additions & 1 deletion Windows.Pebble/Bluetooth/PebbleScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Threading.Tasks;
using Flint.Core;
using InTheHand.Net.Sockets;

Expand Down Expand Up @@ -48,9 +49,10 @@ public PebbleBluetoothConnection(string port)
_SerialPort.DataReceived += SerialPortOnDataReceived;
}

public void Open()
public Task OpenAsync()
{
_SerialPort.Open();
return Task.FromResult((object)null);
}

public void Close()
Expand Down
Loading

0 comments on commit e33ca6b

Please sign in to comment.