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