// Set the alert-level characteristic on the remote device public async Task UART_Transmit(string user_input, int inputlength) { // try-catch block protects us from the race where the device disconnects // just after we've determined that it is connected. try { // use the converter class to convert the string into bytes byte[] data = UART_Data_Converter.TX(user_input, inputlength); await tx_characteristic.WriteValueAsync(data.AsBuffer(), GattWriteOption.WriteWithoutResponse); } catch (Exception) { // ignore exception } }
/// <summary> /// Invoked when Windows receives data from your Bluetooth device. /// </summary> /// <param name="sender">The GattCharacteristic object whose value is received.</param> /// <param name="args">The new characteristic value sent by the device.</param> private void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { var data = new byte[args.CharacteristicValue.Length]; DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(data); // Process the raw data received from the device. var astring = UART_Data_Converter.RX(data); var value = new UARTElement { sent_text = astring }; lock (datapoints) { datapoints.Add(value); } if (ValueChangeCompleted != null) { ValueChangeCompleted(value); } }