简体   繁体   English

Xamarin中的蓝牙打印

[英]Bluetooth printing in Xamarin

I've a Bluetooth thermal printer and i am trying to send a print command using Xamarin.我有一台蓝牙热敏打印机,我正在尝试使用 Xamarin 发送打印命令。

I've tried the following code我试过下面的代码

  BluetoothSocket socket = null;
        BufferedReader inReader = null;
        BufferedWriter outReader = null;


        BluetoothDevice device = (from bd in BluetoothAdapter.DefaultAdapter?.BondedDevices
                                  where bd?.Name == deviceName
                                  select bd).FirstOrDefault();
        //BluetoothDevice hxm = BluetoothAdapter.DefaultAdapter.GetRemoteDevice (bt_printer);
        UUID applicationUUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");


        socket = device.CreateRfcommSocketToServiceRecord(applicationUUID);
        socket.Connect();
        inReader = new BufferedReader(new InputStreamReader(socket.InputStream));
        outReader = new BufferedWriter(new OutputStreamWriter(socket.OutputStream));
        outReader.Write("hhhh");


        outReader.Flush();
        Thread.Sleep(5 * 1000);
        var s = inReader.Ready();
        inReader.Skip(0);
        //close all
        inReader.Close();
        socket.Close();
        outReader.Close();

The screen on the printer shows 'Working' and then back to ready and nothing gets printed out.打印机上的屏幕显示“正在工作”,然后恢复就绪,没有任何内容被打印出来。 As you see i am trying to print the text 'hhhh' do i have to append anything extra for the message.如您所见,我正在尝试打印文本“hhhh”,我是否必须为 append 提供任何额外的信息。 The printer is an RD-G80 Radall thermal printer.打印机是 RD-G80 Radall 热敏打印机。

Hope you can help I've been trying for a week now.希望你能帮助我已经尝试了一个星期。

Thanks谢谢

I used something like the code below to send the cod to a Zebra Mobile Printer on a application that i worked some time ago.我使用类似下面的代码将 cod 发送到我前一段时间工作的应用程序上的 Zebra 移动打印机。 Its important to point that u have to know the right code pattern to send to the printer .重要的是要指出您必须知道要发送到打印机的正确代码模式 Usually they have a documentation about it.通常他们有关于它的文档。 In the> application, i send the product code to our backend-end and it retrieves and build the code pattern that i needed to send from my mobile application to the printer.在 > 应用程序中,我将产品代码发送到我们的后端,它会检索并构建我需要从移动应用程序发送到打印机的代码模式。

Code below.代码如下。 hope this helps you:希望这可以帮助你:

** **

private enum msgRet
        {
            conectionERROR,
            sendERROR,
            sucess
        }
    public int Print(string codToSend, string Print)
        {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
            BluetoothSocket btSocket = null;
            UUID MY_UUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
            BluetoothDevice device = null;
            Stream outStream = null;
            int ret = -1;
            try
            {    
                MessagingCenter.Send(this, "Print");

                var MacAdress = mBluetoothAdapter.BondedDevices.ToList().Find(x => x.Name == Print).Address;

                if(string.IsNullOrEmpty(MacAdress)) 
                 return ret = (int)msgRet.sendERROR;

                if (!mBluetoothAdapter.IsEnabled)
                {
                    BluetoothAdapter mBluetoothAdapter = 
 BluetoothAdapter.DefaultAdapter;
                  if(!mBluetoothAdapter.IsEnabled)
                  {
                    mBluetoothAdapter.Enable();
                  }
                }

                device = mBluetoothAdapter.GetRemoteDevice(MacAdress);
                btSocket = device.CreateInsecureRfcommSocketToServiceRecord(MY_UUID);

                if (!btSocket.IsConnected)
                {
                    btSocket.Connect();
                }
                if (btSocket.IsConnected)
                {
                    Thread.Sleep(200);
                    try
                    {                  
                        byte[] msgBuffer = Encoding.ASCII.GetBytes(codToSend);
                        outStream = btSocket.OutputStream;
                        outStream.Write(msgBuffer, 0, msgBuffer.Length);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("sendERROR" + e.Message);
                        ret = (int)msgRet.sendERROR;
                    }
                }

            } catch (Exception ex) {
                Debug.WriteLine("conectionERROR:" + ex.Message);
                ret = (int)msgRet.conectionERROR;
            }
            finally
            {
                if (outStream != null) { outStream.Close(); outStream.Dispose();}
                mBluetoothAdapter.Dispose();
                if (btSocket!=null) { btSocket.Close(); btSocket.Dispose(); }           
            }

            if (ret == -1) { ret = (int)msgRet.sucess; }

            return ret;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM