Exemplo n.º 1
0
        /// <summary>
        /// Create new NFC object
        /// </summary>
        /// <param name="NFCId">The NFCId<see cref="string"/></param>
        private void AddNewRecord(string NFCId)
        {
            Device.BeginInvokeOnMainThread(async() =>
            {
                var r = await UserDialogs.Instance.PromptAsync(AppResources.nfc_tag_name, inputType: InputType.Name);
                await Task.Delay(500);

                if (!r.Ok)
                {
                    return;
                }
                var name = r.Text;
                if (string.IsNullOrEmpty(name))
                {
                    return;
                }
                App.ShowToast(AppResources.nfc_saved + " " + name);
                var NFC = new NFCModel()
                {
                    Id      = NFCId,
                    Name    = name,
                    Enabled = true,
                };
                _oListSource.Add(NFC);
                SaveAndRefresh();
            });
        }
Exemplo n.º 2
0
 public void ShowBulletins()
 {
     if (App.IsForeground && Bulletins.Count > 0)
     {
         try
         {
             Device.BeginInvokeOnMainThread(async() =>
             {
                 await RaisePropertyChanged(() => Bulletins);
                 if (!Accessibility.AccessibilityEnabled)
                 {
                     // make sure none are currently open
                     if (_page == null)
                     {
                         _page = new BulletinPopupPage();
                         await PopupNavigation.Instance.PushAsync(_page);
                     }
                 }
                 else
                 {
                     foreach (var b in Bulletins)
                     {
                         await UserDialogs.Instance.AlertAsync(b.Description, b.Title);
                     }
                 }
             });
         }
         catch (Exception e)
         {
             Crashes.TrackError(e);
         }
     }
 }
Exemplo n.º 3
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Tag = e.Parameter;

            if (e.Parameter is FeedItem feed)
            {
                BindingContext.Title = "通知详情";
                PrimaryMenu.Add(new AppBarButton {
                    Icon = new SymbolIcon(Symbol.Flag), Label = "详情", Command = new Command(() => Device.OpenUri(new Uri(feed.Link)))
                });
                Title  = feed.Title;
                Time   = "时间:" + feed.PubDate;
                Sender = "分类:" + feed.Category;
                Body   = feed.Description.Replace(' ', '\n');
            }
            else if (e.Parameter is IMessageItem msg)
            {
                BindingContext.Title = "消息详情";
                PrimaryMenu.Add(new AppBarButton {
                    Icon = new SymbolIcon(Symbol.Delete), Label = "设为未读", Command = msg.SetUnread
                });
                PrimaryMenu.Add(new AppBarButton {
                    Icon = new SymbolIcon(Symbol.Delete), Label = "删除", Command = msg.Delete
                });
                Title  = msg.Title;
                Time   = "时间:" + msg.Time.ToString();
                Sender = "发件人:" + msg.Sender;
                Body   = msg.Body;
            }
        }
Exemplo n.º 4
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Tag = e.Parameter;

            if (e.Parameter is FeedItem feed)
            {
                ViewModel.Title = "通知详情";
                PrimaryMenu.Add(new AppBarButton {
                    Icon = new SymbolIcon(Symbol.Flag), Label = "详情", Command = new Command(() => Device.OpenUri(new Uri(feed.Link)))
                });
                Title  = feed.Title;
                Time   = "时间:" + feed.PubDate;
                Sender = "分类:" + feed.Category;
                var desc = feed.Description.Trim();
                while (desc.Contains("    "))
                {
                    desc = desc.Replace("    ", "  ");
                }
                Body = desc;
            }
            else if (e.Parameter is IMessageItem msg)
            {
                ViewModel.Title = "消息详情";
                PrimaryMenu.Add(new AppBarButton {
                    Icon = new SymbolIcon(Symbol.Delete), Label = "删除", Command = msg.Delete
                });
                Title  = msg.Title;
                Time   = "时间:" + msg.Time.ToString();
                Sender = "发件人:" + msg.Sender;
                Body   = msg.Body;
            }
        }
Exemplo n.º 5
0
 void OnEvent(string msg) => Device.BeginInvokeOnMainThread(() =>
                                                            this.Output += msg + Environment.NewLine + Environment.NewLine
                                                            );
