1

USBプリンターに接続してそこからデータを印刷しようとしています。これを行うために以下のコードを使用しています。次のことが起こっています。

  • 電話からアプリケーションを起動します - アプリが開き、「印刷」というラベルの付いたボタンが表示された画面が表示されます。

  • OTG ケーブル経由でプリンターの USB を接続すると、許可のダイアログが表示され、[許可を許可] を選択します。

  • 「印刷」ボタンを押すと、コードに配置されたトーストが表示されます

    1>「デバイス数:1」

    2>「デバイスが接続されました」

    3> "printdata.getBytes().length ====>"

  • プリンターは「データを受信して​​います」と表示されますが、その後何もしません。すべてが正常に機能していますが、問題は何も印刷されません

    public class USBAdapter {
    
    private UsbManager mUsbManager;
    private UsbDevice mDevice;
    UsbDevice printer = null;
    private PendingIntent mPermissionIntent;
    UsbDeviceConnection connection;
    String TAG = "USB";
    private static final String ACTION_USB_PERMISSION =  "com.android_usb_printer.USB_PERMISSION";
    
    
    public USBAdapter() {
    }
    
    public void createConn(Context context) {
        mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        if (deviceList.size() <= 0) {
            Log.i("Info", "No device found");
            Toast.makeText(context, "No device found", Toast.LENGTH_SHORT).show();
        } else {
            Log.i("Info", "Number of device : " + deviceList.size());
            Toast.makeText(context, "Number of device : " + deviceList.size(), Toast.LENGTH_SHORT).show();
    
        }
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    
        mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
        while (deviceIterator.hasNext()) {
            mDevice = deviceIterator.next();
            Log.i("info", "Vendor id : " + mDevice.getVendorId());
            Log.i("info", "Product id : " + mDevice.getProductId());
            Log.i("info", "Device  name : " + mDevice.getDeviceName());
            Log.i("info", "Device class : " + mDevice.getClass().getName());
            Log.i("info", "Device protocol: " + mDevice.getDeviceProtocol());
            Log.i("info", "Device subclass : " + mDevice.getDeviceSubclass());
    
        }
        /*final String ACTION_USB_PERMISSION = "com.android_usb_printer.USB_PERMISSION";
        mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        context.registerReceiver(mUsbReceiver, filter);*/
    }
    
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    
        public void onReceive(Context context, Intent intent) {
            System.out.println("<=======Onreceived=======>");
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    Toast.makeText(context, "mDevice ====>"+mDevice, Toast.LENGTH_SHORT).show();
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (mDevice != null) {
                            //call method to set up device communication
                        }
                    } else {
                        Log.d(TAG, "permission denied for device " + mDevice);
                    }
                }
            }
        }
    };
    
    @SuppressLint("NewApi")
    public void printMessage(final Context context, String msg) {
        // TODO Auto-generated method stub
        final String printdata = msg;
        final UsbEndpoint mEndpointBulkOut;
        if (mUsbManager.hasPermission(mDevice)) {
            UsbInterface intf ;
            for(int i = 0; i < mDevice.getInterfaceCount(); i++){
                intf = mDevice.getInterface(i);
                if(intf.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER){
                    printer = mDevice;
    
                    UsbEndpoint ep = intf.getEndpoint(i);
                    int b;
                    if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                        if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
                            mEndpointBulkOut = ep;
                            connection = mUsbManager.openDevice(printer);
                            if (connection != null) {
                                Log.e("Connection:", " connected");
                                Toast.makeText(context, "Device connected", Toast.LENGTH_SHORT).show();
                            }
    
                            Toast.makeText(context, "printdata.getBytes().length ====>"+printdata.getBytes().length, Toast.LENGTH_SHORT).show();
    
                            boolean forceClaim = true;
                            connection.claimInterface(intf, forceClaim);
                            //Integer res = connection.bulkTransfer(mEndpointBulkOut, printdata.getBytes(), printdata.getBytes().length, 10000);
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    Log.i("Thread:", "in run thread");
                                    byte[] bytes = printdata.getBytes();
                                    int b = connection.bulkTransfer(mEndpointBulkOut, bytes, bytes.length, 100000);
                                    Log.i("Return Status", "b-->" + b);
                                }
                            }).start();
                            connection.releaseInterface(intf);
                            break;
                        }
                    }
                }
            }
        } else {
            mUsbManager.requestPermission(mDevice, mPermissionIntent);
            Toast.makeText(context, "Device have no permission", Toast.LENGTH_SHORT).show();
        }
    }
    
    @SuppressLint("NewApi")
    public void closeConnection(Context context) {
        BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
    
                if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (device != null) {
                        Toast.makeText(context, "Device closed", Toast.LENGTH_SHORT).show();
                        connection.close();
                    }
                }
            }
        };
    }
    }
    

printMessage()私が間違っているMainActivityonclick()ポイントの印刷ボタンから呼び出されています。ありがとう。

4

0 に答える 0