如何将数据发送到蓝牙打印机奥钢联的Andr​​oid应用程序?蓝牙、发送到、如何将、应用程序

2023-09-04 11:45:25 作者:最难的其实就是活成

我开发一个应用程序,它会通过蓝牙将数据传输到打印机打印(收据一种热敏式打印机)。我已经遵循了这个环节的code的。

I am developing an app which will send data to a printer via bluetooth to print (A thermal printer for receipts). I have followed the code which in this link.

http://pastie.org/6203514 而这个链接也的 http://pastie.org/6203516

我能看到设备及其MAC地址和它的名字,当我将数据发送到打印机(光打印机的LED指示灯停止闪烁,并成为标准,即打印机正在与我的Andr​​oid手机连接),但是当我送它不是打印和NOR它给任何错误也是数据。我用Google搜索了很多,我发现很多codeS和尝试了所有集codeS,但可能无法打印。

I am able to see the device and its MAC Address and its name, when I send the data to the printer (the light LED on the printer stops blink and becomes standard i.e the printer is connected with my android phone) but when I send the data it is not printing and nor it is giving any error also. I have googled a lot and I found many codes and tried all set of codes but could not able to print .

请任何人能帮助我离开这里。我听说有意图它可以很容易做到的,但无法得到与意图的精确解。

Please any one could help me out of here. I heard with Intents it can be done easily but could not get the exact solution with Intents.

任何帮助将是AP preciated 在此先感谢

Any help would be appreciated Thanks in Advance

Ganesh神

推荐答案

最后,我用我自己解决了这个问题,问题是,这我发送到打印机头字节是真正的罪魁祸首。其实我送170,1(其中170是打印机必须接收,第二个字节是打印机的ID我的意思是这两个值由打印机控制卡设计给一些COM端口的第一个字节)。实际我要送170,2其中2为打印机ID,以便它给出了正确的打印和每一个打印机,通常根据他们的控制卡的发送数据。

Finally I solved this problem by my self and the problem is that the header byte which I am sending to the printer is the real culprit. Actually I am sending 170,1 (where 170 is the first byte that the printer must receive and the second byte is the printer id I mean some com port which these two values are given by Printer control card designer). Actual I have to send 170,2 where 2 is the Printer Id so that it gives the correct print and for every printer it is common of sending the data based on their control card's.

感谢了很多朋友,这是我的code u可以使用这些code为所有类型的打印机(适用于POS,热敏打印机)

Thanks a lot friends and here is my code that u can use these code for all types of printers(for POS-Thermal Printers)

public void IntentPrint(String txtvalue)
{
    byte[] buffer = txtvalue.getBytes();
    byte[] PrintHeader = { (byte) 0xAA, 0x55,2,0 };
    PrintHeader[3]=(byte) buffer.length;
    InitPrinter();
    if(PrintHeader.length>128)
    {
        value+="\nValue is more than 128 size\n";
        txtLogin.setText(value);
    }
    else
    {
        try
        {
            for(int i=0;i<=PrintHeader.length-1;i++)
            {
                mmOutputStream.write(PrintHeader[i]);
            }
            for(int i=0;i<=buffer.length-1;i++)
            {
                mmOutputStream.write(buffer[i]);
            }
            mmOutputStream.close();
            mmSocket.close();
        }
        catch(Exception ex)
        {
            value+=ex.toString()+ "\n" +"Excep IntentPrint \n";
            txtLogin.setText(value);
        }
    }
} 

这code为休息:

And this code for the rest:

public void InitPrinter()
{
    try
    {
        if(!bluetoothAdapter.isEnabled())
        {
           Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
           startActivityForResult(enableBluetooth, 0);
        }

        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

        if(pairedDevices.size() > 0)
        {
            for(BluetoothDevice device : pairedDevices)
            {
                if(device.getName().equals("Your Device Name")) //Note, you will need to change this to match the name of your device
                {
                    mmDevice = device;
                    break;
                }
            }

            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
            //Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
            //mmSocket = (BluetoothSocket) m.invoke(mmDevice, uuid);
            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
            bluetoothAdapter.cancelDiscovery();
            if(mmDevice.getBondState()==2)
            {
                mmSocket.connect();
                mmOutputStream = mmSocket.getOutputStream();
            }
            else
            {
                value+="Device not connected";
                txtLogin.setText(value);
            }
        }
        else
        {
            value+="No Devices found";
            txtLogin.setText(value);
            return;
        }
    }
    catch(Exception ex)
    {
        value+=ex.toString()+ "\n" +" InitPrinter \n";
        txtLogin.setText(value);
    }
}