A
A
aeaeae12021-07-29 13:54:11
C++ / C#
aeaeae1, 2021-07-29 13:54:11

Why is it not connecting via bluetooth to the phone?

I'm trying to connect via bluetooth to my phone. Here is the connection code

private void Connect()
    {
            using (SelectBluetoothDeviceDialog bldialog = new SelectBluetoothDeviceDialog())
            {
                bldialog.ShowAuthenticated = true;
                bldialog.ShowRemembered = true;
                bldialog.ShowUnknown = true;

                if (bldialog.ShowDialog() == DialogResult.OK)
                {
                    SetControlState(false);

                    tsStatusLabel.Text = "Operation started";

                    if (bldialog.SelectedDevice == null)
                    {
                        MessageBox.Show("No device selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    BluetoothDeviceInfo selecteddevice = bldialog.SelectedDevice;
                    BluetoothEndPoint remoteEndPoint = new BluetoothEndPoint(selecteddevice.DeviceAddress,
                                                                       BluetoothService.ObexFileTransfer);

                    client = new BluetoothClient();
                    try
                    {
                        client.Connect(remoteEndPoint);
                        session = new ObexClientSession(client.GetStream(), UInt16.MaxValue);
                        session.Connect(ObexConstant.Target.FolderBrowsing);
                    }
                    catch (SocketException ex)
                    {
                        ExceptionHandler(ex, false);
                        return;
                    }
                    catch (ObjectDisposedException ex)
                    {
                        ExceptionHandler(ex, false);
                        return;
                    }
                    catch (IOException ex)
                    {
                        ExceptionHandler(ex, false);
                        return;
                    }

                    tsStatusLabel.Text = string.Format("Connected to: {0}", selecteddevice.DeviceName);
                    this.Text = string.Format("Bluetooth Device Browser. Current Device: {0}", selecteddevice.DeviceName);

                    tsiConnect.Enabled = tsbConnect.Enabled = false;
                    bgwWorker.RunWorkerAsync();
                }
            }
    }

It gives such an error
connection failed the required address for its context is incorrect
I also tried to enter the address manually in another way
static BluetoothAddress addr = BluetoothAddress.Parse("e8:6d:cb:7d:1d:b1"); //from device            
BluetoothDeviceInfo device = new BluetoothDeviceInfo(addr); 

device.SetServiceState(serviceClass, true);

Console.WriteLine(BluetoothSecurity.PairRequest(device.DeviceAddress, "0000")); //pairing my device - writes True
BluetoothEndPoint ep = new BluetoothEndPoint(addr, serviceClass);

BluetoothClient conn = new BluetoothClient(ep); 
conn.Connect(ep);
Console.WriteLine(conn.GetStream());

Still the same error

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question