5

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);
    }
}
Ali
  • 1,326
  • 1
  • 17
  • 38
nag
  • 920
  • 6
  • 27
  • 51

3 Answers3

0

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

Jin Thakur
  • 2,711
  • 18
  • 15
  • 1
    I've the latest library which is 32feet 4.0, but didn't have any samples that how to start/use of it. Could you please help me to get any samples to use 32feet 4.0 library - Thank you. – nag Mar 05 '19 at 07:53
0

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.

nag
  • 920
  • 6
  • 27
  • 51
  • How can I say then? The question I posted here have a solution now and I figured it out my self and sharing the same by posting answer. Even I'm not selected this post as a answer, how this can be down voted I don't know? – nag Mar 06 '19 at 10:39
  • Either ask a new question or edit the question so it fits your requirements. A new scenario would probably fit better in a new question. A question doesn't have a continuation, it's question -> answer -> closed. There might be some new answers on the same question that come with newer versions of a language or library but there should't ever be a new question. – Lloyd Mar 06 '19 at 10:54
0

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.

  • How about using in WPF application? Because I've a WPF application which is having serial port communication to communicate/transfer the data to hardware device and now I'm planning to add Bluetooth communication along with serial communication. – nag Mar 06 '19 at 10:45
  • Any libraries in C#.NET/WPF to communicate Bluetooth adapter which is connected to my hardware device. – nag Mar 06 '19 at 10:47
  • Can we move our discussion to here -> https://stackoverflow.com/questions/55005961/c-sharp-bluetooth-communication-for-bluetooth-connected-hardware – nag Mar 06 '19 at 10:58