private void doLocalBroadcast() { try { DiscoveredPrinter[] printers = NetworkDiscoverer.LocalBroadcast(); displayPrinters(printers); } catch (DiscoveryException ex) { handleException(ex.Message); } }
private async Task DiscoverPrintersAsync() { await ClearDiscoveredPrinterListAsync(); await Task.Factory.StartNew(() => { viewModel.IsPrinterListRefreshing = true; }); Device.BeginInvokeOnMainThread(async() => { await AnimateRefreshIconAsync(); }); await Task.Factory.StartNew(() => { try { List <DiscoveredPrinter> usbDriverPrinters = DependencyService.Get <IConnectionManager>().GetZebraUsbDriverPrinters(); foreach (DiscoveredPrinter printer in usbDriverPrinters) { Device.BeginInvokeOnMainThread(() => { viewModel.DiscoveredPrinterList.Add(printer); // ListView view model operations must be done on UI thread due to iOS issues when clearing list while item is selected: https://forums.xamarin.com/discussion/19114/invalid-number-of-rows-in-section }); } } catch (Exception) { // Do nothing } try { DiscoveryHandlerImplementation usbDiscoveryHandler = new DiscoveryHandlerImplementation(this); DependencyService.Get <IConnectionManager>().GetZebraUsbDirectPrinters(usbDiscoveryHandler); while (!usbDiscoveryHandler.IsFinished) { Thread.Sleep(100); } } catch (Exception) { // Do nothing } DiscoveryHandlerImplementation networkDiscoveryHandler = new DiscoveryHandlerImplementation(this); NetworkDiscoverer.LocalBroadcast(networkDiscoveryHandler); while (!networkDiscoveryHandler.IsFinished) { Thread.Sleep(100); } if (Device.RuntimePlatform != Device.WPF || DependencyService.Get <IPlatformHelper>().IsWindows10()) { try { DiscoveryHandlerImplementation bluetoothDiscoveryHandler = new DiscoveryHandlerImplementation(this); DependencyService.Get <IConnectionManager>().FindBluetoothPrinters(bluetoothDiscoveryHandler); while (!bluetoothDiscoveryHandler.IsFinished) { Thread.Sleep(100); } } catch (Exception) { // Do nothing } } }); await Task.Factory.StartNew(() => { viewModel.IsPrinterListRefreshing = false; }); ViewExtensions.CancelAnimations(RefreshIcon); }
/// <exception cref="ConnectionException"></exception> /// <exception cref="DiscoveryException"></exception> private void PerformDiscovery() { DiscoveryHandlerImpl discoveryHandler = new DiscoveryHandlerImpl(this); switch (discoveryMethodsDropdown.SelectedItem) { case DiscoveryMethod.DirectedBroadcast: NetworkDiscoverer.DirectedBroadcast(discoveryHandler, viewModel.IpAddress); break; case DiscoveryMethod.FindPrintersNearMe: NetworkDiscoverer.FindPrinters(discoveryHandler); break; case DiscoveryMethod.LocalBroadcast: NetworkDiscoverer.LocalBroadcast(discoveryHandler); break; case DiscoveryMethod.MulticastBroadcast: NetworkDiscoverer.Multicast(discoveryHandler, int.Parse(viewModel.NumberOfHops)); break; case DiscoveryMethod.SubnetSearch: NetworkDiscoverer.SubnetSearch(discoveryHandler, viewModel.SubnetRange); break; case DiscoveryMethod.ZebraUsbDrivers: try { discoveryHandler = null; discoverPrintersButton.IsEnabled = false; viewModel.DiscoveredPrinters.Clear(); foreach (DiscoveredPrinterDriver printer in UsbDiscoverer.GetZebraDriverPrinters()) { viewModel.DiscoveredPrinters.Add(printer); } } finally { SetDiscoverButtonState(true); } break; case DiscoveryMethod.UsbDirect: try { discoveryHandler = null; discoverPrintersButton.IsEnabled = false; viewModel.DiscoveredPrinters.Clear(); foreach (DiscoveredUsbPrinter printer in UsbDiscoverer.GetZebraUsbPrinters()) { viewModel.DiscoveredPrinters.Add(printer); } } finally { SetDiscoverButtonState(true); } break; case DiscoveryMethod.Bluetooth: BluetoothDiscoverer.FindPrinters(discoveryHandler); break; } }
private async void DiscoverPrintersButton_Clicked(object sender, EventArgs eventArgs) { SetInputEnabled(false); try { ClearDiscoveredPrinters(); DiscoveryHandlerImplementation discoveryHandler = new DiscoveryHandlerImplementation(this); await Task.Factory.StartNew(() => { switch (GetSelectedDiscoveryMethod()) { case DiscoveryMethod.LocalBroadcast: NetworkDiscoverer.LocalBroadcast(discoveryHandler); break; case DiscoveryMethod.DirectedBroadcast: NetworkDiscoverer.DirectedBroadcast(discoveryHandler, IpAddressEntry.Text); break; case DiscoveryMethod.MulticastBroadcast: if (string.IsNullOrWhiteSpace(NumberOfHopsEntry.Text)) { throw new ArgumentException("Number of hops must not be empty"); } try { NetworkDiscoverer.Multicast(discoveryHandler, int.Parse(NumberOfHopsEntry.Text)); } catch (FormatException) { Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Format Error", "Number of hops must be an integer", "OK"); }); } finally { SetInputEnabled(true); } break; case DiscoveryMethod.SubnetSearch: if (string.IsNullOrWhiteSpace(SubnetRangeEntry.Text)) { throw new ArgumentException("Subnet range must not be empty"); } NetworkDiscoverer.SubnetSearch(discoveryHandler, SubnetRangeEntry.Text); break; case DiscoveryMethod.ZebraUsbDrivers: try { foreach (DiscoveredPrinter printer in DependencyService.Get <IConnectionManager>().GetZebraUsbDriverPrinters()) { Device.BeginInvokeOnMainThread(() => { discoveredPrinters.Add(printer); }); } } catch (NotImplementedException) { Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Error", "USB driver discovery not supported on this platform", "OK"); }); } finally { SetInputEnabled(true); } break; case DiscoveryMethod.UsbDirect: try { DependencyService.Get <IConnectionManager>().GetZebraUsbDirectPrinters(discoveryHandler); } catch (NotImplementedException) { Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Error", "USB direct discovery not supported on this platform", "OK"); }); } finally { SetInputEnabled(true); } break; case DiscoveryMethod.FindPrintersNearMe: NetworkDiscoverer.FindPrinters(discoveryHandler); break; case DiscoveryMethod.FindAllBluetoothDevices: try { DependencyService.Get <IConnectionManager>().FindBluetoothPrinters(discoveryHandler); } catch (NotImplementedException) { Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Error", "Bluetooth discovery not supported on this platform", "OK"); }); } finally { SetInputEnabled(true); } break; } }); } catch (Exception e) { await DisplayAlert("Error", e.Message, "OK"); SetInputEnabled(true); } }
private void searchWiFi() { Logger.Write("Start search TCP printers"); // process connection to TCP address PrinterZebra.EPrinterConnectionType connType = PrinterZebra.EPrinterConnectionType.eTCP; if (deviceAdress != null && devicePort > 0 && !PrinterManager.Instance.hasPrinter(deviceAdress)) { Logger.Write("searching in address: " + deviceAdress); ConnecttionJob job = zebraSingleton.tryToConnect(devicePort, deviceAdress, ZebraConstants.connectionTimeout, connType); if (job.Connection != null) { Logger.Write("Found printer on address: " + deviceAdress); PrinterManager.Instance.addPrinterWithID(deviceAdress, devicePort, connType); if (!isSearchStopped) { zebraSingleton.sendConnectResult(job.FriendlyName, deviceAdress, devicePort, connType, oResult); } job.Close(); } } else { Logger.Write("Start search in TCP network"); DiscoveredPrinter[] printers = null; for (int attempt = 0; attempt < connettionAttempts; attempt++) { printers = NetworkDiscoverer.LocalBroadcast(); if (printers.Length > 0) { break; } Thread.Sleep(500); } if (printers.Length == 0) { for (int attempt = 0; attempt < connettionAttempts; attempt++) { printers = NetworkDiscoverer.Multicast(5); if (printers.Length > 0) { break; } Thread.Sleep(500); } } LogDiscoveryPrinters(printers); foreach (DiscoveredPrinter printer in printers) { if (isSearchStopped) { break; } Logger.Write("searching in address: " + printer.Address); if ((printer is DiscoveredPrinterNetwork) && !PrinterManager.Instance.hasPrinter(printer.Address)) { DiscoveredPrinterNetwork networkPrinter = (DiscoveredPrinterNetwork)printer; ConnecttionJob job = zebraSingleton.tryToConnect(networkPrinter.Port, networkPrinter.Address, ZebraConstants.connectionTimeout, connType); if (job.Connection != null) { Logger.Write("Found printer on address: " + printer.Address); PrinterManager.Instance.addPrinterWithID(networkPrinter.Address, networkPrinter.Port, connType); zebraSingleton.sendConnectResult(job.FriendlyName, networkPrinter.Address, networkPrinter.Port, connType, oResult); job.Close(); } } } } }