Exemple #1
0
        public async Task mainAsync()
        {
            /// 接続
            //Connecting ...
            state = "Listening";
            try
            {
                rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid));
            }
            // Catch exception HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_AVAILABLE).
            catch (Exception ex) when((uint)ex.HResult == 0x800710DF)
            {
                state = "None";
                return;
            }


            // Create a listener for this service and start listening
            socketListener = new StreamSocketListener();
            socketListener.ConnectionReceived += OnConnectionReceived;
            var rfcomm = rfcommProvider.ServiceId.AsString();

            await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

            // Set the SDP attributes and start Bluetooth advertising
            InitializeServiceSdpAttributes(rfcommProvider);

            try
            {
                rfcommProvider.StartAdvertising(socketListener, true);
            }
            catch (Exception e)
            {
                // If you aren't able to get a reference to an RfcommServiceProvider, tell the user why.  Usually throws an exception if user changed their privacy settings to prevent Sync w/ Devices.
                state = "None";
                return;
            }
            //Listening for incoming connections
        }
        public async Task InitializeRfcommServer()
        {
            try
            {
                this.rfcommProvider =
                    await RfcommServiceProvider.CreateAsync(
                        RfcommServiceId.FromUuid(Constants.RfcommDeviceServiceUuid));
            }
            // Catch exception HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_AVAILABLE).
            catch (Exception ex) when((uint)ex.HResult == 0x800710DF)
            {
                Debug.Write("Make sure your Bluetooth Radio is on: " + ex.Message);
                throw;
            }

            // Create a listener for this service and start listening
            this.socketListener = new StreamSocketListener();
            this.socketListener.ConnectionReceived += this.OnConnectionReceived;
            var rfcomm = this.rfcommProvider.ServiceId.AsString();

            await this.socketListener.BindServiceNameAsync(this.rfcommProvider.ServiceId.AsString(),
                                                           SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

            // Set the SDP attributes and start Bluetooth advertising
            this.InitializeServiceSdpAttributes(this.rfcommProvider);

            try
            {
                this.rfcommProvider.StartAdvertising(this.socketListener, true);
            }
            catch (Exception e)
            {
                Debug.Write(e);
                throw;
            }

            Debug.Write("Listening for incoming connections");
        }
Exemple #3
0
        private async Task Start()
        {
            try
            {
                rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort);

                socketListener = new StreamSocketListener();
                socketListener.ConnectionReceived += OnConnectionReceived;
                await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(),
                                                          SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

                rfcommProvider.StartAdvertising(socketListener, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, $"Could not start listener. {ex.Message}");
                return;
            }

            status.Text         = "Listening for connections...";
            startButton.Enabled = false;
            stopButton.Enabled  = true;
        }
        private async Task StartRfcommService()
        {
            var serviceGuid = Guid.Parse("34B1CF4D-1069-4AD6-89B6-E161D79BE4D8");
            var serviceId   = RfcommServiceId.FromUuid(serviceGuid);

            rfcommProvider = await RfcommServiceProvider.CreateAsync(serviceId);

            streamSocketListener = new StreamSocketListener();
            streamSocketListener.ConnectionReceived += StreamSocketListener_ConnectionReceived;

            try
            {
                await streamSocketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

                rfcommProvider.StartAdvertising(streamSocketListener);

                DiagnosticInfo.Display(null, "RFCOMM service started. Waiting for clients...");
            }
            catch (Exception ex)
            {
                DiagnosticInfo.Display(null, ex.Message);
            }
        }
        private async Task InitBrowseWpToWin()
        {
            provider = await RfcommServiceProvider.CreateAsync(
                RfcommServiceId.FromUuid(rfcommServiceUuid));

            var listener = new StreamSocketListener();

            listener.ConnectionReceived += HandleConnectionReceived;

            await listener.BindServiceNameAsync(
                provider.ServiceId.AsString(),
                SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

            using (var writer = new DataWriter())
            {
                writer.WriteByte(ServiceVersionAttributeType);
                writer.WriteUInt32(ServiceVersion);

                var data = writer.DetachBuffer();
                provider.SdpRawAttributes.Add(ServiceVersionAttributeId, data);
                provider.StartAdvertising(listener);
            }
        }
        /// <summary>
        /// Initializes the server using RfcommServiceProvider to advertise the Chat Service UUID and start listening
        /// for incoming connections.
        /// </summary>
        private async void InitializeRfcommServer()
        {
            try
            {
                rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(rfcommChatServiceUuid));
            }
            catch (Exception e)
            {
                // If you aren't able to get a reference to an RfcommServiceProvider, tell the user why.  Usually throws an exception if the Bluetooth radio is off.
                Status.Text = e.Message;
                return;
            }


            // Create a listener for this service and start listening
            socketListener = new StreamSocketListener();
            socketListener.ConnectionReceived += OnConnectionReceived;

            await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(),
                                                      SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

            // Set the SDP attributes and start Bluetooth advertising
            InitializeServiceSdpAttributes(rfcommProvider);

            try
            {
                rfcommProvider.StartAdvertising(socketListener, true);
            }
            catch (Exception e)
            {
                Status.Text = e.Message;
                return;
            }


            Status.Text = "Listening for incoming connections";
        }
        private async void InitializeRfcommServer()
        {
            Button_server_start.IsEnabled      = false;
            Button_server_disconnect.IsEnabled = true;
            try
            {
                rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(RfcommChatServiceUuid));
            }
            catch (Exception ex) when((uint)ex.HResult == 0x800710DF)
            {
                // Catch exception HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_AVAILABLE).
                TextBox_log.Text += string.Format("Make sure your Bluetooth Radio is on : {0}\n", ex.Message);
                Button_server_start.IsEnabled      = true;
                Button_server_disconnect.IsEnabled = false;
                return;
            }
            socketListener = new StreamSocketListener();
            socketListener.ConnectionReceived += OnConnectionReceived;
            var rfcomm = rfcommProvider.ServiceId.AsString();
            await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

            // Set the SDP attributes and start Bluetooth advertising
            InitializeServiceSdpAttributes(rfcommProvider);
            try
            {
                rfcommProvider.StartAdvertising(socketListener, true);
            }
            catch (Exception e)
            {
                TextBox_log.Text += string.Format("InitializeRfcommServer : Exception occured : {0}\n", e.Message);
                Button_server_start.IsEnabled      = true;
                Button_server_disconnect.IsEnabled = false;
                return;
            }
            TextBox_log.Text += ("Listening for incoming connections\n");
        }
