1

So I am trying to connect a bluetooth speakers from a script. I am using 32feet.net and I have successfully found the device but it doesn't work when I try to pair and connect to it.

This is the code im using to pair to device, this always fails not sure why:

 private static void connected(BluetoothDeviceInfo[] dev)
   {
      // dev[foundIndex];
       bool paired=false;

       paired = BluetoothSecurity.PairRequest(dev[foundIndex].DeviceAddress, "1166");

       if (paired)
           Console.WriteLine("Passed, Device is connected.");
       else
           Console.WriteLine("Failed....");
   }

Here is the code called after connected to actually connect to the device: bc is my bluetooth client var.

 bc.BeginConnect(devInfo[foundIndex].DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), devInfo[foundIndex]);

private static void Connect(IAsyncResult result)
    {
        if (result.IsCompleted)
        {
            Console.Write("Connected... ");
        }

    }

Any help would be appreciated. I am new to 32feet.net so i dont know much about this, i tried following code online to get where im at.

user3528365
  • 47
  • 2
  • 9

1 Answers1

0

Try BluetoothDeviceInfo.SetServiceState. That will ask Windows to connect to the audio service on the device -- hopefully that'll do the job.

See https://32feet.codeplex.com/wikipage?title=Connecting%20to%20Bluetooth%20Services

Sometimes we don’t want our application to itself send data to/from a remote service but we want instead the local operating system to do so. This is the case for keyboard/mouse/etc with HID, networking with DUN/NAP/PAN/etc, Headset/Handsfree etc.

and then

The short answer in this case is to use BluetoothDeviceInfo.SetServiceState. This is the API equivalent to manually checking the respective checkbox on the “Services” tab of the Device dialog in Bluetooth Control panel.

Also, in these days of Secure Simple Pairing, using PairRequest is fine only if all peer devices will use old style PIN code authentication, otherwise instantiate a BluetoothWin32Authentication and then do the connect (here indirectly via SetServiceState) and handle the authentication in the authentication callback.

alanjmcf
  • 3,430
  • 1
  • 17
  • 14
  • BluetoothDeviceInfo.SetServiceState(BluetoothService.Handsfree, true); So I added this line of code in my project right after I call find(devInfo). But this line wont compile due to no object. Am i missing something? – user3528365 May 19 '14 at 17:19
  • Okay so now when i run my script it prompts the user to click on a badge on the screen(im using windows 8.1) Is there anyway to bypass this and just connect without the click? – user3528365 May 19 '14 at 19:47
  • To help others, was it SetServiceStart & BluetoothWin32Authentication do the job, or was other magic required? Ta – alanjmcf May 20 '14 at 15:14
  • So I didnt use SetServiceStart, just BluetoothWin32Authentication. I followed this link that you also commented on and it helped me see where i made my mistake. http://stackoverflow.com/questions/6998072/windows-bluetooth-autopairing-or-disable-authentication – user3528365 May 20 '14 at 19:55