15

ブルートゥース経由でプリンターにデータを送信して印刷するアプリを開発しています(領収書用のサーマルプリンター)。このリンクのコードに従いました。

http://pastie.org/6203514およびこのリンクもhttp://pastie.org/6203516

データをプリンターに送信すると、デバイスとそのMACアドレスとその名前を確認できます(プリンターのライトLEDが点滅を停止し、標準になります。つまり、プリンターはAndroidフォンに接続されています)。データは印刷されておらず、エラーも発生していません。私はたくさんグーグルで検索しましたが、多くのコードを見つけてすべてのコードセットを試しましたが、印刷できませんでした.

誰かが私をここから助けてください。Intents を使えば簡単にできると聞きましたが、Intents では正確な解決策を得ることができませんでした。

事前に感謝します

ガネーシャ

4

2 に答える 2

10

最後に、私はこの問題を自分で解決しました。問題は、プリンターに送信しているヘッダー バイトが本当の原因であるということです。実際には 170,1 を送信しています (ここで、170 はプリンターが受信する必要のある最初のバイトで、2 番目のバイトはプリンター ID です。これらの 2 つの値は、プリンター コントロール カードの設計者によって与えられる com ポートを意味します)。実際には、170,2 を送信する必要があります。ここで、2 はプリンター ID であり、正しい印刷が行われるようにします。すべてのプリンターで、コントロール カードに基づいてデータを送信するのが一般的です。

どうもありがとう

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);
        }
    }
} 

残りのコードは次のとおりです。

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);
    }
}
于 2013-02-22T16:18:57.240 に答える
1

印刷の特定のプロトコルを目指していますか? (特定のプリンター用?)

そうでない場合は、接続されているプリンターに汎用印刷を行うことができます。次のコード スニペットを使用できます。

特定のファイルを印刷したい場所にこれを書きます:

            Intent intent = Tools.getSendToPrinterIntent(
                    DisplayActivity.this, mPdfAsPictures,
                    mPrintCurrentIndex);

            // notify the activity on return (will need to ask the user for
            // approvel)
            startActivityForResult(intent, ACTIVITY_PRINT);

これはヘルパー メソッドです。

public static Intent getSendToPrinterIntent(Context ctx, String[] fileFullPaths, int indexToPrint){
    Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);

    // This type means we can send either JPEG, or PNG
    sendIntent.setType("image/*");

    ArrayList<Uri> uris = new ArrayList<Uri>();

    File fileIn = new File(fileFullPaths[indexToPrint]);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);

    sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

    return sendIntent;
}

最後に、次の場所で回答を受け取ります。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ACTIVITY_PRINT) {

        switch (resultCode) {
        case Activity.RESULT_CANCELED:
            Log.d(TAG(), "onActivityResult, resultCode = CANCELED");
            break;
        case Activity.RESULT_FIRST_USER:
            Log.d(TAG(), "onActivityResult, resultCode = FIRST_USER");
            break;
        case Activity.RESULT_OK:
            Log.d(TAG(), "onActivityResult, resultCode = OK");
            break;
        }
    }
};

幸運を!

于 2013-02-10T09:56:01.403 に答える