Esempio n. 1
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();
            }
        }
        private async void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var Deferral = args.GetDeferral();

            if (SecondaryButtonText == "中止")
            {
                args.Cancel   = true;
                AbortFromHere = true;

                try
                {
                    await ObexClient.AbortAsync();
                }
                catch (Exception) { }
            }
            else if (SecondaryButtonText == "重试")
            {
                args.Cancel       = true;
                ProgressText.Text = "0%";

                ObexClient.DataTransferFailed     -= ObexClient_DataTransferFailed;
                ObexClient.DataTransferProgressed -= ObexClient_DataTransferProgressed;
                ObexClient.DataTransferSucceeded  -= ObexClient_DataTransferSucceeded;
                ObexClient.ConnectionFailed       -= ObexClient_ConnectionFailed;
                ObexClient.Aborted         -= ObexClient_Aborted;
                ObexClient.Disconnected    -= ObexClient_Disconnected;
                ObexClient.DeviceConnected -= ObexClient_DeviceConnected;

                ObexClient = ObexServiceProvider.GetObexNewInstance();

                ObexClient.DataTransferFailed     += ObexClient_DataTransferFailed;
                ObexClient.DataTransferProgressed += ObexClient_DataTransferProgressed;
                ObexClient.DataTransferSucceeded  += ObexClient_DataTransferSucceeded;
                ObexClient.ConnectionFailed       += ObexClient_ConnectionFailed;
                ObexClient.Aborted         += ObexClient_Aborted;
                ObexClient.Disconnected    += ObexClient_Disconnected;
                ObexClient.DeviceConnected += ObexClient_DeviceConnected;

                try
                {
                    ProgressControl.Value = 0;
                    CloseButtonText       = "";
                    SecondaryButtonText   = "中止";
                    await ObexClient.ConnectAsync();

                    await ObexClient.SendFileAsync(ToDeleteFile);
                }
                catch (Exception)
                {
                    ProgressText.Text = "尝试重新连接失败";
                }
            }

            Deferral.Complete();
        }
Esempio n. 3
0
        public BluetoothFileTransfer(StorageFile FileToSend)
        {
            InitializeComponent();
            this.FileToSend = FileToSend ?? throw new ArgumentNullException(nameof(FileToSend), "Parameter could not be null");

            ObexClient = ObexServiceProvider.GetObexInstance();

            TransferName.Text       = $"{Globalization.GetString("Bluetooth_Transfer_FileName")}: {FileToSend.Name}";
            TransferDeviceName.Text = $"{Globalization.GetString("Bluetooth_Transfer_DeviceName")}: {ObexServiceProvider.DeviceName}";

            Loaded  += BluetoothFileTransfer_Loaded;
            Closing += BluetoothFileTransfer_Closing;
        }
Esempio n. 4
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();
        }
Esempio n. 5
0
        private async void QueueContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var Deferral = args.GetDeferral();

            try
            {
                if (SecondaryButtonText == Globalization.GetString("Bluetooth_Transfer_RetryButton"))
                {
                    args.Cancel = true;
                    ProgressControl.IsIndeterminate = true;
                    ProgressText.Text = "0%";

                    ObexClient.DataTransferFailed     -= ObexClient_DataTransferFailed;
                    ObexClient.DataTransferProgressed -= ObexClient_DataTransferProgressed;
                    ObexClient.DataTransferSucceeded  -= ObexClient_DataTransferSucceeded;
                    ObexClient.ConnectionFailed       -= ObexClient_ConnectionFailed;
                    ObexClient.Aborted         -= ObexClient_Aborted;
                    ObexClient.Disconnected    -= ObexClient_Disconnected;
                    ObexClient.DeviceConnected -= ObexClient_DeviceConnected;

                    ObexClient = ObexServiceProvider.GetObexInstance();

                    ObexClient.DataTransferFailed     += ObexClient_DataTransferFailed;
                    ObexClient.DataTransferProgressed += ObexClient_DataTransferProgressed;
                    ObexClient.DataTransferSucceeded  += ObexClient_DataTransferSucceeded;
                    ObexClient.ConnectionFailed       += ObexClient_ConnectionFailed;
                    ObexClient.Aborted         += ObexClient_Aborted;
                    ObexClient.Disconnected    += ObexClient_Disconnected;
                    ObexClient.DeviceConnected += ObexClient_DeviceConnected;

                    try
                    {
                        ProgressControl.Value = 0;
                        CloseButtonText       = string.Empty;
                        SecondaryButtonText   = Globalization.GetString("BluetoothTranfer.SecondaryButtonText");
                        await ObexClient.ConnectAsync().ConfigureAwait(true);

                        await ObexClient.SendFileAsync(FileToSend).ConfigureAwait(true);
                    }
                    catch (Exception)
                    {
                        ProgressText.Text = Globalization.GetString("Bluetooth_Transfer_Description_6");
                    }
                }
                else
                {
                    args.Cancel   = true;
                    AbortFromHere = true;

                    try
                    {
                        await ObexClient.AbortAsync().ConfigureAwait(true);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            finally
            {
                Deferral.Complete();
            }
        }