public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState) { if (newState == ProfileState.Connected) { _parent.OnStatusUpdated("Requesting MTU"); _gatt = gatt; gatt.RequestMtu(138); } else if (newState == ProfileState.Disconnected && status != GattStatus.Success) { _handler.OnError(); } }
protected override async Task <int> RequestMtuNativeAsync(int requestValue) { if (_gatt == null || _gattCallback == null) { Trace.Message("You can't request a MTU for disconnected devices. Device is {0}", State); return(-1); } if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop) { Trace.Message($"Request MTU not supported in this Android API level"); return(-1); } return(await TaskBuilder.FromEvent <int, EventHandler <MtuRequestCallbackEventArgs>, EventHandler>( execute : () => { _gatt.RequestMtu(requestValue); }, getCompleteHandler : (complete, reject) => ((sender, args) => { 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, unsubscribeComplete : handler => _gattCallback.MtuRequested -= handler, getRejectHandler : reject => ((sender, args) => { reject(new Exception($"Device {Name} disconnected while requesting MTU.")); }), subscribeReject : handler => _gattCallback.ConnectionInterrupted += handler, unsubscribeReject : handler => _gattCallback.ConnectionInterrupted -= handler )); }