.NET Components for Mobility

Detecting Bluetooth

Last post 02-12-2008 9:33 AM by alanjmcf. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 02-07-2008 4:41 AM

    • Lita
    • Top 150 Contributor
    • Joined on 02-07-2008
    • Posts 4

    Detecting Bluetooth

    Hey, great site, has helped me a lot with bluetooth.

    I am currently writing a program in C# for a windows CE5.0 device to connect to a printer via bluetooth. I have it connecting fine no problem, but when the device switches off, it loses the connection. This requires me to go and switch the bluetooth off and then back on. I would like to know if there is a way I can code the detection into C# so if it's off I can switch it on?

    I googled around a bit and found BluetoothRadio would work with bthutil.dll but that dll is not supported by CE :(

    Any help would be appreciated. 

  • 02-07-2008 7:59 AM In reply to

    Re: Detecting Bluetooth

    I don't know of any other way to control the radio mode.  Maybe someone else does?

    Why do you need to disable and enable the radio?  Won't you just get an error on the existing connection, and you can redo the connection then?  Or does something odd happen on switch off/on?

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 02-07-2008 9:11 AM In reply to

    • Lita
    • Top 150 Contributor
    • Joined on 02-07-2008
    • Posts 4

    Re: Detecting Bluetooth

    I just found that manually switching on and off the bluetooth is the only way to make it reconnect.

    I can't use radio because it requires that bthutil dll to work which doesn't exist on CE. Is there a way to work that without needing the dll?

  • 02-08-2008 5:45 AM In reply to

    Re: Detecting Bluetooth

    Can you describe the scenario for me.  When your WinCE machine powers-off, after power-on the existing connection reports an error / just fails silently?  Or is it the other device (printer?) that is powering down.

    Bluetooth only can handle one connection from each client device to a particular server on the server device (unlike TCP/IP, IrDA, IPX, NetBIOS etc).  So if you've still an old RFCOMM connection, then it would need to be closed to allow a new one to connect.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 02-11-2008 4:36 AM In reply to

    • Lita
    • Top 150 Contributor
    • Joined on 02-07-2008
    • Posts 4

    Re: Detecting Bluetooth

     I would say the connection just fails silently. The printer stays on. There would only ever be the one device and one printer paired together.

     The good news is that I found a solution to this problem. 

     It requires using the Microsoft Embedded Source Tools found here.

    This prompts the header: 'using Microsoft.WindowsMobile.SharedSource.Bluetooth;' which unfortunately disallows 'using InTheHand.Net.Bluetooth;' but it allows the use of BluetoothRadio without the need for that bthutil.dll if you're using WinCE. The BluetoothRadio is all I needed to get this program working. The Microsoft.WindowsMobile library does not support the Ports and Sockets feature that InTheHand.Net does so I am using a combination of both.

    Filed under: ,
  • 02-11-2008 6:17 AM In reply to

    Re: Detecting Bluetooth

    OK, if you've got a solution you might want to leave it as is.  But, if you've still go an old BluetoothClient / NetworkStream in existence then you would need to close it before another connection can be made.  This is because there can only be one connection from your WinCE machine to the printer service.  Toggling the radio would kill any existing connection.  So, call BluetoothClient.Close and/or NetworkStream.Dispose before you attempt any new connection.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 02-11-2008 6:51 AM In reply to

    • Lita
    • Top 150 Contributor
    • Joined on 02-07-2008
    • Posts 4

    Re: Detecting Bluetooth

     I actually use BluetoothSerialPort.CreateClient, but I do close it at the end when it's done printing.

  • 02-11-2008 11:33 AM In reply to

    Re: Detecting Bluetooth

    I'm suprised this works since the Microsoft Bluetooth library uses the same P/Invoke to toggle the radio state - BthSetMode in bthutil.dll

    I'm trying to find out how this function is implemented (as it is presumably calling through to a bluetooth driver) and hopefully will find a solution to bypass this dll since it't not present on all devices.

    Peter

    Peter Foot
    Microsoft Device Application Development MVP
    www.peterfoot.net | www.inthehand.com
  • 02-12-2008 9:33 AM In reply to

    Re: Detecting Bluetooth

    OK, I see why we've be talking at cross purposes so far. :-,)   I assumed BluetoothClient.  I'm not too knowledgeable with the Bluetooth serial port workings, but would have hoped that closing the port (?SerialPort.Close) and (also?) BluetoothSerialPort.Close would clear the connection.

    If you're only using the serial port from within your application then things would (hopefully) be simpler if you used a BluetoothClient.  Closing it will reliably close the connection and hopefully you wouldn't need to reset the radio to do so.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
Page 1 of 1 (9 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.