This project has moved. For the latest updates, please go here.

Error Connecting to a WinCE Bluetooth Acceptor

Topics: Bluetooth - Microsoft
Aug 29, 2014 at 11:14 AM
Edited Aug 29, 2014 at 12:09 PM
I'm developing an application for WinCE 5 with .NETCF 3.5.
I try to establish a BlueTooth connection via BluetoothService.SerialPort and the Microsoft Stack (btdrt.dll)

On the server side (WinCE x64) I use following code:
/// <summary>
/// Start Bluetooth Listener 
/// </summary>
private void StartListener()
{
    BluetoothListener lsnr = new BluetoothListener(BluetoothRadio.PrimaryRadio.LocalAddress,
                                                   BluetoothService.SerialPort);
    lsnr.ServiceName = OurServiceName;
    lsnr.SetPin(BluetoothRadio.PrimaryRadio.LocalAddress, AuthPin);
    lsnr.Start();
    m_lsnr = lsnr;
    ThreadPool.QueueUserWorkItem(ListenerAccept_Runner, lsnr);
}

/// <summary>
/// Listenener Accept Runner
/// </summary>
/// <param name="state"></param>
private void ListenerAccept_Runner(object state)
{
    BluetoothListener lsnr = m_lsnr;
    BluetoothClient conn = lsnr.AcceptBluetoothClient();
    NetworkStream peer = conn.GetStream();
    . . .
}
On the Client side(Win7 x64) I use the following code.
BluetoothAddress addr = BluetoothAddress.Parse("0007805C2580");
Guid serviceClass = BluetoothService.SerialPort;
BluetoothEndPoint ep = new BluetoothEndPoint(addr, serviceClass);
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo device = new BluetoothDeviceInfo(addr);

Stream strm;

try
{
    bool pairing = BluetoothSecurity.PairRequest(device.DeviceAddress, "0123456789");
    client.Connect(ep);
}
...
I managed to pair the device successfully but at the execution of the line client.Connect(ep); following exception is thrown:

System.Net.Sockets.SocketException (0x80004005): Cannot send after socket shutdown. A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call.

error code: 10058

What Am I doing wrong?
Can somebody help me?
Developer
Aug 30, 2014 at 6:59 PM
Can you try pairing the two devices manually and then see if it works (remove the PairRequest call). That error has been seen from some devices when pairing is required.

https://32feet.codeplex.com/wikipage?title=Errors&referringTitle=Documentation
"to a Blackberry which requires bonding the error that occurs is 10058 WSAESHUTDOWN (“The L2CAP channel disconnected by remote peer”), rather than say 10013 WSAEACCES"
Sep 1, 2014 at 10:00 AM
Thanks for the fast reply.
I tried to pair it manually and noticed a difference in the behaviour of the Windows7.
Now the properties shows one more service (PANU)
I now have DUN, PANU and SSP(COM19).

But the problem with the exception persists: same place, same errorcode.
(Same with Service = Panu)

I noticed something else:
After I started the BT-listener on WinCE its Endpoint's Address changes to {000080000007:2}

Another thing that I noticed:
With the windows Bluetooth Device Control I can connect and ping to the WinCE via PANU but the listener(even if I configure it as BTService.Panu) doesn't pass the .AccepBTClient() phrase.

Is there something wrong with the Server implementation?
Sep 1, 2014 at 3:50 PM
Edited Sep 1, 2014 at 3:51 PM
Do I have to handle the authentication request on the server side (WinCE)?
How am I supposed to do this? The BluetoothWin32Authentication throws a PlatformNotSupportedException on creation.
Developer
Sep 2, 2014 at 7:41 AM
Get the comms to work first and worry about auth later.

