Hi,
Wow i got it finally solved for
the widcomm stack and happy to share it with
everyone. Maybe some of this can be included in the Widcomm
documentation since, to be honest, it is not clear from the document that the bluetooth listener is quite a different beast when you deal
with a WIDCOMM stack. The previous post with the image to disable the serial
port service was the key to the whole problem. Of course as a developer you
want to disable the serial port service programmatically and enable it when
your application exists. Luckily you can do this by going into the registry. It
took me a bit of testing to see which Key was the one that enables/disables the
serial port service but i found it. So how does it
all work if you want to listen to sensors (e.g. blood glucose monitors) that
use the standard Serial Port service.
You can use the BluetoothListener
as normal (so the same code as for a Microsoft stack. So something like this:
// Disable the Serial Port service on your phone
DisableWidcommSPP();
// Start a listener
and wait for incoming serial port connection
bluetoothListener
= new BluetoothListener(BluetoothService.SerialPort);
bluetoothListener.ServiceName = “your
particular name if you need to”;
bluetoothListener.Start();
// Start a new thread to deal with an incoming Bluetooth connection
Thread t = new Thread(new ThreadStart(ListenLoop));
t.Start();
private void ListenLoop()
{
BluetoothClient btClient =null;
while(listening)
{
try
{
// This call blocks till we get an incoming connection
btClient= bluetoothListener.AcceptBluetoothClient();
// Bond with the device? Very handy if you don’t wantthose annoying popups. Does not work always though!
BluetoothSecurity.PairRequest(btClient.RemoteEndPoint.Address,“the pin for this device”);
// Now do soemthing with theconnection etc
btClient.GetStream());
}
catch (Exception ex)
{
string msg = ex.Message;
}
} // while
// When to closethe connection?
if (btClient!= null)
btClient.Close();
}
// Somewhere in your application restore theserial port service.
RestoreWidcommSPP(;
NOW THIS IS IMPORTANT FOR WIDCOMM. You need to disable the serial port service on the phone so that theBLuetoothListener gets the incoming connection. You can programmaticallydisable the seruial port service by setting the “Auto” key to 0. So before youstart listening with BluetoothListener disable it. You want to enable it onceyou are done with the listener! Here is the code
/// <summary>
/// Disable the serial port service on the phone so long welisten for incoming connections
/// </summary>
public static voidDisableWidcommSPP()
{
RegistryKeyregistryKey = null;
try
{
registryKey = Registry.LocalMachine.CreateSubKey("Software\\WIDCOMM\\BTConfig\\Services\\0001\\");
objectvalue = registryKey.GetValue("Auto");
//remember the status of the SSPP service. We need to restore it later
Constants.WIDCOMMSPP= value;
//Deactivate the spp service
registryKey.SetValue("Auto", 0, RegistryValueKind.DWord);
}
catch(Exception ex)
{
stringtmp = ex.Message;
}
finally
{
if(registryKey != null)
registryKey.Close();
}
}
/// <summary>
/// Restore the Widcomm Serial Port service
/// </summary>
public static voidRestoreWidcommSPP()
{
RegistryKeyregistryKey = null;
try
{
registryKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\WIDCOMM\\BTConfig\\Services\\0001\\");
//Restore the spp service
registryKey.SetValue("Auto", Constants.WIDCOMMSPP);
}
catch(Exception ex)
{
stringtmp = ex.Message;
}
finally
{
if(registryKey != null)
registryKey.Close();
}
}