Exemple #1
0
        public FormsApp(IBluetoothLowEnergyAdapter adapter, IUserDialogs dialogs)
        {
            InitializeComponent();

            m_dialogs = dialogs;
            var logsVm = new LogsViewModel();

            SystemLog.Instance.AddSink(logsVm);

            var bleAssembly = adapter.GetType().GetTypeInfo().Assembly.GetName();

            Log.Info(bleAssembly.Name + "@" + bleAssembly.Version);

            var bleGattServerViewModel = new BleGattServerViewModel(dialogs, adapter);
            var bleScanViewModel       = new BleDeviceScannerViewModel(
                bleAdapter: adapter,
                dialogs: dialogs,
                onSelectDevice: async p =>
            {
                //更新 Server 中的设备信息
                await bleGattServerViewModel.Update(p);
                // 点击了 连接 后,跳转到 设备的 Server 的页面.
                await m_rootPage.PushAsync(
                    new BleGattServerPage(
                        model: bleGattServerViewModel,
                        //点击了 ListView 中的 Service 后,跳转到 Service 页.
                        bleServiceSelected: async s => { await m_rootPage.PushAsync(new BleGattServicePage(s)); }));

                //连接设备
                await bleGattServerViewModel.OpenConnection();
            });

            m_rootPage = new NavigationPage(
                new TabbedPage
            {
                Title = "BLE.net Sample App",
                // Tab 中的 Scan View 和 Log View.
                Children = { new BleDeviceScannerPage(bleScanViewModel), new LogsPage(logsVm) }
            });

            MainPage = m_rootPage;
        }
        private void connection()
        {
            //var logsVm = new LogsViewModel();
            //SystemLog.Instance.AddSink(logsVm);

            var bleAssembly = bleAdapterSaved.GetType().GetTypeInfo().Assembly.GetName();

            Log.Info(bleAssembly.Name + "@" + bleAssembly.Version);

            var bleGattServerViewModel = new BleGattServerViewModel(dialogsSaved, bleAdapterSaved);

            bleScanViewModel = new BleDeviceScannerViewModel(
                bleAdapter: bleAdapterSaved,
                dialogs: dialogsSaved,
                onSelectDevice:



                async p =>
            {
                await bleGattServerViewModel.Update(p);

                background_scan_page_detail.IsVisible = true;



                background_scan_page.IsVisible = false;


                BindingContext = bleGattServerViewModel;

                bleScanViewModel.StopScan();


                macAddress.Text = p.Address;
                deviceID.Text   = p.Name;
                rssiLevel.Text  = p.Rssi.ToString() + " db";



                await Task.Run(async() =>
                {
                    await Task.Delay(500); Device.BeginInvokeOnMainThread(async() =>
                    {
                        await bleGattServerViewModel.OpenConnection();

                        navigationDrawerList.IsEnabled = true;

                        navigationDrawerList.Opacity = 1;

                        bleGattServerViewModel.returnConnect();

                        savedServer = bleGattServerViewModel;
                    });
                });
            }


                );

            BindingContext = bleScanViewModel;



            NavigationPage.SetHasNavigationBar(this, false); //Turn off the Navigation bar

            Task.Run(async() =>
            {
                await Task.Delay(500); Device.BeginInvokeOnMainThread(() =>
                {
                    bleScanViewModel.ScanForDevicesCommand.Execute(true);
                });
            });
        }