Android 应用程序会自动断开蓝牙连接

Posted

技术标签:

【中文标题】Android 应用程序会自动断开蓝牙连接【英文标题】:Bluetooth Connection drops automatically with Android Application 【发布时间】:2019-09-11 15:05:04 【问题描述】:

我正在创建一个应用程序,它通过 android 应用程序以编程方式连接 BLE 设备。这是我的连接/断开代码

当用户点击连接按钮时

new Thread(new Runnable() 
            @Override
            public void run() 
                mConnecting = true;
                mConnectException = null;
                mConnectWait.close(); // Reset the condition.
                if (mConnectedGatt != null) 
                    // Reconnect to the BLE DEX adapter.
                    Logger.d(LOG_TAG, "going to connect");
                    mConnectedGatt.connect();
                    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
                    device.connectGatt(mContext, false, mGattCallback);
                 else 
                    // Start scanning BLE devices.
                    Logger.d(LOG_TAG, "going to scan LE Devices");
                    scanLeDevice(true);
                
                mConnectWait.block(); // Wait for connect to complete
                try 
                    if (mConnected) 
                        connectCallback.onConnectSuccess();

                     else  // Error occurred in the connecting process
                        if (null == mConnectException) 
                            mConnectException = new BleDexException(
                                    "Failed to connect to the BLE DEX adapter",
                                    BleDexException.ERROR_BLE_CONNECT_FAILED);
                        
                        connectCallback.onConnectFailed(mConnectException);
                    
                 catch (Exception e) 
                    e.printStackTrace();
                

                mConnecting = false;
            
        ).start();

扫码:

if (!mBleScanning) 
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() 
                @Override
                public void run() 
                    mBleScanning = false;
                    if ((null != mBleScanner) && (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON)) 
                        mBleScanner.stopScan(mScanCallback);
                        if (mConnecting) 
                            Logger.e(LOG_TAG, "Timed out in scanning BLE devices");
                            mConnectException = new BleDexException(
                                    "Timed out in scanning BLE devices",
                                    BleDexException.ERROR_BLE_CONNECT_FAILED);
                            mConnectWait.open();
                        
                    
                
            , mBleScanPeriod);

            mBleScanning = true;
            //ScanFilter scanFilter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(DEX_SERVICE_SPP)).build();
            ScanFilter scanFilter = new ScanFilter.Builder().setDeviceName("DEXAdapter").build();
            java.util.ArrayList<ScanFilter> scanFilterList = new java.util.ArrayList<ScanFilter>();
            scanFilterList.add(scanFilter);
            ScanSettings scanSettings = new ScanSettings.Builder()
                    .setScanMode(ScanSettings.SCAN_MODE_BALANCED)
                    .build();
            if((null != mBleScanner) && (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON))
                mBleScanner.startScan(scanFilterList, scanSettings, mScanCallback);
        
     else 
        Logger.d(LOG_TAG, "stop scanning. what 's the scan flag is: " + mBleScanning);
        if (mBleScanning) 
            mBleScanning = false;
            if ((null != mBleScanner) && (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON))
                mBleScanner.stopScan(mScanCallback);
        
    

这是用于配对意图的广播接收器