Exemplo n.º 6
0
        void BuildServer()
        {
            if (this.server != null)
            {
                return;
            }

            try
            {
                this.server = this.BleAdapter.CreateGattServer();
                var service = this.server.AddService(Guid.NewGuid(), true);

                var characteristic = service.AddCharacteristic(
                    Guid.NewGuid(),
                    CharacteristicProperties.Read | CharacteristicProperties.Write | CharacteristicProperties.WriteNoResponse,
                    GattPermissions.Read | GattPermissions.Write
                    );
                var notifyCharacteristic = service.AddCharacteristic
                                           (
                    Guid.NewGuid(),
                    CharacteristicProperties.Notify,
                    GattPermissions.Read | GattPermissions.Write
                                           );

                //var descriptor = characteristic.AddDescriptor(Guid.NewGuid(), Encoding.UTF8.GetBytes("Test Descriptor"));

                notifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e =>
                {
                    var @event = e.IsSubscribed ? "Subscribed" : "Unsubcribed";
                    this.OnEvent($"Device {e.Device.Uuid} {@event}");
                    this.OnEvent($"Charcteristic Subcribers: {notifyCharacteristic.SubscribedDevices.Count}");

                    if (this.notifyBroadcast == null)
                    {
                        this.OnEvent("Starting Subscriber Thread");
                        this.notifyBroadcast = Observable
                                               .Interval(TimeSpan.FromSeconds(1))
                                               .Where(x => notifyCharacteristic.SubscribedDevices.Count > 0)
                                               .Subscribe(_ =>
                        {
                            try
                            {
                                var dt    = DateTime.Now.ToString("g");
                                var bytes = Encoding.UTF8.GetBytes(dt);
                                notifyCharacteristic
                                .BroadcastObserve(bytes)
                                .Subscribe(x =>
                                {
                                    var state = x.Success ? "Successfully" : "Failed";
                                    var data  = Encoding.UTF8.GetString(x.Data, 0, x.Data.Length);
                                    this.OnEvent($"{state} Broadcast {data} to device {x.Device.Uuid} from characteristic {x.Characteristic}");
                                });
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Error during broadcast: " + ex);
                            }
                        });
                    }
                });

                characteristic.WhenReadReceived().Subscribe(x =>
                {
                    var write = this.CharacteristicValue;
                    if (String.IsNullOrWhiteSpace(write))
                    {
                        write = "(NOTHING)";
                    }

                    x.Value = Encoding.UTF8.GetBytes(write);
                    this.OnEvent("Characteristic Read Received");
                });
                characteristic.WhenWriteReceived().Subscribe(x =>
                {
                    var write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
                    this.OnEvent($"Characteristic Write Received - {write}");
                });

                this.server
                .WhenRunningChanged()
                .Catch <bool, ArgumentException>(ex =>
                {
                    this.Dialogs.Alert("Error Starting GATT Server - " + ex);
                    return(Observable.Return(false));
                })
                .Subscribe(started => Device.BeginInvokeOnMainThread(() =>
                {
                    if (!started)
                    {
                        this.ServerText = "Start Server";
                        this.OnEvent("GATT Server Stopped");
                    }
                    else
                    {
                        this.notifyBroadcast?.Dispose();
                        this.notifyBroadcast = null;

                        this.ServerText = "Stop Server";
                        this.OnEvent("GATT Server Started");
                        foreach (var s in this.server.Services)
                        {
                            this.OnEvent($"Service {s.Uuid} Created");
                            foreach (var ch in s.Characteristics)
                            {
                                this.OnEvent($"Characteristic {ch.Uuid} Online - Properties {ch.Properties}");
                            }
                        }
                    }
                }));

                this.server
                .WhenAnyCharacteristicSubscriptionChanged()
                .Subscribe(x =>
                           this.OnEvent($"[WhenAnyCharacteristicSubscriptionChanged] UUID: {x.Characteristic.Uuid} - Device: {x.Device.Uuid} - Subscription: {x.IsSubscribing}")
                           );

                //descriptor.WhenReadReceived().Subscribe(x =>
                //    this.OnEvent("Descriptor Read Received")
                //);
                //descriptor.WhenWriteReceived().Subscribe(x =>
                //{
                //    var write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
                //    this.OnEvent($"Descriptor Write Received - {write}");
                //});
            }
            catch (Exception ex)
            {
                this.Dialogs.Alert("Error building gatt server - " + ex);
            }
        }
        /// <summary>
        /// Add new qr code to system
        /// </summary>
        /// <param name="sender">The sender<see cref="object"/></param>
        /// <param name="e">The e<see cref="EventArgs"/></param>
        private async void ToolbarItem_Activated(object sender, EventArgs e)
        {
            if (!App.AppSettings.QRCodeEnabled)
            {
                return;
            }
            if (await ValidatePermissions())
            {
                if (Device.RuntimePlatform == Device.iOS)
                {
                    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
                    var result  = await scanner.Scan();

                    if (result == null)
                    {
                        return;
                    }
                    try
                    {
                        var qrCodeId = result.Text.GetHashCode() + "";
                        if (_oListSource.Any(o => string.Compare(o.Id, qrCodeId, StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            App.ShowToast(AppResources.qrcode_exists);
                        }
                        else
                        {
                            AddNewRecord(qrCodeId);
                        }
                    }
                    catch (Exception ex)
                    {
                        App.AddLog(ex.Message);
                    }
                }
                else
                {
                    const BarcodeFormat expectedFormat = BarcodeFormat.QR_CODE;
                    var opts = new ZXing.Mobile.MobileBarcodeScanningOptions
                    {
                        PossibleFormats = new List <BarcodeFormat> {
                            expectedFormat
                        }
                    };
                    System.Diagnostics.Debug.WriteLine("Scanning " + expectedFormat);

                    var scanPage = new ZXingScannerPage(opts);
                    scanPage.OnScanResult += (result) =>
                    {
                        scanPage.IsScanning = false;

                        Device.BeginInvokeOnMainThread(async() =>
                        {
                            await Navigation.PopAsync();
                            try
                            {
                                var qrCodeId = result.Text.GetHashCode() + "";
                                if (_oListSource.Any(
                                        o => string.Compare(o.Id, qrCodeId, StringComparison.OrdinalIgnoreCase) == 0))
                                {
                                    App.ShowToast(AppResources.qrcode_exists);
                                }
                                else
                                {
                                    AddNewRecord(qrCodeId);
                                }
                            }
                            catch (Exception ex)
                            {
                                App.AddLog(ex.Message);
                            }
                        });
                    };

                    await Navigation.PushAsync(scanPage);
                }
            }
        }