#StackBounty: #c# #wpf #bluetooth #32feet Unable to connect Bluetooth device using c#

Bounty: 50

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);
    }
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.