Can't pair to Bluetooth Printer Zebra MZ 320

Topics: Bluetooth - Microsoft
May 11, 2012 at 12:53 PM
Edited May 11, 2012 at 1:00 PM

Hello,

i want to pair between a Zebra MZ 320 Bluetooth Printer and a WM 6.5 Device.

The WM 6.5 Device uses the Windows Bluetooth Stack.

At first i tried to use the InTheHand.Net.Personal.dll  Version 3.4.0429.0 but it fails with false at this Call

BluetoothSecurity.PairRequest(btDevice.DeviceAddress, "")


For a better debugging i added the InTheHand.Net.Personal.CF2 and InTheHand.Net.Personal (Version 3.4.0429.0) Projects to my Solution.

I found ouf that in Class InTheHand.Net.Bluetooth.Msft.WindowsBluetoothSecurity the Method-Call 

int result = NativeMethods.BthPairRequest(device.ToByteArray(), len, pinbytes);

returns the result 87.

 

What does this means ?

Thx for your support

May 14, 2012 at 2:34 PM

Is this the Reason why it does not work ?

https://km.zebra.com/kb/index?page=answeropen&type=open&searchid=1336682809706&answerid=16777216&iqaction=5&url=https%3A%2F%2Fkm.zebra.com%2Fkb%2Findex%3Fpage%3Dcontent%26id%3DSO8031%26actp%3Dsearch%26viewlocale%3Den_US&highlightinfo=6292341,26,43#

http://stackoverflow.com/questions/10540482/c-sharp-windows-mobile-pairing-with-zebra-rw-420

 

I have tried to connect without pairing. This works for me...

http://32feet.codeplex.com/wikipage?title=General%20Bluetooth%20Data%20Connections 

 

...but when i try to send/write my CPCL-Command Bytes to the Printer it does "print" some empty rows.

Does anybody knows why ?

Thx for any support

Developer
May 15, 2012 at 3:30 PM

Error code 87 is ERROR_INVALID_PARAMETER The BthPairRequest page (http://msdn.microsoft.com/en-us/library/ee495963.aspx) suggests that that occur when "The PIN length is incorrect."

As to you 'blank' printing.  Does CPCL need some special commands to set some mode or something.  Does it need the job to end with FF maybe?

If you're want to set-up a virtual COM port as asked at stackoverflow, try using class BluetoothSerialPort or BluetoothDeviceInfo.SetServiceState. See http://32feet.codeplex.com/wikipage?title=Bluetooth%20Serial%20Ports

May 16, 2012 at 1:08 PM

Tronix, I am having the same issues. I have to wait for a charger before I can continue work, but I've sent a few emails with Zebra. If the device has authentication turned off, try turning it on and setting a pin on it. Or if you can set a pin without turning on authentication, try that as well. Hope it works. (I'm actually the one that posted the stack overflow question.)

May 17, 2012 at 11:43 AM
Edited May 17, 2012 at 11:44 AM

thank you both for your reply,

actually i found a other way without pairing, that works for me.

 

