简体   繁体   中英

android bluetooth SPP connection terminates before sending all data

This might be delicate:

I develop an App for Android 6 which may print several receipts on an Bixolon SPP-R-200II. First, the normal way works fine, connection, printing, etc. looks good.

Due to testings I faced one special error. Occasionally the print stops before being finished. The Log shows this:

06-20 15:04:17.456 5369-5393/? W/bt_rfcomm: port_rfc_closed RFCOMM connection in state 2 closed: Peer connection failed (res: 16) 06-20 15:04:17.457 5369-5397/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 7 06-20 15:04:17.463 5369-5393/? I/bt_btm_sec: btm_sec_disconnected clearing pending flag handle:2 reason:8 06-20 15:04:17.463 5369-5393/? W/bt_l2cap: L2CA_SetDesireRole() new:x0, disallow_switch:0 L2CA_SetDesireRole() new:x0, disallow_switch:0 06-20 15:04:17.463 5369-5387/? E/BluetoothRemoteDevices: state12newState1 06-20 15:04:17.463 5369-5387/? D/BluetoothRemoteDevices: aclStateChangeCallback: sending ACL disconnected intent 06-20 15:04:17.464 5369-5387/? D/BluetoothRemoteDevices: aclStateChangeCallback: State:DisConnected to Device:74:F0:7D:E4:8E:3C 06-20 15:04:17.466 5369-5369/? D/BluetoothMapService: onReceive onReceive: android.bluetooth.device.action.ACL_DISCONNECTED 06-20 15:04:17.468 5369-5369/? V/BluetoothPbapService: action: android.bluetooth.device.action.ACL_DISCONNECTED 06-20 15:04:17.472 5369-5369/? V/BluetoothFtpService: Ftp Service onStartCommand PARSE INTENT action: android.bluetooth.device.action.ACL_DISCONNECTED 06-20 15:04:17.475 5369-5369/? D/BluetoothDunService: parseIntent: action: android.bluetooth.device.action.ACL_DISCONNECTED

As I found out there were some issues with the BT stack from Android 4 and newer with devices having a BT-Module Gen 2 or 3. This seems to be the problem here, as old printers with BT Gen 1 work fine, the others amlost for 50% of the time.

Is there any workaround/fix?

Ok, it seems to be a fault with the printers, here ist my actually working solution:

First, I decompiled the Bixolon API from their SDK. I checked how they implemented the transmission and found out, that on several devices or from a specified API level of Android it is needed to send data in smaller packages. See code below:

From:

    osr.write(output);

Changed to:

    //this is needed for API lvl > 16 due to connection losses with big data
    if (output.length > 1024) {
    for (int i = 0; i < output.length; i += 1024) {
        osr.write(output, i, i + 1024 > output.length ? output.length - i : 1024);
        try
        {
            Thread.sleep(150);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    } else {
        osr.write(output);
    }

Keep in mind, osr is the OutputStream...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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