繁体   English   中英

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

[英]android bluetooth SPP connection terminates before sending all data

这可能很微妙:

我为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%的时间。

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

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

首先,我从他们的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 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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