Skip to content

Commit

Permalink
Added launch protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalej committed Aug 25, 2017
1 parent 8d7200b commit 21dd42a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
21 changes: 19 additions & 2 deletions BlueMuse/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,27 @@ private void Launch(ApplicationExecutionState previousExecutionState, bool prela
protected override void OnActivated(IActivatedEventArgs args)
{
// Check for protocol activation
if (args.Kind == ActivationKind.Protocol)
if (args.Kind == ActivationKind.Protocol && args.PreviousExecutionState != ApplicationExecutionState.Running)
{
var protocolArgs = (ProtocolActivatedEventArgs)args;
string argval = protocolArgs.Uri.ToString();
string argStr = protocolArgs.Uri.PathAndQuery;
var splitArgs = argStr.Split('&');
var addressesStr = splitArgs.FirstOrDefault(x => x.Contains("addresses"));
if(addressesStr != null)
{
var addresses = addressesStr.Trim().Replace("addresses=","").Split(',');
foreach(var address in addresses)
{
Constants.MusesToAutoStream.Add(address);
}
}
else
{
var streamFirstStr = splitArgs.FirstOrDefault(x => x.Contains("streamfirst"));
streamFirstStr = streamFirstStr.Trim().Replace("streamfirst=", "");
if (streamFirstStr.ToLower() == "true" || streamFirstStr == "1")
Constants.StreamFirst = true;
}
// Manipulate arguments …
Launch(ApplicationExecutionState.NotRunning, false);
}
Expand Down
25 changes: 24 additions & 1 deletion BlueMuse/Bluetooth/BluetoothManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public BluetoothManager(ObservableCollection<Muse> muses) {

private async void Current_Suspending(object sender, SuspendingEventArgs e)
{
var def = e.SuspendingOperation.GetDeferral();
await StopStreamingAll();
await DeactivateLSLBridge();
def.Complete();
}

public void FindMuses()
Expand Down Expand Up @@ -104,8 +107,10 @@ private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation a
}
else
{
muses.Add(new Muse(device, device.Name, device.DeviceId, device.ConnectionStatus == BluetoothConnectionStatus.Connected ? MuseConnectionStatus.Online : MuseConnectionStatus.Offline));
muse = new Muse(device, device.Name, device.DeviceId, device.ConnectionStatus == BluetoothConnectionStatus.Connected ? MuseConnectionStatus.Online : MuseConnectionStatus.Offline);
muses.Add(muse);
}
ResolveAutoStream(muse);
}

// Must watch for status changed because Added and Updated are not always called upon connecting or disconnecting.
Expand All @@ -114,6 +119,23 @@ private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation a
}
}

private void ResolveAutoStream(Muse muse)
{
if (muse.Device.ConnectionStatus == BluetoothConnectionStatus.Connected)
{
if (Constants.StreamFirst && muses.IndexOf(muse) == 0)
{
Constants.StreamFirst = false;
StartStreaming(muse.Id);
}
else if (Constants.MusesToAutoStream.Any(x => x == muse.MacAddress))
{
Constants.MusesToAutoStream.Remove(muse.MacAddress);
StartStreaming(muse.Id);
}
}
}

private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
// Again, filter for Muses.
Expand Down Expand Up @@ -148,6 +170,7 @@ private void Device_ConnectionStatusChanged(BluetoothLEDevice sender, object arg
{
muse.Status = sender.ConnectionStatus == BluetoothConnectionStatus.Connected ? MuseConnectionStatus.Online : MuseConnectionStatus.Offline;
if (sender.ConnectionStatus == BluetoothConnectionStatus.Disconnected && muse.IsStreaming) StopStreaming(muse);
else ResolveAutoStream(muse);
Debug.WriteLine(string.Format("Device: {0} is now {1}.", sender.Name, sender.ConnectionStatus));
}
}
Expand Down
4 changes: 4 additions & 0 deletions BlueMuse/Misc/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;

namespace BlueMuse
{
static class Constants
{
public static List<string> MusesToAutoStream = new List<string>();
public static bool StreamFirst = false;

public static readonly string ALL_AQS = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\""; // Wildcard based "Muse*" filter - not supported it seems. AND (System.ItemNameDisplay:~\"Muse*\" OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#True)";

public const int MUSE_SAMPLE_RATE = 256;
Expand Down
16 changes: 8 additions & 8 deletions LSLBridge/LSL/MuseLSLStreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ private void LSLService_RequestReceived(AppServiceConnection sender, AppServiceR
}
break;

// Currently not working - cannot close bridge without main app exiting a few seconds later.
//case Constants.LSL_MESSAGE_TYPE_CLOSE_BRIDGE:
// {
// lslStreamService.RequestReceived -= LSLService_RequestReceived;
// lslStreamService.Dispose();
// Application.Current.Shutdown();
// }
// break;
// Should not be called until application is closing.
case Constants.LSL_MESSAGE_TYPE_CLOSE_BRIDGE:
{
lslStreamService.RequestReceived -= LSLService_RequestReceived;
lslStreamService.Dispose();
Process.GetCurrentProcess().Kill();
}
break;
}
}

Expand Down

0 comments on commit 21dd42a

Please sign in to comment.