public void onReceive(Context context, Intent intent) 
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) 
            if (mBtPin == null || mBtMacAddress == null) 
                return;
            
            // Programmatically set the Bluetooth PIN.
            device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            pairingRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
                    BluetoothDevice.ERROR);
            Logger.d(LOG_TAG, "Received pairing request intent, Device mac address is: " + mBtMacAddress +
                    ". Device pairingrequest type is: " + pairingRequestType);
            if (device.getAddress().equals(mBtMacAddress)) 
                try 
                    int btpin = Integer.parseInt(mBtPin);
                    Logger.d(LOG_TAG, "Set pin to BT = " + btpin);
                    byte[] pinbytes;
                    pinbytes = ("" + btpin).getBytes("UTF-8");
                    device.setPin(pinbytes);
                    abortBroadcast();
                 catch (Exception e) 
                    e.printStackTrace();
                
            
         else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) 
            int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
            switch (state) 
                case BluetoothDevice.BOND_NONE:
                    Logger.d(LOG_TAG, "Remote device is not bonded. Device: ");
                case BluetoothDevice.BOND_BONDING:
                    Logger.d(LOG_TAG, "Remote device is in bonding process");
                    break;
                case BluetoothDevice.BOND_BONDED:
                    Logger.d(LOG_TAG, "Remote device is paired");
                    BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress);
                    mConnectedGatt = bluetoothDevice.connectGatt(mContext, false, mGattCallback);
                    if (mConnectedGatt == null) 
                        Logger.e(LOG_TAG, "Failed to connect to GATT server");
                        if (mConnecting) 
                            mConnectException = new BleDexException(
                                    "Failed to connect to GATT server",
                                    BleDexException.ERROR_BLE_CONNECT_FAILED);
                            mConnectWait.open();
                        
                    
                    //scanLeDevice(false);
                    break;
            
        
    
;

会发生什么,只要我按下连接按钮,它就会检查设备是否已连接?如果没有,它将开始蓝牙 LE 扫描。我添加了用于扫描设备的 ScanFilter,以便它扫描受限设备。

一旦应用程序接收到扫描回调,它就会使用 createbond() 方法对设备进行配对。一旦完成并配对完成,它将尝试连接蓝牙 gatt。

所有这一切都在第一次尝试时成功。但一段时间后,应用程序会自动断开蓝牙设备的连接。

这是自动断线的日志

