Skip to content

Commit

Permalink
Added error check in completionHandler. Added device in GattCallback.…
Browse files Browse the repository at this point in the history
… Added Todo description in DeviceListViewModel.
  • Loading branch information
Dragon160 committed Apr 26, 2017
1 parent 6be1968 commit 1fedaa5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ private async void ConnectAndDisposeDevice(DeviceListItemViewModel item)
_userDialogs.ShowLoading($"Connecting to {item.Name} ...");
await Adapter.ConnectToDeviceAsync(item.Device);

// TODO make this configurable
var resultMTU = await item.Device.RequestMtuAsync(60);
System.Diagnostics.Debug.WriteLine($"Requested MTU. Result is {resultMTU}");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using System;
using System;
using Plugin.BLE.Abstractions.Contracts;

namespace Plugin.BLE.Android.CallbackEventArgs
{
public class MtuRequestCallbackEventArgs : EventArgs
{
public IDevice Device { get; }
public Exception Error { get; }
public int Mtu { get; }
public MtuRequestCallbackEventArgs(Exception error, int mtu)

public MtuRequestCallbackEventArgs(IDevice device, Exception error, int mtu)
{
Device = device;
Error = error;
Mtu = mtu;
}
Expand Down
20 changes: 15 additions & 5 deletions Source/Plugin.BLE.Android/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,23 @@ protected override async Task<int> RequestMtuNativeAsync(int requestValue)
}

return await TaskBuilder.FromEvent<int, EventHandler<MtuRequestCallbackEventArgs>>(
execute: () => { _gatt.RequestMtu(requestValue); },
getCompleteHandler: (complete, reject) => ((sender, args) =>
execute: () => { _gatt.RequestMtu(requestValue); },
getCompleteHandler: (complete, reject) => ((sender, args) =>
{
complete(args.Mtu);
if (args.Device == null || args.Device.Id != Id)
return;

if (args.Error != null)
{
Trace.Message($"Failed to request MTU ({requestValue}) for device {Id}-{Name}. {args.Error.Message}");
reject(new Exception($"Request MTU error: {args.Error.Message}"));
}
else
{
complete(args.Mtu);
}
}),

subscribeComplete: handler => _gattCallback.MtuRequested += handler,
subscribeComplete: handler => _gattCallback.MtuRequested += handler,
unsubscribeComplete: handler => _gattCallback.MtuRequested -= handler
);
}
Expand Down
9 changes: 8 additions & 1 deletion Source/Plugin.BLE.Android/GattCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,14 @@ public override void OnMtuChanged(BluetoothGatt gatt, int mtu, GattStatus status
base.OnMtuChanged(gatt, mtu, status);

Trace.Message("OnMtuChanged to value: {0}", mtu);
MtuRequested?.Invoke(this, new MtuRequestCallbackEventArgs(GetExceptionFromGattStatus(status), mtu));

IDevice device;
if (!_adapter.ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device))
{
Trace.Message("Device for MTU changed is not in connected list. This should not happen.");
}

MtuRequested?.Invoke(this, new MtuRequestCallbackEventArgs(device, GetExceptionFromGattStatus(status), mtu));
}

public override void OnReadRemoteRssi(BluetoothGatt gatt, int rssi, GattStatus status)
Expand Down

0 comments on commit 1fedaa5

Please sign in to comment.