private void ButtonTrigerHaptic(object sender, RoutedEventArgs e) { byte[] Data = { 0xC0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x03, 0x72, 0x01, 0x09, 0x7C, 0xC0 }; FindCharacter.WriteWithoutResponse(Data).Subscribe(Result => { CharacteristicGattResult Res = Result; Debug.WriteLine("Write Result OK"); }); }
void OnReceive(CharacteristicGattResult result) { string data; try { data = Encoding.UTF8.GetString(result.Data); } catch (Exception ex) { App.AddLog($"{MethodBase.GetCurrentMethod().Name} {Name} {ex.Message}"); return; } MessagingCenter.Send(this, "Received", data); }
/// <summary> /// Called when recieving samples /// </summary> /// <param name="result"></param> private void HandleActivityChar(CharacteristicGattResult result) { // Each sample is made up from 4 bytes. The first byte represents the status. if (result.Data.Length % 4 != 1) { InitiateFetching(_lastTimestamp.AddMinutes(1)); } else { CreateSamplesFromResponse(result.Data); } }
/// <summary>The set read value.</summary> /// <param name="result">The result.</param> /// <param name="fromUtf8">The from utf 8.</param> private void SetReadValue(CharacteristicGattResult result, bool fromUtf8) { if (result.Data == null) { this.Value = string.Empty; } else { this.Value = fromUtf8 ? Encoding.UTF8.GetString(result.Data, 0, result.Data.Length) : Encoding.ASCII.GetString(result.Data, 0, result.Data.Length); } }
/// <summary> /// Invoke error from BLE response /// </summary> /// <param name="response"></param> private void InvokeError(CharacteristicGattResult result) { var err = (ResponseErrors)result.Data[2]; if (err == ResponseErrors.NRF_DFU_RES_CODE_EXT_ERROR) { var error = (ExtendedErrors)result.Data[3]; DFUEvents.OnExtendedError?.Invoke(error); } else { var error = (ResponseErrors)result.Data[3]; DFUEvents.OnResponseError?.Invoke(error); } }
void SetReadValue(CharacteristicGattResult result) => Device.BeginInvokeOnMainThread(() => { if (result.Data == null) { Textin = "!EMPTY"; } else { Textin = Encoding.UTF8.GetString(result.Data, 0, result.Data.Length); } if (Textin == "ER") { ShowMessage("Oops", "Data didn't send, Try again..."); } Decode(Textin); });
void SetReadValue(CharacteristicGattResult result, bool fromUtf8) => Device.BeginInvokeOnMainThread(() => { this.IsValueAvailable = true; this.LastValue = DateTime.Now; if (result.Data == null) { this.Value = "EMPTY"; } else { this.Value = fromUtf8 ? Encoding.UTF8.GetString(result.Data, 0, result.Data.Length) : BitConverter.ToString(result.Data); } });
/// <summary> /// Called when recieving MetaData /// </summary> /// <param name="result"></param> private async void HandleUnknownChar(CharacteristicGattResult result) { //Correct response if (result.Data.Length >= 3) { // Create an empty byte array and copy the response type to it byte[] responseByte = new byte[3]; Buffer.BlockCopy(result.Data, 0, responseByte, 0, 3); // Start or Continue fetching process if (responseByte.SequenceEqual(new byte[3] { 0x10, 0x01, 0x01 })) { await HandleResponse(result.Data); return; } // Finished fetching if (responseByte.SequenceEqual(new byte[3] { 0x10, 0x02, 0x01 })) { if (_lastTimestamp >= DateTime.Now.AddMinutes(-1)) { _finishedCallback(_samples); _charActivitySub?.Dispose(); _charUnknownSub?.Dispose(); return; } await InitiateFetching(_lastTimestamp.AddMinutes(1)); return; } } Trace.WriteLine("No more samples could be fetched, samples returned: " + _samples.Count); _finishedCallback(_samples); _charActivitySub?.Dispose(); _charUnknownSub?.Dispose(); return; }
/// <summary> /// Assert that commmand executed successfully and raise error if not /// </summary> /// <param name="result"></param> private void AssertSuccess(CharacteristicGattResult result) { /* * The response received from the DFU device contains: * +---------+--------+----------------------------------------------------+ * | byte no | value | description | * +---------+--------+----------------------------------------------------+ * | 0 | 0x60 | Response code | * | 1 | 0x06 | The Op Code of a request that this response is for | * | 2 | STATUS | Status code | * | ... | ... | ... | * +---------+--------+----------------------------------------------------+ */ if (result.Data[2] != SecureDFUCommandSuccess) { InvokeError(result); throw new Exception(String.Format("Assertion failed while testing command [{0}] response status: [{1}]", result.Data[1], result.Data[2])); } }
/// <summary> /// Request and read DFU checksum from control point after data have been sent /// </summary> /// <param name="device"></param> /// <returns></returns> private async Task <ObjectChecksum> ReadChecksum(IGattCharacteristic controlPoint) { Debug.WriteLineIf(LogLevelDebug, String.Format("Begin read checksum")); ObjectChecksum checksum = new ObjectChecksum(); CharacteristicGattResult result = null; for (var retry = 0; retry < MaxRetries; retry++) { try { // Request checksum var notif = GetTimedNotification(controlPoint); var re = await controlPoint.Write(CCalculateCRC).Timeout(OperationTimeout); Debug.WriteLine(re); result = await notif.Task; break; } catch (Exception) { await Task.Delay(100); } finally { if (retry == MaxRetries) { throw new DFUTimeout(); } } } Debug.WriteLineIf(LogLevelDebug, String.Format("Check Sum Response {0}", BitConverter.ToString(result.Data))); AssertSuccess(result); SetChecksum(checksum, result.Data); Debug.WriteLineIf(LogLevelDebug, String.Format("End read checksum")); return(checksum); }
void SetReadValue(CharacteristicGattResult result, bool fromUtf8) => Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { this.IsValueAvailable = true; this.LastValue = DateTime.Now; if (result.Data == null) { this.Value = "EMPTY"; } else { this.Value = result.Data[0].ToString(); } RaisePropertyChanged(nameof(IsValueAvailable)); RaisePropertyChanged(nameof(LastValue)); RaisePropertyChanged(nameof(Value)); //fromUtf8 // ? Encoding.UTF8.GetString(result.Data, 0, result.Data.Length) // : BitConverter.ToString(result.Data); });