07-05 15:15:26.101  6698  6698 D BleDexToolkitSample: Invoice send transaction initiated and is in progress.
07-05 15:15:26.103  6698  6765 D BleDexToolkitSample: SetConfigTransmissionControlNumber Success
07-05 15:15:26.103  6698  6765 D BleDexToolkitSample: SetConfigTestIndicator Success
07-05 15:15:26.103  6698  6765 D BleDexToolkitSample: SetRetailer Success
07-05 15:15:26.104  6698  6765 D BleDexToolkitSample: SetSupplier Success
07-05 15:15:26.112  6698  6765 D BleDexToolkitSample: BeginTransactionSet Success
07-05 15:15:26.115  6698  6765 D BleDexToolkitSample: WriteSTSegment Success
07-05 15:15:26.116  6698  6765 D BleDexToolkitSample: WriteG82Segment Success
07-05 15:15:26.116  6698  6765 D BleDexToolkitSample: WriteLoopStart Success
07-05 15:15:26.117  6698  6765 D BleDexToolkitSample: WriteG83Segment Success
07-05 15:15:26.117  6698  6765 D BleDexToolkitSample: WriteG83Segment Success
07-05 15:15:26.118  6698  6765 D BleDexToolkitSample: WriteG22Segment Success
07-05 15:15:26.118  6698  6765 D BleDexToolkitSample: WriteLoopEnd Success
07-05 15:15:26.118  6698  6765 D BleDexToolkitSample: WriteG84Segment Success
07-05 15:15:26.118  6698  6765 D BleDexToolkitSample: WriteG86Segment Success
07-05 15:15:26.118  6698  6765 D BleDexToolkitSample: WriteG85Segment Success
07-05 15:15:26.119  6698  6765 D BleDexToolkitSample: WriteSESegment Success
07-05 15:15:26.119  6698  6765 D BleDexToolkitSample: EndTransactionSet Success
07-05 15:15:26.136  6698  6723 D BleDexDevice: Initiated sending data: 05 
07-05 15:15:27.168  6698  6723 D BleDexDevice: Initiated sending data: 05 
07-05 15:15:28.174  1934  2382 W bt_btif : bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0008
07-05 15:15:28.175  1934  2382 W bt_btif : bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0008
07-05 15:15:28.175  1934  2382 W bt_btif : bta_gattc_conn_cback() - cif=6 connected=0 conn_id=6 reason=0x0008
07-05 15:15:28.176  1934  2049 D BtGatt.GattService: onConnected() connId=5, address=00:10:20:8E:26:97, connected=false
07-05 15:15:28.176  1934  2382 W bt_btif : bta_gattc_conn_cback() - cif=8 connected=0 conn_id=8 reason=0x0008
07-05 15:15:28.176  1934  2049 D BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=00:10:20:8E:26:97
07-05 15:15:28.180  1934  2049 D BtGatt.GattService: onDisconnected() - clientIf=8, connId=8, address=00:10:20:8E:26:97
07-05 15:15:28.181  6698  6711 D BluetoothGatt: onClientConnectionState() - status=8 clientIf=8 device=00:10:20:8E:26:97
07-05 15:15:28.181  6698  6711 E BleDexDevice: onConnectionStateChange failure status=8 newState=0
07-05 15:15:28.183  1934  2049 E BluetoothRemoteDevices: state12newState1
07-05 15:15:28.183  1934  2049 D BluetoothRemoteDevices: aclStateChangeCallback: State:DisConnected to Device:00:10:20:8E:26:97
07-05 15:15:28.189  6698  6698 D BleDexToolkitSample: BLE DEX Adapter disconnected.
07-05 15:15:28.193  1934  1934 D AvrcpBipRsp: onReceive: android.bluetooth.device.action.ACL_DISCONNECTED
07-05 15:15:28.193  1934  1934 D BluetoothMapService: onReceive
07-05 15:15:28.193  1934  1934 D BluetoothMapService: onReceive: android.bluetooth.device.action.ACL_DISCONNECTED
07-05 15:15:28.194  1934  1934 E BluetoothMapService: Unexpected error!
07-05 15:15:28.194  1934  1934 D BluetoothPbapReceiver: PbapReceiver onReceive action = android.bluetooth.device.action.ACL_DISCONNECTED
07-05 15:15:28.195  1934  1934 D BluetoothPbapReceiver: Calling start service with action = null
07-05 15:15:28.199  1934  1934 I BluetoothPbapReceiver: Exit - onReceive for intent:android.bluetooth.device.action.ACL_DISCONNECTED
07-05 15:15:28.199  1934  1934 D BluetoothPbapService: Enter - onStartCommand for service PBAP
07-05 15:15:28.200  1934  1934 D BluetoothPbapService: action: android.bluetooth.device.action.ACL_DISCONNECTED
07-05 15:15:28.200  1934  1934 D BluetoothPbapService: Exit - onStartCommand for service PBAP
07-05 15:15:28.203  1934  1952 E BtGatt.GattService: writeCharacteristic() - No connection for 00:10:20:8E:26:97...
07-05 15:15:28.203  6698  6723 D BleDexDevice: Initiated sending data: 05 
07-05 15:15:28.219  1934  4912 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@b82a9b1
07-05 15:15:28.219  4939  4939 I BTLEASH : BTReceive action : android.bluetooth.device.action.ACL_DISCONNECTED :: Device : DEXAdapter
07-05 15:15:28.233  1934  1934 V BluetoothFtpService: PARSE INTENT action: android.bluetooth.device.action.ACL_DISCONNECTED
07-05 15:15:28.239  1934  1934 D BluetoothDunService: parseIntent: action: android.bluetooth.device.action.ACL_DISCONNECTED

现在我稍微改变了应用程序的行为,而不是配对和连接设备,我直接连接设备而不配对。通过这种更改,我看到蓝牙连接在连接 30 秒后自动断开。以下是日志:

