public WebNfcListener(string ident) { if (!ident.Contains("@")) { throw new ArgumentException("Invalid ident - missing @"); } var parts = ident.Split('@'); if (parts.Length != 2) { throw new ArgumentException("Invalid ident - invalid length"); } _endpoint = parts[0]; _address = parts[1]; _ioHandler = IO.Socket($"http://{_address}", new IO.Options { AutoConnect = false, Reconnection = true }); _ioHandler.On(Socket.EVENT_CONNECT, (obj) => { OnConnected?.Invoke(); }); _ioHandler.On("scanner.registered", (obj) => { var scanner = NfcScanner.FromObject(obj); if (!Scanners.Exists(x => x.DeviceId == scanner.DeviceId)) { Scanners.Add(scanner); OnScannerRegistered?.Invoke(scanner); } }); _ioHandler.On("scanner.disconnected", (obj) => { dynamic o = obj; string deviceId = o.device_id; var scanner = Scanners.FirstOrDefault(x => x.DeviceId == deviceId); if (scanner != null) { Scanners.Remove(scanner); } OnScannerDisconnected?.Invoke(scanner); }); _ioHandler.On("scanner.scanned", (obj) => { dynamic o = obj; string deviceId = o.device_id; var scanner = Scanners.FirstOrDefault(x => x.DeviceId == deviceId); var s = new NfcScan { Scanner = scanner, CardId = ((dynamic)obj).card_id }; OnScanned?.Invoke(s); }); }
public void Scan() { if (_isScanned) { return; } _isScanned = true; OnScanned?.Invoke(); }
//private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) //{ // int len = serialPort.BytesToRead; // byte[] buffer = new byte[len]; // serialPort.Read(buffer, 0, len); // Buffer.AddRange(buffer);//断帧、粘包处理 // int idx_0a;//0x0d 0x0a 结束符 // while ((idx_0a = Buffer.IndexOf(0x0a)) != -1)//断帧、粘包处理 // { // byte[] data = Buffer.Take(idx_0a - 1).ToArray();//截取0x0d 0x0a前报文 // Buffer.RemoveRange(0, idx_0a + 1); // string strCodes = Encoding.Default.GetString(data);//TODO:编码格式 Default=GB2312 // RaiseScanned(this.Scanner, strCodes); // } //} protected void RaiseScanned(ScanDevice scan, string stopNo, string data) { try { OnScanned?.Invoke(this.Scanner, stopNo, data); } catch (Exception ex) { string errMsg = "OnScanned事件委托异常!"; RaiseError(errMsg); //MyLog.WriteLog(errMsg, ex); } }
public async Task StartScan(MobileBarcodeScanningOptions options = null) { try { var scanner = new MobileBarcodeScanner() { TopText = "Scan Code" }; scanner.ScanContinuously(options ?? new MobileBarcodeScanningOptions { PossibleFormats = new List <BarcodeFormat> { BarcodeFormat.EAN_8, BarcodeFormat.EAN_13, BarcodeFormat.CODE_39, BarcodeFormat.CODE_128, BarcodeFormat.QR_CODE }, AutoRotate = false, UseFrontCameraIfAvailable = false, TryHarder = true }, (result) => { CrossSimpleAudioPlayer.Current.Load("beep.wav"); CrossSimpleAudioPlayer.Current.Play(); OnScanned?.Invoke(this, result.Text); MessagingCenter.Send(this, MessageKey.BARCODE_SCANNED, result.Text); }); Device.StartTimer(TimeSpan.FromSeconds(3), () => { scanner.AutoFocus(); return(true); }); } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert(TranslateExtension.GetValue("alert_title_error"), ex.Message, TranslateExtension.GetValue("alert_message_ok")); } }