Ejemplo n.º 1
0
        public async Task EnumerateDevicesAsync()
        {
            BluetoothService btService = BluetoothService.GetDefault();

            btService.SearchForPairedDevicesFailed    += btService_SearchForPairedDevicesFailed;
            btService.SearchForPairedDevicesSucceeded += btService_SearchForPairedDevicesSucceeded;
            await btService.SearchForPairedDevicesAsync();
        }
Ejemplo n.º 2
0
        private async void QueueContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var Deferral = args.GetDeferral();

            try
            {
                if (BluetoothControl.SelectedIndex == -1 || !BluetoothDeviceCollection[BluetoothControl.SelectedIndex].DeviceInfo.Pairing.IsPaired)
                {
                    Tips.Text       = Globalization.GetString("BluetoothUI_Tips_Text_1");
                    Tips.Visibility = Visibility.Visible;
                    args.Cancel     = true;
                }
                else
                {
                    //首先连接到RFComm服务,获取到设备的规范名称
                    string CanonicalName = await ConnectToRfcommServiceAsync(BluetoothDeviceCollection[BluetoothControl.SelectedIndex]).ConfigureAwait(true);

                    BluetoothService BTService = BluetoothService.GetDefault();
                    BTService.SearchForPairedDevicesSucceeded += BTService_SearchForPairedDevicesSucceeded;

                    void BTService_SearchForPairedDevicesSucceeded(object sender, SearchForPairedDevicesSucceededEventArgs e)
                    {
                        BTService.SearchForPairedDevicesSucceeded -= BTService_SearchForPairedDevicesSucceeded;

                        if (e.PairedDevices.FirstOrDefault((Device) => Device.DeviceHost.CanonicalName == CanonicalName) is BluetoothDevice BTDevice)
                        {
                            ObexServiceProvider.SetObexInstance(BTDevice, BluetoothDeviceCollection[BluetoothControl.SelectedIndex].Name);

                            if (ObexServiceProvider.GetObexInstance() == null)
                            {
                                throw new Exception(Globalization.GetString("BluetoothUI_Tips_Text_2"));
                            }
                        }
                        else
                        {
                            throw new Exception(Globalization.GetString("BluetoothUI_Tips_Text_2"));
                        }
                    }

                    //能到这里说明该设备已经配对,启动搜索,完成后PairedBluetoothDeviceCollection被填充
                    await BTService.SearchForPairedDevicesAsync().ConfigureAwait(true);
                }
            }
            catch (Exception e)
            {
                args.Cancel = true;

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Tips.Text       = e.Message;
                    Tips.Visibility = Visibility.Visible;
                });
            }
            finally
            {
                Deferral.Complete();
            }
        }
Ejemplo n.º 3
0
        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var Deferral = args.GetDeferral();

            if (BluetoothControl.SelectedIndex == -1 || !BluetoothDeviceCollection[BluetoothControl.SelectedIndex].DeviceInfo.Pairing.IsPaired)
            {
                Tips.Text       = "请先选择一个已配对的设备";
                Tips.Visibility = Visibility.Visible;
                args.Cancel     = true;
                Deferral.Complete();
                return;
            }


            try
            {
                //首先连接到RFComm服务,获取到设备的规范名称
                string CanonicalName = await ConnectToRfcommServiceAsync(BluetoothDeviceCollection[BluetoothControl.SelectedIndex]);

                BluetoothService BTService = BluetoothService.GetDefault();
                BTService.SearchForPairedDevicesSucceeded += (s, e) =>
                {
                    PairedBluetoothDeviceCollection = e.PairedDevices;
                };

                //能到这里说明该设备已经配对,启动搜索,完成后PairedBluetoothDeviceCollection被填充
                await BTService.SearchForPairedDevicesAsync();

                foreach (var BTDevice in from BTDevice in PairedBluetoothDeviceCollection
                         //找到符合刚刚RFComm服务获取到的规范名称的蓝牙设备
                         where BTDevice.DeviceHost.CanonicalName == CanonicalName
                         select BTDevice)
                {
                    //从该设备的BluetoothDevice对象获取到Obex服务的实例
                    ObexServiceProvider.SetObexInstance(BTDevice);
                    break;
                }

                if (ObexServiceProvider.GetObexNewInstance() == null)
                {
                    throw new Exception("未能找到已配对的设备,请打开该设备的蓝牙开关");
                }
            }
            catch (Exception e)
            {
                Tips.Text       = e.Message;
                Tips.Visibility = Visibility.Visible;
            }

            Deferral.Complete();
        }