当设备仍处于开启状态时,BLE BluetoothGattCallback会获得STATE_DISCONNECTED

时间:2018-11-01 10:54:25

标签: android android-bluetooth android-ble

我使用以下方法连接到蓝牙设备:

/**
     * Connects to the GATT server hosted on the Bluetooth LE device.
     *
     * @param address The device address of the destination device.
     *
     * @return Return true if the connection is initiated successfully. The connection result
     *         is reported asynchronously through the
     *         {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
     *         callback.
     */


      public boolean connect(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }

        // Previously connected device.  Try to reconnect.
        if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
                && mBluetoothGatt != null) {
            Log.e(TAG, "Trying to use an existing mBluetoothGatt for connection.");
            if (mBluetoothGatt.connect()) {
                mConnectionState = STATE_CONNECTING;
                return true;
            } else {
                return false;
            }
        }


        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.e(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        // We want to directly connect to the device, so we are setting the autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
        Log.e(TAG, "Trying to create a new connection.");
        mBluetoothDeviceAddress = address;
        mConnectionState = STATE_CONNECTING;
        return true;
    }

这是我的BluetoothGattCallback:

    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        Log.e(TAG, "In onConnectionStateChange and there has been a change of :" +newState);
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.e(TAG, "Connected to GATT server.");

            // Attempts to discover services after successful connection.
            Log.e(TAG, "Attempting to start service discovery:");
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Boolean discover = mBluetoothGatt.discoverServices();
            if (discover == true) {
                //Raise flag
                unstableBluetoothBehaviour = true;
                Log.e(TAG, "Service discovery returns true and is started");
            } else {
                Log.e(TAG, "Service discovery returns false and has stopped unexpectedly");
            }

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            if (unstableBluetoothBehaviour == true) {

                Log.e(TAG, "Unstable behaviour detected");

            }
            Log.e(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
            close();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.e(TAG, "In onServicesDiscovered");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.e(TAG, "mBluetoothGatt = " + mBluetoothGatt);

            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.e(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }


    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        Log.e(TAG, "Characteristic has been updated = " + characteristic);
    }


};

问题在于,当我调用Connect()时,将使用STATE_CONNECTED调用onConnectionChange()。然后使用STATE_DISCONNECTED再次调用它!


这是 Logcat 的印刷品,它可能有助于您解决问题:

11-01 12:42:29.082 17969-17969/com.bluemaestro.tempo_utility D/ViewRootImpl: #3 mView = null
11-01 12:42:29.172 17969-17969/com.bluemaestro.tempo_utility D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 24 - 0, 0) vi=Rect(0, 24 - 0, 0) or=1
11-01 12:42:29.242 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: onServiceConnected mService= com.bluemaestro.tempo_utility.delivery_monitor.MyUARTService@dd6c856
11-01 12:42:29.242 17969-17969/com.bluemaestro.tempo_utility D/BluetoothGatt: connect() - device: ED:90:ED:F9:4C:60, auto: false
    registerApp()
    registerApp() - UUID=b3664342-0b17-427b-b32b-048726bc32de
11-01 12:42:29.282 17969-17987/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
11-01 12:42:29.292 17969-17969/com.bluemaestro.tempo_utility E/MyUARTService: Trying to create a new connection.
11-01 12:42:29.322 17969-17969/com.bluemaestro.tempo_utility I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@574a141 time:88055063
    Timeline: Activity_idle id: android.os.BinderProxy@c3710fb time:88055065
11-01 12:42:29.512 17969-17969/com.bluemaestro.tempo_utility V/ActivityThread: updateVisibility : ActivityRecord{2870b81 token=android.os.BinderProxy@c3710fb {com.bluemaestro.tempo_utility/com.bluemaestro.tempo_utility.delivery_monitor.ConnectSensorActivity}} show : false
11-01 12:42:30.482 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=ED:90:ED:F9:4C:60
11-01 12:42:30.492 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: In onConnectionStateChange and there has been a change of :2
    Connected to GATT server.
    Attempting to start service discovery:
11-01 12:42:30.502 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: UART_CONNECT_MSG
11-01 12:42:32.502 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: discoverServices() - device: ED:90:ED:F9:4C:60
11-01 12:42:32.502 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: Service discovery returns true and is started
11-01 12:42:32.502 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnParamsChanged() - Device=ED:90:ED:F9:4C:60 interval=6 status=0
    onClientConnParamsChanged() - Device=ED:90:ED:F9:4C:60 interval=39 status=0
