nag
nag

Reputation: 930

Unable to connect Bluetooth device using c#

I'm using 32 feet library to develop Bluetooth communication WPF app, and able to pair the device but not working to connect it and ended up with an exception like below.

Note: I've tried to connect the devices like my mobile and my PC, but both are giving the same errors as explained below.

I've seen somewhere about this issue and they mentioned like, this issue may be because of 32 feet library is not compatible with the Bluetooth device that I've in my PC.

But actually, I've tested this in some other PC's which are running with Windows 7 OS - 64 bit and getting the same error message.

Anyone help me out. Thank you.

Error Message: The requested address is not valid in its context ECD09F51114A:0000110100001000800000805f9b34fb

Exception Details:

My code sample:

Guid uId = new Guid("0000110E-0000-1000-8000-00805f9b34fb");
bool receiverStarted = false;
private List<BluetoothDeviceInfo> deviceList;
private List<string> deviceNames;
private BluetoothDeviceInfo deviceInfo;
private string myPin = "1234";
private BluetoothClient sender;

private void BtnScan_Click(object sender, RoutedEventArgs e)
{
    ScanAvailableDevices();
}
private void ScanAvailableDevices()
{
    lstAvailableDevices.ItemsSource = null;
    lstAvailableDevices.Items.Clear();
    deviceList.Clear();
    deviceNames.Clear();
    Thread senderThread = new Thread(new ThreadStart(Scan));
    senderThread.Start();
}

private void Scan()
{
     UpdateStatus("Starting scan...");
     sender = new BluetoothClient();
     availableDevices = sender.DiscoverDevicesInRange();
     UpdateStatus("Scan completed.");
     UpdateStatus(availableDevices.Length.ToString() + " device(s) discovered");

     foreach(BluetoothDeviceInfo device in availableDevices)
     {
        deviceList.Add(device);
        deviceNames.Add(device.DeviceName);
     }

     UpdateAvailableDevices();  
}

private void UpdateAvailableDevices()
{
    Func<int> devicesDelegate = delegate ()
            {
                lstAvailableDevices.ItemsSource = deviceNames;
                return 0;
            };

            Dispatcher.BeginInvoke((Action)(() =>
            {
                devicesDelegate.Invoke();
            }));
 }

private void PairDevice()
        {
            deviceInfo = deviceList[lstAvailableDevices.SelectedIndex];
            if (CanPair())
            {
                UpdateStatus("Device paired..");
                UpdateStatus("Starting to connect the device");
                Thread senderThread = new Thread(new ThreadStart(SenderConnectThread));
                senderThread.Start();
            }
        }

        private bool CanPair()
        {
            if(!deviceInfo.Authenticated)
            {
                if(!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress,myPin))
                {
                    return false;
                }
            }
            return true;
        }

private void LstAvailableDevices_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    deviceInfo = deviceList[lstAvailableDevices.SelectedIndex];
    UpdateStatus(deviceInfo.DeviceName + " was selected, attempting connect");

    if (CanPair())
    {
        UpdateStatus("Device paired..");
        UpdateStatus("Starting connect thread");
        Thread senderThread = new Thread(new ThreadStart(ClientConnectThread));
        senderThread.Start();
    }
    else
    {
        UpdateStatus("Pair failed");
    }
}

private void ClientConnectThread()
{
    BluetoothClient sender = new BluetoothClient();
    BluetoothAddress address = deviceInfo.DeviceAddress;
    //sender.SetPin(deviceInfo.DeviceAddress, myPin);
    var endPoint = new BluetoothEndPoint(address, uId);
    sender.Connect(endPoint);

    //Another way that I've tried
    BluetoothClient client = new BluetoothClient();
    UpdateStatus("Attempting connect");
    //client.Connect(deviceInfo.DeviceAddress, uId);
    client.BeginConnect(deviceInfo.DeviceAddress, uId, this.BluetoothClientConnectCallback, client);
}

void BluetoothClientConnectCallback(IAsyncResult result)
{
    BluetoothClient senderE = (BluetoothClient)result.AsyncState;
    senderE.EndConnect(result);

    Stream stream = senderE.GetStream();

    while (true)
    {
        while (!ready) ;
        byte[] message = Encoding.ASCII.GetBytes(txtSenderMessage.Text);

        stream.Write(message, 0, message.Length);
    }
}

Upvotes: 5

Views: 3542

Answers (3)

nag
nag

Reputation: 930

Folks, I'm able to pair and connect it using the same application running in different PC and acting it as a server. Earlier I've tried this without having the same application running in the target PC and thus it's giving the error that I mentioned above.

Thanks guys for your time and support.

Upvotes: 0

Max Destiny
Max Destiny

Reputation: 9

There are libraries in UWP where you can easily make a connection between your desktop and other devices , you can easily handle the Bluetooth Adapter.

Upvotes: 0

Jin Thakur
Jin Thakur

Reputation: 2773

There are multiple downloads for 32 feet

Try these Downloading https://github.com/inthehand/32feet

Downloads are available here on the Downloads tab. Packages are also available at NuGet:-

InTheHand.Devices.Bluetooth - Modern (v4.x) - Preview NuGet version

32feet.NET - Legacy (v3.x) NuGet version

32feet.NET.Phone - Windows Phone NuGet version

InTheHand.Devices.Enumeration (Windows 8 / Windows Phone Device Pickers) NuGet version

Upvotes: 0

Related Questions