.NET Components for Mobility

Connecting to printer

Last post 04-17-2008 11:03 AM by abaker. 11 replies.
Page 1 of 1 (12 items)
Sort Posts: Previous Next
  • 04-03-2008 6:35 AM

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Connecting to printer

    I have a VB.NET application that runs on a CE5 device and I am trying to write code to connect and print to a bluetooth printer. I am using PrinterCE, which requires me to make the connection to the printer and pass the name of the com port that I am connected on to the PrinterCE code. I am using DiscoverDevices to find the printer, then the following code to establish the connection:-

    objBluetoothEndPoint = New BluetoothEndPoint(CType(objBluetoothDevices(i).DeviceAddress, BluetoothAddress), BluetoothService.SerialPort)

    objBluetoothClient.Connect(objBluetoothEndPoint)

    objBluetoothSerialPort = BluetoothSerialPort.CreateClient(objBluetoothEndPoint)

    If I then use objBluetoothSerialPort.PortName, I am hoping that this will give me the com port that I need. It was working, but I am now finding that it is returning COM0 as the serial port, and PrinterCE requires a port in the range 1-9. Is COM0 a valid port or is it indicating that there is an error somewhere? If it is valid, how can I force it to connect on a port other than COM0? Thanks in advance for any help.

    Andy Baker

  • 04-06-2008 4:06 PM In reply to

    Re: Connecting to printer

    You a return of COM0 generally indicates failure.  We've fixed this in the repository version, see http://www.codeplex.com/32feet/WorkItem/View.aspx?WorkItemId=15124  However we're deprecating that API due to the unreliability of the underlying native API and recommending that one uses the BluetoothDevice.SetServiceState method instead.  However that's only available in the current respository code and not in any released version.

    We previously helped a user with a *very* similar requirement, and he wrote a paper on his solution.  I'll see whether I can get a reference to it for you.

    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.
  • 04-07-2008 3:48 AM In reply to

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Re: Connecting to printer

    Thanks for the reply. You are correct, COM0 does look like it was indicating a failure. I reset the device, and then it started working again, returning COM9. I have read elsewhere about the need to close the COM port after use, as I was developing there was probably an occasion where I stopped the code before doing so, so this might explain it. From this it seems that the best thing to do would be only open the COM port when I wish to print and close it as soon as printing is finished. However, I have found the only way I can get it to work is by scanning for devices each time, which is not ideal as it takes time. If I try to use objBluetoothEndPoint or objBluetoothSerialPort without doing so, I get a socket exception. Is there a simple way of just reponening the serial port?

    However, as you say if the underlying API is unreliable, perhaps I need to look at a different way altogether. I would be grateful if you could let me see that paper.

    Andy Baker

  • 04-07-2008 9:01 AM In reply to

    Re: Connecting to printer

    Hi Andy,

    I developed an applicaiton on the WM5 which allows you to bond a printer using a barcode (which was just a mac address).  I will have an article on this issue soon, however, until then I can provide you the steps I used to get this working.

    I actually hacked the API a little bit and modified the code so it consistently gave me COM8.  Once this was achieved, I used the following process:

    1. BluetoothSecurity class is used to create the bond

    2. Used the BluetoothDeviceInfo class to create the serial bond.

    3. Check the conneciton using BluetoothClient class

    4. Reset the device since this is required for the registry settings to take affect.

    This is a very simple description, obviously a lot more detail was required,  You can access the code through codeplex to customise for your needs.

    Let me know if you'd like a copy of the paper.

    Regards,

     

    Nick

  • 04-07-2008 10:12 AM In reply to

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Re: Connecting to printer

    Hi Nick

    Thanks for the advice, however I must confess as to not understanding most of it! Do you have a link to your code, I have been unable to find it? I would very much like a copy of your paper.

     Andy Baker

     

     

  • 04-08-2008 1:16 AM In reply to

    Re: Connecting to printer

    Hi  Andy,

     

    I hope that my article will be easier to understand then my article.  Do you have an email address which I can send too?

     

    Regards,

     

    Nick

  • 04-08-2008 3:30 AM In reply to

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Re: Connecting to printer

    Hi Nick

    Thanks, my email address is abaker@vanputer.com

    Regards,

    Andy

  • 04-09-2008 6:34 AM In reply to

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Re: Connecting to printer

    Hi Nick 

    Thanks for that, it does make it easier to understand, however I am still getting a few problems. As I said PrinterCE requires a serial port. I have adapted your code for storing the Bluetooth address details in the registry. Instead of scanning a barcode like you do, I am creating a BluetoothClient object and using DiscoverDevices, then when I find the printer, I write the details to the registry in the same way. I have been unable to force the connection to be on Com8 though - how do the registry settings work? My printer seems to connect on a random Com port, usually Com7 or Com9. What does the line .CreateSubKey(this.DeviceAddress.ToString("8")) actually do? (It is the "8" that is confusing me). On starting the application I check the registry, and if a printer has already been stored, I use that address first, only using DiscoverDevices if the printer cannot be found - presumably if the user has changed the printer. The other thing I am having a problem with is the BluetoothClient object? In order to use the connection, does the BluetoothClient object need to be connected? In your example, you are closing the BluetoothClient object after you have tested the printer connection, so I would say not. However, the only time I have actually got it to print is when scanning for devices then connecting and printing straight away, otherwise I get the error 'Unable to establish communication link to printer'. This happens whenever I read from the registry to get the device address, even if the BluetoothClient.Connect code works, or if I close the BluetoothClient object before printing. When trying to use BluetoothClient.Connect, I also sometimes get a SocketException 10061 error, 'No connection could be made because the target machine actively refused it'. I have not established a pattern to this however, it is probably because I am not closing something somewhere.

      I am confused about how to use BluetoothSerialPort. The code that I am using (VB.NET) is as follows:

    If (Me.ComPort Is Nothing) Then

        Dim objBluetoothSerialPort As BluetoothSerialPort = BluetoothSerialPort.CreateClient(strComPort, New BluetoothEndPoint(Me.Address, BluetoothService.SerialPort))    Me.ComPort = New System.IO.Ports.SerialPort(objBluetoothSerialPort.PortName)

       objBluetoothSerialPort.Close()

    End If

    If objBluetoothClient.Connected = False Then objBluetoothClient.Connect(New BluetoothEndPoint(Me.Address, BluetoothService.SerialPort))

    My class that handles all the communication has properties SerialPort (System.IO.Ports.SerialPort) and Address (String), amongst others. The above code certainly creates a BluetoothSerialPort on COM8 (strComPort is set to that), but I am unsure if this is the correct port that my printer is actually connecting on. I read elsewhere on the forum about the need to close the BluetoothSerialPort object as soon as it has been used, and to use the PortName property to create a System.IO.Ports.SerialPort object, so I hope I have done this correctly. Of course, if I could get the registry hack to work to make it always connect on Com8, I don't think I would have to worry about BluetoothSerialPort, if I have understood it correctly.

      Finally, I need to know how to cope with what happens if the printer is connected, but the user moves out of range and the connection is lost, then moves back into range. Is the connection automatically re-established as it would be with WiFi? If so, I could just send some Nulls to the printer and see if there is an error, if not I assume the connection is OK. If there is an error, go through the process of re-establishing the connection, or could I just use the BluetoothClient.Connected property? However, I think this is where I was getting the SocketException, so it doesn't seem as if it is straightforward.

       I get the feeling that I am nearly there, but am missing something important. Thanks for all your help.

     Andy Baker

     

  • 04-11-2008 4:53 PM In reply to

    Re: Connecting to printer

    I haven't read all the thread, but some points.

    The "8" in the ToString just selects the format.

    As I understand it, Nicholas doesn't use BluetoothSerialPort at all.  Instead, SetService is used to create the serial port configuration and, I think, a reboot is then required.

    The BluetoothClient usage is just to test the comms to the remote device.

     

     

    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.
  • 04-14-2008 6:14 AM In reply to

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Re: Connecting to printer

    Hi Alan

    Nicholas doesn't use BluetoothSerialPort. My problem is that I have code for WiFi and serial printers that passes a port name to the printing class. If I am not to change this code, which I don't really want to as it works, I have to make the Bluetooth connection and get the name of the com port which it connects on, then I can just pass this port name to my existing code. Either I can do this by forcing the Bluetooth device to always connect on the same port, as Nicholas suggested, or make the connection and then use BluetoothSerialPort to get the port name (I think). However, I am obviously doing something wrong as I cannot get either option to work at the moment. Apparently my handheld suppliers have a library for forcing the com port so if I can use that, then I can use your library for scanning etc, and hopefully get it to work that way. Thanks for your help.

    Andy Baker

     

  • 04-14-2008 8:39 PM In reply to

    Re: Connecting to printer

    Hi Andy,

    Sorry for the delayed response.  The connection I create also has a management application which I developed which checks the port before each connection.  If this does not work, it rebuilds the bluetooth connection.  From what I understand, it has to do with the synchronisation of bluetooth clock and recreating the bond allows the Bluetooth connection to be in sync again.  You'll aslo need a reset once the first connection is made, since this port is configured in the registry.  Hope this helps.

  • 04-17-2008 11:03 AM In reply to

    • abaker
    • Top 75 Contributor
    • Joined on 04-03-2008
    • Posts 7

    Re: Connecting to printer

    Hi Nick

    I have more or less got it working now. My hardware supplier sent me a simple library that allowed me to open, close and write to a bluetooth connection, which is really all I want to do. I am using the InTheHand library for scanning for available printers, then saving the bluetooth address in the registry when found. I can then use my supplier's library to open the connection and print the data. As long as I close the connection as soon as I have finished using it I have not had any problems as yet, although I could do with a way of testing whether the connection is present before I write to it. If there is a problem opening the connection, it returns an error but if it opens the connection successfully and then fails when writing, it doesn't, and just keeps trying infinitely. The only way I have managed to produce this problem is by pausing the application after opening the connection, and switching the printer off, so obviously it wouldn't happen in the real world, but it would be nice to fix it anyway. One for whoever wrote the library though. Thanks for your help / advice on this.

     Andy Baker

     

Page 1 of 1 (12 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.