11-01 12:42:32.512 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onSearchComplete() = Device=ED:90:ED:F9:4C:60 Status=0
11-01 12:42:32.512 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: In onServicesDiscovered
    mBluetoothGatt = android.bluetooth.BluetoothGatt@e01b0d7
11-01 12:42:32.512 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: ENABLETXNOTIFICATION
11-01 12:42:36.092 17969-17980/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnParamsChanged() - Device=ED:90:ED:F9:4C:60 interval=159 status=0
11-01 12:42:56.842 17969-17969/com.bluemaestro.tempo_utility D/BluetoothAdapter: stopLeScan()
    STATE_ON
    STATE_ON
    scan not started yet
11-01 12:43:00.762 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnectionState() - status=19 clientIf=7 device=ED:90:ED:F9:4C:60
11-01 12:43:00.762 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: In onConnectionStateChange and there has been a change of :0
    Unstable behaviour detected
    Disconnected from GATT server.
    mBluetoothGatt closed
11-01 12:43:00.762 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
11-01 12:43:00.762 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: UART_DISCONNECT_MSG
11-01 12:43:00.762 17969-17969/com.bluemaestro.tempo_utility E/MyUARTService: mBluetoothGatt closed
11-01 12:43:00.762 17969-17969/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
11-01 12:43:00.772 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: unregisterApp() - mClientIf=7
11-01 12:43:00.772 17969-17969/com.bluemaestro.tempo_utility D/BluetoothGatt: unregisterApp() - mClientIf=7
11-01 12:44:28.422 17969-17969/com.bluemaestro.tempo_utility V/ActivityThread: updateVisibility : ActivityRecord{7e3fdc4 token=android.os.BinderProxy@574a141 {com.bluemaestro.tempo_utility/com.bluemaestro.tempo_utility.delivery_monitor.DashboardActivity}} show : true
11-01 12:49:51.792 17969-17969/com.bluemaestro.tempo_utility I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@574a141 time:88497537

另一只日志猫,我却未更改任何代码:

11-01 14:07:25.892 21448-21448/com.bluemaestro.tempo_utility E/MyUARTService: Trying to create a new connection.
11-01 14:07:25.892 21448-21462/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
11-01 14:07:25.902 21448-21448/com.bluemaestro.tempo_utility I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3cd6867 time:93151645
    Timeline: Activity_idle id: android.os.BinderProxy@a2ac28a time:93151645
11-01 14:07:26.112 21448-21448/com.bluemaestro.tempo_utility V/ActivityThread: updateVisibility : ActivityRecord{258b2e1 token=android.os.BinderProxy@a2ac28a {com.bluemaestro.tempo_utility/com.bluemaestro.tempo_utility.delivery_monitor.ConnectSensorActivity}} show : false
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7 device=ED:90:ED:F9:4C:60
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility E/MyUARTService: In onConnectionStateChange and there has been a change of :0
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility E/MyUARTService: Disconnected from GATT server.
    mBluetoothGatt closed
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
    unregisterApp() - mClientIf=7
11-01 14:07:30.902 21448-21448/com.bluemaestro.tempo_utility E/BlueMaestro: UART_DISCONNECT_MSG
11-01 14:07:30.902 21448-21448/com.bluemaestro.tempo_utility E/MyUARTService: mBluetoothGatt closed
11-01 14:07:30.902 21448-21448/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
11-01 14:07:30.912 21448-21448/com.bluemaestro.tempo_utility D/BluetoothGatt: unregisterApp() - mClientIf=7
11-01 14:07:50.572 21448-21448/com.bluemaestro.tempo_utility D/BluetoothAdapter: stopLeScan()
    STATE_ON
    STATE_ON
    scan not started yet

1 个答案:

答案 0 :(得分:0)

1-确保连接到正确的设备。蓝牙扫描器将扫描许多设备,并可能找到无法连接的设备(以与我们连接到BLE设备相同的方式)

2-确保在尝试连接之前或之后不要呼叫-std=c++11。在成功获得连接状态2(已连接)之后,请拨打电话