Can you change that ServiceClassId you are using to a new unique value. E.g. with guidgen.exe https://32feet.codeplex.com/wikipage?title=Bluetooth%20Server-side&referringTitle=Documentation It's possible there's some conflict in using 'SerialPort'. Of course use that value in both programs.
Sep 2, 2014 at 1:38 PM
Edited Sep 2, 2014 at 5:09 PM
I did as you said.
But the error still remains the same.
(and the Listeners EndpointAddress still changes from {0007805C2580:GUID} to {000080000007:2} after lsnr.Start())

The new GUID has been listed under BTDeviceInfo.InstalledServices on the client side.
The CoD is stated as 0x9A0100 (Information | ObjectTransfer | Capturing | Network)

Is that somehow related to the problem?
Sep 5, 2014 at 3:30 PM
Edited Sep 5, 2014 at 3:49 PM
There is another Issue maybe related to this.
When I try to create a Virtual Comport using
BluetoothSerialPort.CreateServer("COM9", MyGUID);
I get an Exception with error 110 (Could not Open Port)

But when I write my own native VComPort Creator according to msdn
extern HANDLE RegisterBluetoothComPort(int index, BT_ADDR BluetoothAddress){
    int retVal = 1;
    PORTEMUPortParams pp;
    memset (&pp, 0, sizeof(pp));

    pp.flocal = TRUE;
    pp.channel = RFCOMM_CHANNEL_MULTIPLE & 0xff;
    pp.device = BluetoothAddress;
//      BluetoothService.SerialPort {00001101-0000-1000-8000-00805f9b34fb}
    pp.uuidService.Data1 = 0x00001101;
    pp.uuidService.Data2 = 0x0;
    pp.uuidService.Data3 = 0x1000;
    pp.uuidService.Data4[0] = 0x80;
    pp.uuidService.Data4[1] = 0;
    pp.uuidService.Data4[2] = 0;
    pp.uuidService.Data4[3] = 0x80;
    pp.uuidService.Data4[4] = 0x5f;
    pp.uuidService.Data4[5] = 0x9b;
    pp.uuidService.Data4[6] = 0x34;
    pp.uuidService.Data4[7] = 0xfb;
    return RegisterDevice (L"COM", index, L"btd.dll", (DWORD)&pp);
}
I get a valid handle and the corresponding COM-Port is visible and can be opened using System.IO.Ports API
I tried to download the source code of 32feet to take a look on the implementation of the createServer method, but on the svn repos many files are missing.
The ones I found by googling showed me, that you apparently do the same.

Anyway, using my implementation, neither the service nor a new comport is being seen by the Client Computer.
Oct 1, 2014 at 5:10 PM
Edited Oct 1, 2014 at 5:16 PM
Finally I found the solution to my problem.
I have been pointed into the right direction after reading this discussion

to share the solution with others, here is the working source:
/// <summary>
/// Start Bluetooth Listener 
/// </summary>
private void StartListener()
{
    // the constructor with address AND GUID seems to have caused all my problems
    BluetoothListener lsnr = new BluetoothListener(BluetoothService.SerialPort);
    lsnr.ServiceName = OurServiceName;
    lsnr.Start();
    m_lsnr = lsnr;
    ThreadPool.QueueUserWorkItem(ListenerAccept_Runner, lsnr);
}

/// <summary>
/// Listenener Accept Runner
/// </summary>
/// <param name="state"></param>
private void ListenerAccept_Runner(object state)
{
    BluetoothListener lsnr = m_lsnr;
    BluetoothClient conn = lsnr.AcceptBluetoothClient();
    NetworkStream peer = conn.GetStream();
    . . .
}
As I mentioned in an earlier post the Listeners LocalEndPoint changed to {000080000007:2} after calling its -Start() method.

Now, before I call the Start() method (after the creation of the listener with the other constructor):
  • LocalEndPoint = {000000000000:0000110100001000800000805f9b34fb}.
And after the call to Start() it changes to the desired form:
  • LocalEndPoint = {0007805C2580:2}
thanks anyway for your efforts Alan
Marked as answer by MichaelE_ on 10/1/2014 at 10:10 AM