I have a digital Human Interface Device that I'm trying to connect to using 32feet.net so I can read input data from it and process an output in my application. I've never programmed with bluetooth before and am learning as I go.
I can connect my device to Windows 7 using the microsoft/broadcom stack no problem. I can also discover the device using 32feet.net but when I try to connect it I get an error. I made sure my stack was supported using BluetoothRadio.IsSupported Here is a code snippet:
var client = new BluetoothClient();
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
Console.WriteLine(device.DeviceName);
Guid serviceClass = BluetoothService.HumanInterfaceDevice;
client.Connect(new BluetoothEndPoint(addr, serviceClass));
The last line causes the following error: A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
I've tried some other methods as well with client.Connect. I've tried using DiscoverDevices to get a DeviceInfo array and manually choosing the device out of that array and connecting to it. I've tried setting the serviceClass as Empty because that is what shows up when I use DeviceInfo.ClassOfDevice.Service. I've been able to connect the device to windows using DeviceInfo.SetServiceState(BluetoothService.HumanInterfaceDevice, true) but not able to get a stream from it.
Even if I can get a data stream from the device after it's been connected in Windows, that would be fine. My goal here is simple to be able to read input from the device like a button press.
Try this:
var client = new BluetoothClient();
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
Console.WriteLine(device.DeviceName);
BluetoothSecurity.PairRequest(addr, "Whatever pin");
device.SetServiceState(BluetoothService.HumanInterfaceDevice, true);
Thread.Sleep(100); // Just in case
if (device.InstalledServices.Length == 0)
{
// I wouldn't know why it doesn't install the service
}
client.Connect(addr, BluetoothService.HumanInterfaceDevice);
I am no expert by any means, but for me:
In every stage something needs to happen:
On another note, the exception that you get is really cryptic, but it just made me think if the device is not already connected to something. If you check "device.Connected" it must return false or you won't ever be able to connect.