=Bluetooth Connected successfully logs =
07-10 13:27:00.554  5001  5001 D BleDexToolkitSample: Connecting to BLE DEX Adapter.
07-10 13:27:00.556  5001  5338 D BleDexDevice: device found, device address is: 00:10:20:8E:26:97
07-10 13:27:00.557  5001  5338 D BluetoothGatt: connect() - device: 00:10:20:8E:26:97, auto: false
07-10 13:27:00.558  5001  5338 D BluetoothGatt: registerApp()
07-10 13:27:00.558  5001  5338 D BluetoothGatt: registerApp() - UUID=9ed830a0-85b0-4192-9b4d-c9960f5bca84
07-10 13:27:00.560  1957  2829 D BtGatt.GattService: registerClient() - UUID=9ed830a0-85b0-4192-9b4d-c9960f5bca84
07-10 13:27:00.562  1957  2068 D BtGatt.GattService: onClientRegistered() - UUID=9ed830a0-85b0-4192-9b4d-c9960f5bca84, clientIf=9
07-10 13:27:00.562  5001  5015 D BluetoothGatt: onClientRegistered() - status=0 clientIf=9
07-10 13:27:00.563  1957  5052 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:00.564  1957  5052 I A2dpService: audio isMusicActive is false
07-10 13:27:00.565  1957  5052 D BtGatt.GattService: clientConnect() - address=00:10:20:8E:26:97, isDirect=true
07-10 13:27:00.565  1957  2068 D bt_btif_config: btif_get_address_type: Device [00:10:20:8e:26:97] address type 0
07-10 13:27:00.566  1957  2068 D bt_btif_config: btif_get_device_type: Device [00:10:20:8e:26:97] type 2
07-10 13:27:00.567  1957  2319 W bt_l2cap: l2cble_init_direct_conn
07-10 13:27:00.573  2198  2304 I WCNSS_FILTER: ibs_bt_device_wakeup: Writing IBS_WAKE_IND
07-10 13:27:01.577  2198  2325 I WCNSS_FILTER: ibs_wcnss_bt_device_sleep: TX Awake, Sending SLEEP_IND
07-10 13:27:01.926  1957  2319 W bt_btm  : btm_acl_created hci_handle=4 link_role=0  transport=2
07-10 13:27:01.927  2198  2304 I WCNSS_FILTER: ibs_bt_device_wakeup: Writing IBS_WAKE_IND
07-10 13:27:01.946  1957  2068 D BtGatt.GattService: onConnected() connId=5, address=00:10:20:8E:26:97, connected=true
07-10 13:27:01.946  1957  2068 D BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=00:10:20:8E:26:97
07-10 13:27:01.946  1957  2319 W bt_smp  : smp_br_connect_callback is called on unexpected transport 2
07-10 13:27:01.948  1957  2068 D bt_btif_config: btif_get_device_type: Device [00:10:20:8e:26:97] type 2
07-10 13:27:01.948  1957  2319 E bt_btif : bta_dm_acl_change new acl connetion:count = 1
07-10 13:27:01.949  1957  2319 W bt_btif : bta_dm_acl_change info: 0x0
07-10 13:27:01.949  1957  2319 W bt_l2cap: L2CA_SetDesireRole() new:x1, disallow_switch:0
07-10 13:27:01.949  1957  2068 D BluetoothRemoteDevices: Property type: 4
07-10 13:27:01.950  1957  2319 E bt_btif : bta_gattc_cache_load: can't open GATT cache file /data/misc/bluetooth/gatt_cache_0010208e2697 for reading, error: No such file or directory
07-10 13:27:01.952  1957  2068 D BluetoothRemoteDevices: Remote class is:7936
07-10 13:27:01.953  1957  2068 D BluetoothRemoteDevices: Property type: 5
07-10 13:27:01.953  1957  2068 D bt_btif_config: btif_get_device_type: Device [00:10:20:8e:26:97] type 2
07-10 13:27:01.953  1957  2068 I bt_btif_dm: get_cod remote_cod = 0x00001f00
07-10 13:27:01.953  1957  2068 I BluetoothBondStateMachine: bondStateChangeCallback: Status: 0 Address: 00:10:20:8E:26:97 newState: 1
07-10 13:27:01.954  1957  2068 I BluetoothBondStateMachine: sspRequestCallback: [B@f9753ea name: [B@ffc36db cod: 7936 pairingVariant 2 passkey: 0
07-10 13:27:01.955  1957  2068 D bt_btif_dm: remote version info [00:10:20:8e:26:97]: 0, 0, 0
07-10 13:27:01.957  1957  2068 E BluetoothRemoteDevices: state12newState0
07-10 13:27:01.957  1957  2068 D BluetoothRemoteDevices: aclStateChangeCallback: State:Connected to Device:00:10:20:8E:26:97
07-10 13:27:01.957  1957  2069 I BluetoothBondStateMachine: Bond State Change Intent:00:10:20:8E:26:97 OldState: 10 NewState: 11
07-10 13:27:01.957  1957  2069 I BluetoothBondStateMachine: Entering PendingCommandState State
07-10 13:27:01.958  2128  2128 W BluetoothEventManager: CachedBluetoothDevice for device 00:10:20:8E:26:97 not found, calling readPairedDevices().
07-10 13:27:01.961  2128  2128 E BluetoothEventManager: Got bonding state changed for 00:10:20:8E:26:97, but we have no record of that device.
07-10 13:27:01.974  1957  2068 D BtGatt.GattService: onConnected() - clientIf=9, connId=9, address=00:10:20:8E:26:97
07-10 13:27:01.975  5001  5015 D BluetoothGatt: onClientConnectionState() - status=0 clientIf=9 device=00:10:20:8E:26:97
07-10 13:27:01.975  5001  5015 D BluetoothGatt: discoverServices() - device: 00:10:20:8E:26:97
07-10 13:27:01.977  1957  2173 D BtGatt.GattService: discoverServices() - address=00:10:20:8E:26:97, connId=9
07-10 13:27:01.978  1957  1957 V BluetoothFtpService: Ftp Service onStartCommand
07-10 13:27:01.978  1957  1957 V BluetoothFtpService: PARSE INTENT action: android.bluetooth.device.action.BOND_STATE_CHANGED
07-10 13:27:01.984  5001  5001 D BleDexDevice: Received pairing request intent, Device mac address is: 00:10:20:8E:26:97. Device pairingrequest type is: 3
07-10 13:27:01.984  5001  5001 D BleDexDevice: Set pin to BT = 369371
07-10 13:27:01.986  1957  1957 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:01.987  1957  1957 D BluetoothFtpService: device: DEXAdapter
07-10 13:27:01.987  1957  2319 W bt_smp  : SMP_PasskeyReply() - Wrong State: 1
07-10 13:27:01.990  1957  2319 E bt_btm  : BTM_SetBlePhy failed, peer does not support request
07-10 13:27:01.993  1957  1957 D BluetoothDunService: parseIntent: action: android.bluetooth.device.action.BOND_STATE_CHANGED
07-10 13:27:02.000  1957  1957 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:02.002  1957  1957 D BluetoothDunService: device: DEXAdapter
07-10 13:27:02.456  1957  2319 W bt_btm  : btm_read_remote_version_complete: BDA: 00-10-20-8e-26-97
07-10 13:27:02.456  1957  2319 W bt_btm  : btm_read_remote_version_complete lmp_version 7 manufacturer 13 lmp_subversion 528
07-10 13:27:02.837  1957  2319 W bt_bta_gattc: bta_gattc_explore_srvc no more services found
07-10 13:27:02.840  1957  2068 D BtGatt.GattService: onSearchCompleted() - connId=9, status=0
07-10 13:27:02.843  1957  2068 D bt_bta_gattc: bta_gattc_get_gatt_db
07-10 13:27:02.845  1957  2068 D BtGatt.GattService: onGetGattDb() - address=00:10:20:8E:26:97
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got service with UUID=00001800-0000-1000-8000-00805f9b34fb
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got characteristic with UUID=00002a00-0000-1000-8000-00805f9b34fb
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got characteristic with UUID=00002a01-0000-1000-8000-00805f9b34fb
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got characteristic with UUID=00002a04-0000-1000-8000-00805f9b34fb
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got service with UUID=00001801-0000-1000-8000-00805f9b34fb
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got service with UUID=f000c0e0-0451-4000-b000-000000000000
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got characteristic with UUID=f000c0e1-0451-4000-b000-000000000000
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
07-10 13:27:02.846  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002901-0000-1000-8000-00805f9b34fb
07-10 13:27:02.847  1957  2068 D BtGatt.GattService: got characteristic with UUID=f000c0e2-0451-4000-b000-000000000000
07-10 13:27:02.847  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002901-0000-1000-8000-00805f9b34fb
07-10 13:27:02.847  1957  2068 D BtGatt.GattService: got service with UUID=f000ffd0-0451-4000-b000-000000000000
07-10 13:27:02.847  1957  2068 D BtGatt.GattService: got characteristic with UUID=f000ffd1-0451-4000-b000-000000000000
07-10 13:27:02.847  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002901-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got characteristic with UUID=f000ffd2-0451-4000-b000-000000000000
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002901-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got characteristic with UUID=f000ffd3-0451-4000-b000-000000000000
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002901-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got service with UUID=0000180f-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got characteristic with UUID=00002a19-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002902-0000-1000-8000-00805f9b34fb
07-10 13:27:02.848  1957  2068 D BtGatt.GattService: got descriptor with UUID=00002904-0000-1000-8000-00805f9b34fb
07-10 13:27:02.854  5001  5015 D BluetoothGatt: onSearchComplete() = Device=00:10:20:8E:26:97 Status=0
07-10 13:27:02.855  5001  5015 D BluetoothGatt: setCharacteristicNotification() - uuid: f000c0e1-0451-4000-b000-000000000000 enable: true
07-10 13:27:02.856  1957  1972 D BtGatt.GattService: registerForNotification() - address=00:10:20:8E:26:97 enable: true
07-10 13:27:02.857  1957  2068 D BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, handle=11
07-10 13:27:02.863  5001  5338 D BleDexToolkitSample: BLE DEX Adapter connection Success
07-10 13:27:02.870  5001  5001 D BleDexToolkitSample: BLE DEX Adapter connection Success.
07-10 13:27:03.863  2198  2325 I WCNSS_FILTER: ibs_wcnss_bt_device_sleep: TX Awake, Sending SLEEP_IND
07-10 13:27:07.947  2198  2304 I WCNSS_FILTER: ibs_bt_device_wakeup: Writing IBS_WAKE_IND
07-10 13:27:08.950  2198  2325 I WCNSS_FILTER: ibs_wcnss_bt_device_sleep: TX Awake, Sending SLEEP_IND

=android device disconneted the ble =

07-10 13:27:31.953  1957  2068 D bt_btif_config: btif_get_device_type: Device [00:10:20:8e:26:97] type 2
07-10 13:27:31.953  1957  2068 I bt_btif_dm: get_cod remote_cod = 0x00001f00
07-10 13:27:31.953  2172  5348 E bt_logger: Deleting old log file /data/media/0/bt_vnd_log20190710131438.txt
07-10 13:27:31.954  1957  2068 I BluetoothBondStateMachine: bondStateChangeCallback: Status: 1 Address: 00:10:20:8E:26:97 newState: 0
07-10 13:27:31.955  2172  5348 E bt_logger: Writing logs to file
07-10 13:27:31.955  1957  2069 D BluetoothAdapterProperties: Failed to remove device: 00:10:20:8E:26:97
07-10 13:27:31.962  2128  2128 W BluetoothEventManager: CachedBluetoothDevice for device 00:10:20:8E:26:97 not found, calling readPairedDevices().
07-10 13:27:31.964  1957  2069 I BluetoothBondStateMachine: Bond State Change Intent:00:10:20:8E:26:97 OldState: 11 NewState: 10
07-10 13:27:31.968  1957  2069 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:31.970  1957  2069 D A2dpService: Enter setPriority
07-10 13:27:31.972  1957  2069 D A2dpService: Saved priority 00:10:20:8E:26:97 = -1
07-10 13:27:31.972  1957  2069 D A2dpService: Exit setPriority
07-10 13:27:31.972  2128  2128 E BluetoothEventManager: Got bonding state changed for 00:10:20:8E:26:97, but we have no record of that device.
07-10 13:27:31.975  1957  2069 I BluetoothBondStateMachine: StableState(): Entering Off State
07-10 13:27:31.990  1957  1957 V BluetoothFtpService: Ftp Service onStartCommand
07-10 13:27:31.990  1957  1957 V BluetoothFtpService: PARSE INTENT action: android.bluetooth.device.action.BOND_STATE_CHANGED
07-10 13:27:31.994  1957  1957 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:31.994  1957  1957 D BluetoothFtpService: device: DEXAdapter
07-10 13:27:31.996  1957  1957 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:31.996  1957  1957 D BluetoothFtpService: BOND_STATE_CHANGED REFRESH trustDevices DEXAdapter
07-10 13:27:32.002  1957  1957 D BluetoothDunService: parseIntent: action: android.bluetooth.device.action.BOND_STATE_CHANGED
07-10 13:27:32.003  1957  1957 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:32.004  1957  1957 D BluetoothDunService: device: DEXAdapter
07-10 13:27:32.005  1957  1957 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:32.005  1957  1957 D BluetoothDunService: BOND_STATE_CHANGED REFRESH trustDevices DEXAdapter
07-10 13:27:32.020  1937  2199 W BluetoothEventManager: showUnbondMessage: Not displaying any message for reason: 9
07-10 13:27:32.021  2681  3699 I LicenseObserver: installLicenses - /storage/emulated/0/bt_vnd_log20190710132731.txt
07-10 13:27:34.953  1957  2066 D bt_osi_alarm: reschedule_root_alarm alarm expiration too close for posix timers, switching to guns
07-10 13:27:34.959  2198  2304 I WCNSS_FILTER: ibs_bt_device_wakeup: Writing IBS_WAKE_IND
07-10 13:27:34.994  1957  2319 W bt_btif : bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
07-10 13:27:34.994  1957  2319 W bt_btif : bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0016
07-10 13:27:34.995  1957  2319 W bt_btif : bta_gattc_conn_cback() - cif=6 connected=0 conn_id=6 reason=0x0016
07-10 13:27:34.995  1957  2319 W bt_btif : bta_gattc_conn_cback() - cif=7 connected=0 conn_id=7 reason=0x0016
07-10 13:27:34.995  1957  2319 W bt_btif : bta_gattc_conn_cback() - cif=8 connected=0 conn_id=8 reason=0x0016
07-10 13:27:34.995  1957  2068 D BtGatt.GattService: onConnected() connId=5, address=00:10:20:8E:26:97, connected=false
07-10 13:27:34.995  1957  2319 W bt_btif : bta_gattc_conn_cback() - cif=9 connected=0 conn_id=9 reason=0x0016
07-10 13:27:34.995  1957  2068 D BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=00:10:20:8E:26:97
07-10 13:27:34.996  1957  2319 I bt_btm_sec: btm_sec_disconnected clearing pending flag handle:4 reason:22
07-10 13:27:34.998  1957  2068 D BtGatt.GattService: onDisconnected() - clientIf=9, connId=9, address=00:10:20:8E:26:97
07-10 13:27:34.999  5001  5014 D BluetoothGatt: onClientConnectionState() - status=22 clientIf=9 device=00:10:20:8E:26:97
07-10 13:27:34.999  5001  5014 D BleDexDevice: onConnectionStateChange failure status=22 newState=0
07-10 13:27:34.999  2172  2172 E bt_logger: Logger Process: Invalid packet with no length field
07-10 13:27:34.999  2172  2172 E bt_logger: Error saving packet, buff = ound p_reg tcb_idx=0 gatt_if=9  conn_id=0x9�\
07-10 13:27:34.999  2172  2172 E bt_logger: Error saving packet, buff = 
07-10 13:27:35.000  1957  2319 W bt_l2cap: L2CA_SetDesireRole() new:x1, disallow_switch:0
07-10 13:27:35.001  1957  2319 E bt_btif : bta_gattc_mark_bg_conn unable to find the bg connection mask for: 00:10:20:8e:26:97
07-10 13:27:35.002  1957  2068 E BluetoothRemoteDevices: state12newState1
07-10 13:27:35.002  1957  2068 D BluetoothRemoteDevices: aclStateChangeCallback: State:DisConnected to Device:00:10:20:8E:26:97
07-10 13:27:35.004  5001  5001 D BleDexToolkitSample: BLE DEX Adapter disconnected.
07-10 13:27:35.006  1957  1957 D AvrcpBipRsp: onReceive: android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:35.006  1957  1957 D BluetoothMapService: onReceive
07-10 13:27:35.006  1957  1957 D BluetoothMapService: onReceive: android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:35.006  1957  1957 E BluetoothMapService: Unexpected error!
07-10 13:27:35.008  1957  1957 D BluetoothPbapReceiver: PbapReceiver onReceive action = android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:35.009  1957  1957 D BluetoothPbapReceiver: Calling start service with action = null
07-10 13:27:35.012  1957  1957 I BluetoothPbapReceiver: Exit - onReceive for intent:android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:35.013  1957  1957 D BluetoothPbapService: Enter - onStartCommand for service PBAP
07-10 13:27:35.013  1957  1957 D BluetoothPbapService: action: android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:35.013  1957  1957 D BluetoothPbapService: Exit - onStartCommand for service PBAP
07-10 13:27:35.026  1957  2829 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:35.027  5034  5034 I BTLEASH : BTReceive action : android.bluetooth.device.action.ACL_DISCONNECTED :: Device : DEXAdapter
07-10 13:27:35.030  1957  1970 D A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d4dde01
07-10 13:27:35.045  1957  1957 V BluetoothFtpService: Ftp Service onStartCommand
07-10 13:27:35.046  1957  1957 V BluetoothFtpService: PARSE INTENT action: android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:35.053  1957  1957 D BluetoothDunService: parseIntent: action: android.bluetooth.device.action.ACL_DISCONNECTED
07-10 13:27:36.005  2198  2325 I WCNSS_FILTER: ibs_wcnss_bt_device_sleep: TX Awake, Sending SLEEP_IND
^C

提前致谢。

【问题讨论】:

您使用的手机是什么牌子的? 需要一些说明 BLE 连接连接和断开以使用 DEX/UCS-MMJ 我有同样的问题,但硬件部分不是我的。我通过在客户端清除缓存解决了这个问题 【参考方案1】:

Disconnect with reason 8 表示连接超时,这是硬件问题(不是软件问题)。连接超时失败的唯一原因(至少我现在能想到的)是当两个设备尝试使用两个不同的绑定密钥时。然后两者都使用不同的密钥设置加密,然后因此失败。

您可以使用空气嗅探器找出断开连接的原因或时间。

【讨论】:

在与我的硬件团队讨论后,他们同意硬件设备存在问题,导致蓝牙断开连接。有时我也会收到以下日志消息 bt_btif : bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016 这表明它是 BT_HCI_ERR_LOCALHOST_TERM_CONN【参考方案2】:

如果 Android 在 30 秒后断开连接,则表示远程设备没有响应 GATT 请求或 SMP(配对)请求等请求。请确保远程设备正确响应所有请求。如果您不知道哪个请求没有得到响应,请使用 HCI 日志或空气嗅探器查看哪个数据包没有响应。

【讨论】:

我想我有一个理论,为什么它会断开连接。由于我已经知道 ble 设备的 mac 地址,所以我直接连接到没有扫描和配对。所以我相信因为我不需要扫描,因为我知道mac地址,但是我在使用BluetoothGatt连接设备之前通过调用createBond()方法进行了配对。所以现在我的设备不再断开连接了。 @Emil,您能否告诉您的 cmets 我所做的这些更改? 扫描与否无关。如果您进行不同的绑定,那么这可能会使您的绑定顺序不同,并且可能是之前失败的原因。但是您应该检查数据包日志以真正找出原因。否则我们只能推测。【参考方案3】:

在已经知道 MAC 地址的情况下跳过扫描,但保持配对 - 除非不是对方的固件意外终止连接。随机连接断开的固件可能至少有助于测试。

【讨论】:

是的,这就是我所做的,它正在工作并且不再断开连接

以上是关于Android 应用程序会自动断开蓝牙连接的主要内容,如果未能解决你的问题,请参考以下文章

oppo手机怎么查看蓝牙耳机断开时间

Android怎么检测蓝牙的连接状态?如果一段断开,我这边怎么检测得到?

当蓝牙断开连接或重新连接时,我的webview会重新加载

微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位

微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位

自动将蓝牙连接到最后连接的设备