android蓝牙SPP连接在发送所有数据之前终止

时间:2018-06-20 15:05:16

标签: android bluetooth spp bixolon-printer

这可能很微妙:

我为Android 6开发了一个应用程序,该应用程序可以在Bixolon SPP-R-200II上打印多个收据。首先,正常方式可以正常工作,连接,打印等看起来都不错。

由于测试,我遇到了一个特殊错误。有时打印会在完成之前停止。日志显示如下:

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

我发现Android 4和更高版本的BT堆栈具有BT-Module Gen 2或3的设备存在一些问题,这似乎是问题所在,因为带有BT Gen 1的旧打印机可以正常工作,其他人则占50%的时间。

有任何解决方法/修复程序吗?

1 个答案:

答案 0 :(得分:0)

好吧,这似乎是打印机的问题,这是我的实际工作解决方案:

首先,我从其SDK中反编译了Bixolon API。我检查了他们是如何实现传输的,发现在多个设备上或从Android的指定API级别起,需要以较小的包发送数据。参见下面的代码:

发件人:

    osr.write(output);

更改为:

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

请记住,osr是OutputStream ...