I just connect to the zebra printer by using BluetoothClient.Connect  (http://32feet.net/library/html/M_InTheHand_Net_Sockets_BluetoothClient_Connect_1.htm)

The BluetoothEndpoint does not assigned to a specific com-port (http://inthehand.com/library/html/M_InTheHand_Net_BluetoothEndPoint__ctor.htm)

To send/write some data (incl CPCL Commands) i use the BluetoothClient.GetStream  (http://32feet.net/library/html/M_InTheHand_Net_Sockets_BluetoothClient_GetStream.htm)

and write to the datastream.  

I do not use the Zebra SDK to create the CPCL Commands (e.g. https://km.zebra.com/kb/index?page=content&id=SA277&actp=LIST),

i just build them by using StringBuilder and convert with http://msdn.microsoft.com/en-us/library/system.text.unicodeencoding.getbytes

My App simple stores the selected Printer-Address in the registry to remember it for the next time.

 

HTH someone

Thx and best regards

May 24, 2012 at 9:29 AM

Hello,

I'm trying to develop a similar project (using Motorola ES400 running WM6.5 to print to Zebra MZ320).

I've tried several approaches but still with no success.

Can someone provide some code sample for this ?

Thanks in advance for any help!

All the best!

May 24, 2012 at 1:13 PM
Edited May 24, 2012 at 1:14 PM

Hello Pico,

this works for me.

 

    BluetoothClient _btClient = null;

    void ActivateBT()
    {
      BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
      System.Threading.Thread.Sleep(1000);
      _btClient = new BluetoothClient();
    }


void SearchBTDevices()
    {
      Cursor.Current = Cursors.WaitCursor;

      try
      {
        //search
        BluetoothDeviceInfo[] btDevices = _btClient.DiscoverDevices(1, true, true, true);

        foreach (BluetoothDeviceInfo btDevice in btDevices)
        {
          ConnectDevice(btDevice.DeviceAddress);
        }
      }
      catch (Exception)
      {
        throw;
      }
      finally
      {
        Cursor.Current = Cursors.Default;
      }
    }


void ConnectDevice(InTheHand.Net.BluetoothAddress address)
    {
      _btClient.Connect(new InTheHand.Net.BluetoothEndPoint(address, BluetoothService.SerialPort));
      //BTServices.Instance.GetServiceValue("RFCommProtocol")));

      if (_btClient.Connected)
      {
        string cpclCommand = "! 0 200 200 40 1\r\n" +
                             "IN-MILLIMETERS\r\n" +
                             "LABEL\r\n" +
                             "CONTRAST 0\r\n" +
                             "TONE -20\r\n" +
                             "SPEED 0\r\n" +
                             ";// PAGE 0000000003800240\r\n" +
                             "IN-DOTS\r\n" +
                             "L 0 95 530 95 1\r\n" +
                             "IN-MILLIMETERS\r\n" +
                             "T 15 0 0 13 * * *   ZEBRA Printer Test   * * *\r\n" +
                             "IN-DOTS\r\n" +
                             "L 0 255 530 255 1\r\n" +
                             "FORM\r\n" +
                             "PRINT\r\n";

        byte[] printCommandBytes = System.Text.Encoding.Default.GetBytes(cpclCommand.ToString());

        using (System.Net.Sockets.NetworkStream stream = _btClient.GetStream())
        {
          stream.Write(printCommandBytes, 0, printCommandBytes.Length);

          Application.DoEvents();

          _btClient.Dispose();
        }
      }
    }

 

You just have to modify this solution by storing the DeviceAddress to the Registry and use them like that for the next time:

 

ConnectDevice(InTheHand.Net.BluetoothAddress.Parse(myStoredDeviceAddressString));

 

HTH

May 25, 2012 at 1:35 AM

Hi Tronix,

Many many thanks! It worked great!

In any case I also share my code, based on yours, with some minor changes.

private void btPrint_Click(object sender, EventArgs e)
        {            
            // Activate BT
            BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
            System.Threading.Thread.Sleep(1000);
            // Connect  
            BluetoothAddress btAddress;
            btAddress = BluetoothAddress.Parse("0022583165F7");            
            BluetoothClient btClient = new BluetoothClient();
            try
            {
                btClient.Connect(new BluetoothEndPoint(btAddress, BluetoothService.SerialPort));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            // Send data
            string CPCLStr1 =
                "! 0 200 200 210 1" + Environment.NewLine +
                "ML 25" + Environment.NewLine +
                "TEXT 7 0 10 20" + Environment.NewLine +
                "Just" + Environment.NewLine +
                "Testing" + Environment.NewLine +
                "ENDML" + Environment.NewLine +
                "FORM" + Environment.NewLine +
                "PRINT" + Environment.NewLine;

            // Convert CPCL String to byte array
            byte[] CPCLbytes1 = ASCIIEncoding.ASCII.GetBytes(CPCLStr1);

            NetworkStream ns = btClient.GetStream();
            ns.Write(CPCLbytes1, 0, CPCLbytes1.Length);

            btClient.Close();
        }

 

Dec 12, 2012 at 9:14 AM

hey guys about the above code where do i put it?

i mean how do i execute it/ and where?

Jun 3, 2014 at 4:40 AM
Hello Pico,

Above print code is working fine for me at once but when I print the document again then I need to restart the printer then after it will print.

So is there any solution for this to avoid restart printer??