Skip to content

Commit

Permalink
Merge pull request #37 from tboogh/issue/36
Browse files Browse the repository at this point in the history
Fixes iOS characteristics discovery
  • Loading branch information
tboogh authored Mar 22, 2017
2 parents cd1a232 + 500da3d commit b18266e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
<MtouchDebug>true</MtouchDebug>
<MtouchFastDev>true</MtouchFastDev>
<MtouchProfiling>true</MtouchProfiling>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<IOSDebuggerPort>41433</IOSDebuggerPort>
<MtouchLink>None</MtouchLink>
<MtouchArch>x86_64</MtouchArch>
Expand All @@ -40,8 +38,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchFloat32>true</MtouchFloat32>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<MtouchLink>SdkOnly</MtouchLink>
Expand All @@ -58,8 +54,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchLink>None</MtouchLink>
<MtouchArch>x86_64</MtouchArch>
<MtouchHttpClientHandler>HttpClientHandler</MtouchHttpClientHandler>
Expand All @@ -78,8 +72,6 @@
<MtouchDebug>true</MtouchDebug>
<MtouchFastDev>true</MtouchFastDev>
<MtouchProfiling>true</MtouchProfiling>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchFloat32>true</MtouchFloat32>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<MtouchLink>SdkOnly</MtouchLink>
Expand Down Expand Up @@ -173,4 +165,5 @@
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
<Import Project="..\..\packages\Xamarin.Forms.2.3.2.127\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\..\packages\Xamarin.Forms.2.3.2.127\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
</Project>
1 change: 1 addition & 0 deletions Demo/ReactiveBluetooth.Demo.iOS/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommonServiceLocator" version="1.3" targetFramework="xamarinios10" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="xamarinios10" />
<package id="Prism.Core" version="6.3.0-pre1" targetFramework="xamarinios10" />
<package id="Prism.Forms" version="6.3.0-pre1" targetFramework="xamarinios10" />
<package id="Prism.Unity.Forms" version="6.3.0-pre1" targetFramework="xamarinios10" />
Expand Down
1 change: 1 addition & 0 deletions Demo/ReactiveBluetooth.Demo/ReactiveBluetooth.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,5 @@
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="..\..\packages\Xamarin.Forms.2.3.2.127\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\..\packages\Xamarin.Forms.2.3.2.127\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
</Project>
1 change: 1 addition & 0 deletions Demo/ReactiveBluetooth.Demo/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommonServiceLocator" version="1.3" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="portable45-net45+win8+wpa81" />
<package id="Prism.Core" version="6.3.0-pre1" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="Prism.Forms" version="6.3.0-pre1" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="Prism.Unity.Forms" version="6.3.0-pre1" targetFramework="portable45-net45+win8+wp8+wpa81" />
Expand Down
34 changes: 26 additions & 8 deletions ReactiveBluetooth.iOS/Central/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading;
Expand All @@ -27,24 +28,27 @@ public Service(CBService service, CBPeripheral nativeDevice, PeripheralDelegate.
_cbPeripheralDelegate = cbPeripheralDelegate;
}

public Guid Uuid
{
get { return _service.UUID.Uuid.ToGuid(); }
}
public Guid Uuid => _service.UUID.Uuid.ToGuid();

public ServiceType ServiceType => _service.Primary ? ServiceType.Primary : ServiceType.Secondary;

public Task<IList<ICharacteristic>> DiscoverCharacteristics(CancellationToken cancellationToken)
{
var observable = Observable.FromEvent<IList<ICharacteristic>>(action => { _nativeDevice.DiscoverCharacteristics(_service); }, _ => { });
var discoverObservable = CreateDiscoverCharacteristicsObservable(cancellationToken);

IObservable<IList<ICharacteristic>> delegateObservable = _cbPeripheralDelegate.DiscoveredCharacteristicsSubject
.Where(x => Equals(x.Item2, _service) && Equals(x.Item1, _nativeDevice))
IObservable<IList<ICharacteristic>> delegateObservable = _cbPeripheralDelegate.
DiscoveredCharacteristicsSubject
.Where(x =>
{
var serviceEqual = Equals(x.Item2.UUID.Uuid, _service.UUID.Uuid);
var deviceEqual = Equals(x.Item1.Identifier, _nativeDevice.Identifier);
return serviceEqual && deviceEqual;
})
.Select(x => x.Item2.Characteristics.Select(y => new Characteristic(y))
.Cast<ICharacteristic>()
.ToList());

return observable.Merge(delegateObservable)
return discoverObservable.Merge(delegateObservable)
.Take(1)
.ToTask(cancellationToken);
}
Expand All @@ -67,5 +71,19 @@ public Task<IList<IService>> DiscoverIncludedServices(CancellationToken cancella
.Take(1)
.ToTask(cancellationToken);
}

private IObservable<IList<ICharacteristic>> CreateDiscoverCharacteristicsObservable(CancellationToken cancellationToken)
{
return Observable.Create<IList<ICharacteristic>>(async observer =>
{
CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
await Task.Run(() =>
{
_nativeDevice.DiscoverCharacteristics(_service);
observer.OnCompleted();
}, cancellationToken);
return new CancellationDisposable(cancellationTokenSource);
});
}
}
}
5 changes: 1 addition & 4 deletions ReactiveBluetooth.iOS/ReactiveBluetooth.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
<MtouchDebug>true</MtouchDebug>
<MtouchFastDev>true</MtouchFastDev>
<MtouchProfiling>true</MtouchProfiling>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<IOSDebuggerPort>54913</IOSDebuggerPort>
<DeviceSpecificBuild>false</DeviceSpecificBuild>
<MtouchLink></MtouchLink>
Expand All @@ -38,8 +36,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchLink>SdkOnly</MtouchLink>
<MtouchHttpClientHandler></MtouchHttpClientHandler>
<MtouchTlsProvider></MtouchTlsProvider>
Expand Down Expand Up @@ -105,4 +101,5 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
</Project>
1 change: 1 addition & 0 deletions ReactiveBluetooth.iOS/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="xamarinios10" />
<package id="System.Collections" version="4.0.11" targetFramework="xamarinios10" />
<package id="System.Collections.Concurrent" version="4.0.12" targetFramework="xamarinios10" />
<package id="System.ComponentModel" version="4.0.1" targetFramework="xamarinios10" />
Expand Down
3 changes: 3 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## New in 1.0.8
* Fixed characteristics discovery on iOS

## New in 1.0.7
* Added try-catch to start advertising on android

Expand Down

0 comments on commit b18266e

Please sign in to comment.