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();
            }
        }
Esempio n. 2
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. 3
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();
            }
        }