private async Task _ReceiveAsync() { await Task.Run(async() => { bool error = false; while (true) { try { if (TcpClient.Available > 0) { byte[] buffer = new byte[TcpClient.Available]; var size = await NetworkStream.ReadAsync(buffer, 0, TcpClient.Available); Device.BeginInvokeOnMainThread(() => { MessageReceived?.Invoke(this, buffer); TranspondCharacteristicWrapper.Value = buffer; TranspondCharacteristicWrapper.NotifyAll(); }); } } catch (Exception exception) { Debug.WriteLine(exception.Message); error = true; break; } } if (error) { State = TcpConnState.Error; StateChanged?.Invoke(this, State); } }); }
public TcpTranspondServiceWrapper(IBluetoothManager bluetoothManager) { BluetoothManager = bluetoothManager; TranspondCharacteristicWrapper = new TranspondCharacteristicWrapper(bluetoothManager); IGattServiceBuilder builder = bluetoothManager.NewGattServiceBuilder(); GattServerService = builder.SetUuid(Uuid) .AddCharacteristics(TranspondCharacteristicWrapper.GattServerCharacteristic) .Build(); TranspondCharacteristicWrapper.GattServerCharacteristic.OnRead += GattServerCharacteristic_OnRead; TranspondCharacteristicWrapper.GattServerCharacteristic.OnWrite += GattServerCharacteristic_OnWrite; State = TcpConnState.Created; TcpClient = new TcpClient(); }