Skip to content

Commit

Permalink
first version of requesting connection interval API
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon160 committed Apr 28, 2017
1 parent 5064651 commit 2ff4e78
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ private async void ConnectAndDisposeDevice(DeviceListItemViewModel item)
var resultMTU = await item.Device.RequestMtuAsync(60);
System.Diagnostics.Debug.WriteLine($"Requested MTU. Result is {resultMTU}");

var resultInterval = await item.Device.UpdateConnectionIntervalAsync(ConnectionInterval.High);
System.Diagnostics.Debug.WriteLine($"Set Connection Interval. Result is {resultInterval}");

item.Update();
_userDialogs.ShowSuccess($"Connected {item.Device.Name}");

Expand Down
10 changes: 10 additions & 0 deletions Source/Plugin.BLE.Abstractions/ConnectionInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace Plugin.BLE.Abstractions
{
public enum ConnectionInterval
{
Normal = 0,
High = 1,
Low = 2
}
}
13 changes: 13 additions & 0 deletions Source/Plugin.BLE.Abstractions/Contracts/IDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,18 @@ public interface IDevice : IDisposable
/// A task that represents the asynchronous operation. The result contains the negotiated MTU size between master and slave</returns>
/// <param name="requestValue">The requested MTU</param>
Task<int> RequestMtuAsync(int requestValue);

/// <summary>
/// Requests a bluetooth-le connection update request. Be aware that this is only implemented on Android (>= API 21).
/// You can choose between a high, low and a normal mode which will requests the following connection intervals: HIGH (11-15ms). NORMAL (30-50ms). LOW (100-125ms).
/// Its not possible to request a specific connection interval.
///
/// Important:
/// On Android: This function will only work with API level 21 and higher. Other API level will return false as function result.
/// On iOS: Updating the connection interval is not supported by iOS. The function simply returns true.
/// </summary>
/// <returns>True if the update request was sucessfull. On iOS it will always return true.</returns>
/// <param name="interval">The requested interval (High/Low/Normal)</param>
Task<bool> UpdateConnectionIntervalAsync(ConnectionInterval interval);
}
}
6 changes: 6 additions & 0 deletions Source/Plugin.BLE.Abstractions/DeviceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ public async Task<int> RequestMtuAsync(int requestValue)
return await RequestMtuNativeAsync(requestValue);
}

public async Task<bool> UpdateConnectionIntervalAsync(ConnectionInterval interval)
{
return await UpdateConnectionIntervalNativeAsync(interval);
}

public abstract Task<bool> UpdateRssiAsync();
protected abstract DeviceState GetState();
protected abstract Task<IEnumerable<IService>> GetServicesNativeAsync();
protected abstract Task<int> RequestMtuNativeAsync(int requestValue);
protected abstract Task<bool> UpdateConnectionIntervalNativeAsync(ConnectionInterval interval);

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Utils\BleCommandQueue.cs" />
<Compile Include="Utils\FakeAdapter.cs" />
<Compile Include="Utils\TaskBuilder.cs" />
<Compile Include="ConnectionInterval.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
25 changes: 25 additions & 0 deletions Source/Plugin.BLE.Android/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,30 @@ protected override async Task<int> RequestMtuNativeAsync(int requestValue)
unsubscribeReject: handler => _gattCallback.ConnectionInterrupted -= handler
);
}

protected override async Task<bool> UpdateConnectionIntervalNativeAsync(ConnectionInterval interval)
{

if (_gatt == null || _gattCallback == null)
{
Trace.Message("You can't update a connection interval for disconnected devices. Device is {0}", State);
return false;
}

if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
Trace.Message($"Update connection interval paramter in this Android API level");
return false;
}

try
{
return await Task.FromResult(_gatt.RequestConnectionPriority((GattConnectionPriority)(int)interval));
}
catch(Exception ex)
{
throw new Exception($"Update Connection Interval fails with error. {ex.Message}");
}
}
}
}
6 changes: 6 additions & 0 deletions Source/Plugin.BLE.iOS/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,11 @@ protected override async Task<int> RequestMtuNativeAsync(int requestValue)
Trace.Message($"Request MTU is not supported on iOS.");
return await Task.FromResult((int)_nativeDevice.GetMaximumWriteValueLength(CBCharacteristicWriteType.WithoutResponse));
}

protected override async Task<bool> UpdateConnectionIntervalNativeAsync(ConnectionInterval interval)
{
Trace.Message("Cannot update connection inteval on iOS. Returning TRUE ..");
return await Task.FromResult(true);
}
}
}

0 comments on commit 2ff4e78

Please sign in to comment.