diff --git a/Example/MainWindow.xaml b/Example/MainWindow.xaml index b979850..cb33845 100644 --- a/Example/MainWindow.xaml +++ b/Example/MainWindow.xaml @@ -191,13 +191,15 @@ + + Repeat Write - + Write Text @@ -209,7 +211,11 @@ @@ -221,7 +227,11 @@ - + + + + Repeat Write + @@ -398,7 +408,7 @@ Lock Tags - This operation instruct the Tappy to lock every tag it encounters until + This operation instruct the Tappy to lock a tag it encounters until you either issue a stop command or another instruction. Tag locking is an irreversible operation that prevents the locked memory from ever being rewritten, so it is advisable to make certain that any tags you lock diff --git a/Example/MainWindow.xaml.cs b/Example/MainWindow.xaml.cs index 27abe0f..81d8a4c 100644 --- a/Example/MainWindow.xaml.cs +++ b/Example/MainWindow.xaml.cs @@ -21,6 +21,9 @@ namespace TapTrack.Demo using NdefLibrary.Ndef; using System.Text; using Tcmp.CommandFamilies.System; + using System.Management; + using System.Text.RegularExpressions; + /// /// Interaction logic for MainWindow.xaml /// @@ -151,36 +154,60 @@ private void AddUID(ResponseFrame frame, Exception e) private void WriteURLButton_Click(object sender, RoutedEventArgs e) { string url = string.Copy(urlTextBox.Text); + Command cmd = new WriteUri((byte)timeout.Value, (bool)lockCheckBox.IsChecked, new NdefUri(url)); + Callback repeatCommand = null; + bool repeat = (bool)repeatUrlWrite.IsChecked; + Action sendCommand = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand); ShowPendingStatus("Waiting for tap"); - Command cmd = new WriteUri((byte)timeout.Value, (bool)lockCheckBox.IsChecked, new NdefUri(url)); + repeatCommand = (ResponseFrame frame, Exception exc) => + { + if (repeat) + { + if (CheckForErrorsOrTimeout(frame, exc)) + return; + Thread.Sleep(800); + ShowPendingStatus("Waiting for tap"); + Dispatcher.BeginInvoke(sendCommand); + } + }; - tappy.SendCommand(cmd, ResponseCallback); + tappy.SendCommand(cmd, ResponseCallback + repeatCommand); } private void WriteUrlWithTagMirror_Click(object sender, RoutedEventArgs e) { string temp = string.Copy(urlTextBox.Text); bool willLock = (bool)lockCheckBox.IsChecked; + bool repeat = (bool)repeatUrlWrite.IsChecked; byte timeoutValue = (byte)timeout.Value; Command detectTag = new DetectSingleTagUid(timeoutValue, DetectTagSetting.Type2Type4AandMifare); ShowPendingStatus("Waiting for tap"); + Callback detectTagCallback = null; Callback writeCallback = (ResponseFrame frame, Exception exc) => { if (CheckForErrorsOrTimeout(frame, exc)) return; ShowSuccessStatus(); + + if (repeat) + { + Thread.Sleep(1000); + ShowPendingStatus("Waiting for tap"); + Task.Run(() => tappy.SendCommand(detectTag, detectTagCallback)); + } }; - Callback detectTagCallback = (ResponseFrame frame, Exception exc) => + detectTagCallback = (ResponseFrame frame, Exception exc) => { if (CheckForErrorsOrTimeout(frame, exc)) return; Tag tag = new Tag(frame.Data); Command write = new WriteUri(timeoutValue, willLock, temp.Replace("[uid]", tag.UidToString())); + ShowPendingStatus("Tag detected, please hold steady while tag is written"); Task.Run(() => tappy.SendCommand(write, writeCallback)); }; @@ -193,11 +220,26 @@ private void WriteUrlWithTagMirror_Click(object sender, RoutedEventArgs e) private void WriteTextButton_Click(object sender, RoutedEventArgs e) { + Command cmd = new WriteText((byte)timeout.Value, (bool)lockCheckBox.IsChecked, TextBox.Text ?? ""); ShowPendingStatus("Waiting for tap"); - Command cmd = new WriteText((byte)timeout.Value, (bool)lockCheckBox.IsChecked, TextBox.Text); + Callback repeatCommand = null; + bool repeat = (bool)repeatTextWrite.IsChecked; + Action sendCommand = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand); - tappy.SendCommand(cmd, ResponseCallback); + repeatCommand = (ResponseFrame frame, Exception exc) => + { + if (repeat) + { + if (CheckForErrorsOrTimeout(frame, exc)) + return; + Thread.Sleep(1000); + ShowPendingStatus("Waiting for tap"); + Dispatcher.BeginInvoke(sendCommand); + } + }; + + tappy.SendCommand(cmd, ResponseCallback + repeatCommand); } // @@ -229,8 +271,23 @@ private void WriteMultNdef(object send, RoutedEventArgs e) ShowPendingStatus("Waiting for tap"); Command cmd = new WriteCustomNdef((byte)timeout.Value, (bool)lockCheckBox.IsChecked, message); + Callback repeatCommand = null; + bool repeat = (bool)repeatMultiNdefWrite.IsChecked; + Action sendCommand = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand); - tappy.SendCommand(cmd, ResponseCallback); + repeatCommand = (ResponseFrame frame, Exception exc) => + { + if (repeat) + { + if (CheckForErrorsOrTimeout(frame, exc)) + return; + Thread.Sleep(1000); + ShowPendingStatus("Waiting for tap"); + Dispatcher.BeginInvoke(sendCommand); + } + }; + + tappy.SendCommand(cmd, ResponseCallback + repeatCommand); } private void AddTextRowButton_Click(object sender, RoutedEventArgs e) @@ -404,7 +461,14 @@ private void AutoDetectButton_Click(object sender, RoutedEventArgs e) if (window.Protocol == CommunicationProtocol.Usb) batteryTab.Visibility = Visibility.Hidden; else if (window.Protocol == CommunicationProtocol.Bluetooth) + { batteryTab.Visibility = Visibility.Visible; + if (GetBluegigaDevice() == null) + { + ShowFailStatus("Please insert BLED112 dongle"); + return; + } + } tappy.SwitchProtocol(window.Protocol); @@ -417,8 +481,15 @@ private void AutoDetectButton_Click(object sender, RoutedEventArgs e) ShowSuccessStatus($"Connected to {tappy.DeviceName}"); if (window.Protocol == CommunicationProtocol.Bluetooth) { - Command cmd = new EnableDataThrottling(10, 5); - tappy.SendCommand(cmd); + try + { + Command cmd = new EnableDataThrottling(10, 5); + tappy.SendCommand(cmd); + } + catch + { + + } } } else @@ -437,10 +508,15 @@ private void SettingsButton_Click(object sender, RoutedEventArgs e) private void ShowPendingStatus(string message) { - statusPopup.IsOpen = true; - statusText.Content = "Pending"; - statusMessage.Content = message; - ImageBehavior.SetAnimatedSource(statusImage, (BitmapImage)FindResource("Pending")); + Action show = () => + { + statusPopup.IsOpen = true; + statusText.Content = "Pending"; + statusMessage.Content = message; + ImageBehavior.SetAnimatedSource(statusImage, (BitmapImage)FindResource("Pending")); + }; + + Dispatcher.BeginInvoke(show); } private void ShowSuccessStatus(string message = "") @@ -628,7 +704,15 @@ private void ConfigSuccess(ResponseFrame frame, Exception e) private void disconnectButton_Click(object sender, RoutedEventArgs e) { - tappy.Disconnect(); + try + { + tappy.Disconnect(); + ShowSuccessStatus("Disconnect was successful"); + } + catch + { + ShowFailStatus("Disconnect was unsuccessful"); + } } private void firmwareVersionButton_Click(object sender, RoutedEventArgs e) @@ -737,5 +821,32 @@ private void disableType2Button_Click(object sender, RoutedEventArgs e) tappy.SendCommand(cmd, Type2Callback); } + + private string Search(string searchLocation) + { + ManagementObjectCollection comPortDevices; + ManagementObjectSearcher searcher = new ManagementObjectSearcher($"Select * From {searchLocation}"); + comPortDevices = searcher.Get(); + + foreach (ManagementObject device in comPortDevices) + { + string name = device["Name"] as string; + + if (name?.Contains("Bluegiga Bluetooth Low Energy") ?? false) + { + Debug.WriteLine($"Found {device["Name"]}"); + Match match = Regex.Match(name, @"\(([^)]*)\)"); + if (match.Groups.Count > 1) + return match.Groups[1].Value; + } + } + + return null; + } + + private string GetBluegigaDevice() + { + return Search("Win32_SerialPort") ?? Search("Win32_pnpEntity"); + } } } diff --git a/Example/Styles/TapTrack Styles.xaml b/Example/Styles/TapTrack Styles.xaml index a94e968..232c825 100644 --- a/Example/Styles/TapTrack Styles.xaml +++ b/Example/Styles/TapTrack Styles.xaml @@ -155,7 +155,7 @@ - + @@ -421,7 +421,7 @@ - + diff --git a/Example/Tappy Tcmp Demo.csproj b/Example/Tappy Tcmp Demo.csproj index 446ef32..f1501a5 100644 --- a/Example/Tappy Tcmp Demo.csproj +++ b/Example/Tappy Tcmp Demo.csproj @@ -91,6 +91,7 @@ + diff --git a/TapTrack.Tcmp.sln b/TapTrack.Tcmp.sln index f46bfe1..8753e7a 100644 --- a/TapTrack.Tcmp.sln +++ b/TapTrack.Tcmp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tappy Tcmp Demo", "Example\Tappy Tcmp Demo.csproj", "{0A4DA2F5-AECC-4D4A-A942-B1CCC9CA5237}" EndProject