Exemple #8
0
        /// <summary>
        /// Initializes the server using RfcommServiceProvider to advertise the Chat Service UUID and start listening
        /// for incoming connections.
        /// </summary>
        private async void InitializeRfcommServer()
        {
            ListenButton.IsEnabled     = false;
            DisconnectButton.IsEnabled = true;

            try
            {
                rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid));
            }
            // Catch exception HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_AVAILABLE).
            catch (Exception ex) when((uint)ex.HResult == 0x800710DF)
            {
                // The Bluetooth radio may be off.
                //MP..NotifyUser("Make sure your Bluetooth Radio is on: " + ex.Message, NotifyType.ErrorMessage);
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    status.Text = string.Format("{0}: {1}", MainPage.NotifyType.ErrorMessage,
                                                "Make sure your Bluetooth Radio is on: ");
                });

                ListenButton.IsEnabled     = true;
                DisconnectButton.IsEnabled = false;
                return;
            }


            // Create a listener for this service and start listening
            socketListener = new StreamSocketListener();
            socketListener.ConnectionReceived += OnConnectionReceived;
            var rfcomm = rfcommProvider.ServiceId.AsString();

            await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(),
                                                      SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

            // Set the SDP attributes and start Bluetooth advertising
            InitializeServiceSdpAttributes(rfcommProvider);

            try
            {
                rfcommProvider.StartAdvertising(socketListener, true);
            }
            catch (Exception e)
            {
                // If you aren't able to get a reference to an RfcommServiceProvider, tell the user why.  Usually throws an exception if user changed their privacy settings to prevent Sync w/ Devices.
                //MP.NotifyUser(e.Message, NotifyType.ErrorMessage);
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    status.Text = string.Format("{0}: {1}", MainPage.NotifyType.ErrorMessage,
                                                e.Message);
                });

                ListenButton.IsEnabled     = true;
                DisconnectButton.IsEnabled = false;
                return;
            }

            //MP.NotifyUser("Listening for incoming connections", NotifyType.StatusMessage);
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                status.Text = string.Format("{0}: {1}", MainPage.NotifyType.StatusMessage,
                                            "Listening for incoming connections");
            });
        }
Exemple #9
0
        //private IRfcommServiceProviderWrapper _provider;

        internal Adapter()
        {
            _provider = RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort).AsTask().Result;
            //_provider = RfcommServiceProviderWrapperFactory.GetProvider(RfcommServiceId.SerialPort);
        }