蓝牙Gatt问题onClientConnectionState()-status = 8 clientIf = 29 [英] Bluetooth Gatt issue onClientConnectionState() - status=8 clientIf=29

查看:470
本文介绍了蓝牙Gatt问题onClientConnectionState()-status = 8 clientIf = 29的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作android BLE应用.根据文件,我提出申请.我的问题是,有时该应用程序可以正常运行,但有时会抛出错误.有2个日志,一个为成功日志,第二个为错误日志.每次蓝牙断开时,我都发送关闭方法.我不知道我被困在哪里.

第一个成功日志

7-11 11:44:54.371 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: try to connecte
07-11 11:44:54.371 17649-17649/com.clavax.ultrapen D/BluetoothGatt: connect() - device: 00:13:43:07:98:D4, auto: false
07-11 11:44:54.371 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp()
07-11 11:44:54.371 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp() - UUID=240e1b71-f4c5-43f2-9535-cd337b501374
07-11 11:44:54.421 17649-17660/com.clavax.ultrapen D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
07-11 11:44:54.611 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=00:13:43:07:98:D4
07-11 11:44:54.611 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP connection created 2 0
07-11 11:44:54.621 17649-17649/com.clavax.ultrapen D/BluetoothGatt: discoverServices() - device: 00:13:43:07:98:D4
07-11 11:44:54.621 17649-17649/com.clavax.ultrapen D/BluetoothGatt 1: Discover Services started: true
07-11 11:44:54.621 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1:  connection created
07-11 11:44:54.811 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=6 status=0
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen D/BluetoothGatt: onSearchComplete() = Device=00:13:43:07:98:D4 Status=0
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: fdd6b4d3-046d-4330-bdec-1fd0c90cb43b enable: true
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered 2
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate1
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: 18cda784-4bd3-4370-85bb-bfed91ec86af enable: true
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate notification1true
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=39 status=0
07-11 11:44:55.761 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate write1true
07-11 11:44:55.841 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:44:55.841 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate2
07-11 11:44:55.841 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate read2true
07-11 11:44:55.931 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic read 1
07-11 11:44:55.931 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate3
07-11 11:44:55.941 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP brsp m0detrue
07-11 12:07:21.461 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic read 2
07-11 12:07:21.561 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 1
07-11 12:07:21.561 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 3
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP write byte0
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sending
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sendingtrue
07-11 12:07:21.601 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.601 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP return
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 1
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 2
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sending
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sendingtrue
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 1
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 2
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP bytes null
07-11 12:07:21.901 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic change 1
07-11 12:07:21.901 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic change 2
07-11 12:07:21.901 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP write byte�eqInfo|PTType>ORP|P
continue.........................................

第二个日志

07-11 11:42:25.451 17649-17649/com.clavax.ultrapen D/BluetoothGatt: connect() - device: 00:13:43:07:98:D4, auto: false
07-11 11:42:25.451 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp()
07-11 11:42:25.451 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp() - UUID=8bcbf61d-323c-46c8-b509-3f29dbcc5002
07-11 11:42:25.491 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
07-11 11:42:25.881 17649-17660/com.clavax.ultrapen D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=00:13:43:07:98:D4
07-11 11:42:25.881 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP connection created 2 0
07-11 11:42:25.891 17649-17649/com.clavax.ultrapen D/BluetoothGatt: discoverServices() - device: 00:13:43:07:98:D4
07-11 11:42:25.891 17649-17649/com.clavax.ultrapen D/BluetoothGatt 1: Discover Services started: true
07-11 11:42:25.891 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1:  connection created
07-11 11:42:26.081 17649-17660/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=6 status=0
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen D/BluetoothGatt: onSearchComplete() = Device=00:13:43:07:98:D4 Status=0
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: fdd6b4d3-046d-4330-bdec-1fd0c90cb43b enable: true
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered 2
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate1
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: 18cda784-4bd3-4370-85bb-bfed91ec86af enable: true
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate notification1true
07-11 11:42:26.561 17649-17661/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=39 status=0
07-11 11:42:27.051 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: l;e scanningtrue
07-11 11:42:27.131 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate write1true
07-11 11:42:46.571 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:42:46.571 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate2
07-11 11:42:46.581 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=8 device=00:13:43:07:98:D4
07-11 11:42:46.581 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP disconnection created 0 8     ******//here is the error throw****** 

如上面的日志所示,第一个显示数据,但是第二个显示状态8 的问题,它会在成功连接3或4后发生.我的想法是,如果出现问题在代码中没有给我一个成功的连接.

///BluetoothServiceClass中的Bluetooth Gatt回调

@Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            int inputBufferSize = 1000;
            int outputBufferSize = 1000;
            _lastRTS = 0;
            inputBufferSize = (inputBufferSize < 1) ? DEFAULT_BUFFER_SIZE : inputBufferSize;
            inputBufferSize = (inputBufferSize < 1) ? DEFAULT_BUFFER_SIZE : inputBufferSize;

            setBuffers(inputBufferSize, outputBufferSize);


            Log.i("BluetoothGatt 1", "BRSP connection created " + newState + " " + status);
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            // Attempts to discover services after successful connection.

            Log.d(TAG, "Device connected");
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    boolean ans = mBluetoothGatt.discoverServices();
                    Log.d("BluetoothGatt 1", "Discover Services started: " + ans);
                }
            });


        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i("BluetoothGatt 1", "BRSP disconnection created " + newState + " " + status);

            //    refreshDeviceCache(gatt);
            mBluetoothGatt.close();

            _initState = 0;
            _lastRTS = 0;
            _lastRTS = 0;
            setBuffers(_inputBuffer.size() + _inputBuffer.remainingCapacity(), _outputBuffer.size() + _outputBuffer.remainingCapacity());
            if (status != 8 && status != 22)
                broadcastUpdate(intentAction);
            else
                connect(mBluetoothDeviceAddress);
            //  mBluetoothGatt.close();
        }
    }
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {

    Log.i("BluetoothGatt 1", "BRSP service discovered");
    BluetoothGattService brspService1 = gatt.getService(BRSP_SERVICE_UUID1);
    BluetoothGattService brspService = gatt.getService(BRSP_SERVICE_UUID);
    List<BluetoothGattService> brspservices = getSupportedGattServices();

    if (brspService != null) {
        // Call the first write descriptor for initializing the BRSP
        // serrvice.
        mBluetoothGatt.setCharacteristicNotification(brspService.getCharacteristic(BRSP_RTS_UUID), true);
        BluetoothGattDescriptor RTS_CCCD = brspService.getCharacteristic(BRSP_RTS_UUID).getDescriptor(
                UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        RTS_CCCD.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        mBluetoothGatt.writeDescriptor(RTS_CCCD);

        Log.i("BluetoothGatt 1", "BRSP service discovered 2");
    }
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
    Log.i("BluetoothGatt 1", "BRSP characteristic read 1");
    if (_initState < _initStepCount) {
        doNextInitStep();
    }
    if (characteristic.getUuid().equals(BRSP_INFO_UUID)) {
        characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
        Log.i("BluetoothGatt 1", "BRSP characteristic read 2");
    }
}

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        Log.i("BluetoothGatt 1", "BRSP characteristic write 1");
        if (_initState < _initStepCount) {
            doNextInitStep();
        }
        if (characteristic.getUuid().equals(BRSP_RX_UUID)) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
          /*      _lastBytes = null;
                if (_outputBuffer.isEmpty())
                    _brspCallback.onSendingStateChanged(Brsp.this);*/
                Log.i("BluetoothGatt 1", "BRSP characteristic write 2");
            }
            _sending = false;
            sendPacket();
            // debugLog("RX characteristic wrote");
        } else if (characteristic.getUuid().equals(BRSP_MODE_UUID)) {
            characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
            Log.i("BluetoothGatt 1", "BRSP characteristic write 3");
            _outputBuffer.clear();
            String intentAction = ACTION_Eco_Send;
            broadcastUpdate(intentAction);
        }
        if (status != 0) {
            if (status == 15) {
                // Can't figure out a fix to the pairing issues as of yet
                // _gatt.getDevice().createBond();
                // TODO: Resend last write once bonded?
            }
        }
    }

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        Log.i("BluetoothGatt 1", "BRSP description read");
        if (_initState < _initStepCount) {
            doNextInitStep();
        }
        super.onDescriptorRead(gatt, descriptor, status);
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        Log.i("BluetoothGatt 1", "BRSP description write");
        if (_initState < _initStepCount) {
            doNextInitStep();
        }
        super.onDescriptorWrite(gatt, descriptor, status);
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {

        Log.i("BluetoothGatt 1", "BRSP characteristic change 1");

        if (_initState < _initStepCount) {
            doNextInitStep();
        } else {
            if (characteristic.getUuid().equals(BRSP_TX_UUID)) {
                Log.i("BluetoothGatt 1", "BRSP characteristic change 2");

                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            } else if (characteristic.getUuid().equals(BRSP_RTS_UUID)) {
                Log.i("BluetoothGatt 1", "BRSP characteristic change ");
                _lastRTS = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
            }
        }
    }
};



  private void doNextInitStep() {
        _initState++;
        // debugLog("initState:" + _initState);
        Log.i("BluetoothGatt 1", "BRSP initstate" + _initState);
        BluetoothGattService brspService = mBluetoothGatt.getService(BRSP_SERVICE_UUID);
        if (brspService == null)
            Log.i("BluetoothGatt 1", "BRSP brsp service null");

        switch (_initState) {
            case 1:
                boolean initstateNotification = mBluetoothGatt.setCharacteristicNotification(brspService.getCharacteristic(BRSP_TX_UUID), true);
                Log.i("BluetoothGatt 1", "BRSP initstate notification" + _initState + initstateNotification);
                final BluetoothGattDescriptor TX_CCCD = brspService.getCharacteristic(BRSP_TX_UUID).getDescriptor(
                        UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                TX_CCCD.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                Log.d(TAG, "Device connected");
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        boolean initstatewrite = mBluetoothGatt.writeDescriptor(TX_CCCD);
                        Log.i("BluetoothGatt 1", "BRSP initstate write" + _initState + initstatewrite);
                    }
                }, 500);
                break;
            case 2:
                final BluetoothGattCharacteristic brspInfo = brspService.getCharacteristic(BRSP_INFO_UUID);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        boolean initstateread = mBluetoothGatt.readCharacteristic(brspInfo);
                        Log.i("BluetoothGatt 1", "BRSP initstate read" + _initState + initstateread);
                    }
                });

                break;
            case 3:
                setBrspMode(BRSP_MODE_DATA); // Important: Make sure this is the
                // last init step
                break;
            default:
                break;
        }
        if (_initState == _initStepCount) {
            // _brspState = BRSP_STATE_READY;
            // _brspCallback.onBrspStateChanged(this);
        }
    }

解决方案

发生的问题已由我解决.

我已经看到有很多与状态8和22有关的问题会在我们连接到设备时发生.我是说这是状态8和状态22的确切答案,但是在某些情况下,这是原因.

之所以发生这种情况,是因为当我们成功发现服务时,我们编写描述符并立即调用ondescripte覆盖方法,然后进行读写操作.因此,由于不执行一个流程而造成的后果可能会导致这些问题.

我发现的解决方案效果很好.始终尝试制作 Looper处理程序,这样我在执行一次执行时会有所延迟.

       new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mBluetoothGatt.writeDescriptor(RTS_CCCD); //for descriptor
  //  or
    mBluetoothGatt.readCharacteristic(brspInfo); //for read
//or
    mBluetoothGatt.writeCharacteristic(brspInfo); //for write
                            }
                        }, 500);

为什么以及什么状态8发出此处. >

为什么以及什么状态22发出此处.

I am making android BLE app. As per the Documents, I make application. My Problem is that sometime the app will work good , but sometime it throw the errors .There is 2 logs, one for success and 2nd for the errors. Every time bluetooth disconnect, i am send close method. I don't know where I am stuck.

1st success log

7-11 11:44:54.371 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: try to connecte
07-11 11:44:54.371 17649-17649/com.clavax.ultrapen D/BluetoothGatt: connect() - device: 00:13:43:07:98:D4, auto: false
07-11 11:44:54.371 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp()
07-11 11:44:54.371 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp() - UUID=240e1b71-f4c5-43f2-9535-cd337b501374
07-11 11:44:54.421 17649-17660/com.clavax.ultrapen D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
07-11 11:44:54.611 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=00:13:43:07:98:D4
07-11 11:44:54.611 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP connection created 2 0
07-11 11:44:54.621 17649-17649/com.clavax.ultrapen D/BluetoothGatt: discoverServices() - device: 00:13:43:07:98:D4
07-11 11:44:54.621 17649-17649/com.clavax.ultrapen D/BluetoothGatt 1: Discover Services started: true
07-11 11:44:54.621 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1:  connection created
07-11 11:44:54.811 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=6 status=0
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen D/BluetoothGatt: onSearchComplete() = Device=00:13:43:07:98:D4 Status=0
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: fdd6b4d3-046d-4330-bdec-1fd0c90cb43b enable: true
07-11 11:44:55.231 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered 2
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate1
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: 18cda784-4bd3-4370-85bb-bfed91ec86af enable: true
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate notification1true
07-11 11:44:55.261 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=39 status=0
07-11 11:44:55.761 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate write1true
07-11 11:44:55.841 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:44:55.841 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate2
07-11 11:44:55.841 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate read2true
07-11 11:44:55.931 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic read 1
07-11 11:44:55.931 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate3
07-11 11:44:55.941 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP brsp m0detrue
07-11 12:07:21.461 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic read 2
07-11 12:07:21.561 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 1
07-11 12:07:21.561 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 3
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP write byte0
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sending
07-11 12:07:21.581 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sendingtrue
07-11 12:07:21.601 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.601 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP return
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 1
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 2
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sending
07-11 12:07:21.661 17649-21210/com.clavax.ultrapen I/BluetoothGatt 1: BRSP sendingtrue
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 1
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic write 2
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP send packets
07-11 12:07:21.801 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP bytes null
07-11 12:07:21.901 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic change 1
07-11 12:07:21.901 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP characteristic change 2
07-11 12:07:21.901 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP write byte�eqInfo|PTType>ORP|P
continue.........................................

2nd log

07-11 11:42:25.451 17649-17649/com.clavax.ultrapen D/BluetoothGatt: connect() - device: 00:13:43:07:98:D4, auto: false
07-11 11:42:25.451 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp()
07-11 11:42:25.451 17649-17649/com.clavax.ultrapen D/BluetoothGatt: registerApp() - UUID=8bcbf61d-323c-46c8-b509-3f29dbcc5002
07-11 11:42:25.491 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
07-11 11:42:25.881 17649-17660/com.clavax.ultrapen D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=00:13:43:07:98:D4
07-11 11:42:25.881 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP connection created 2 0
07-11 11:42:25.891 17649-17649/com.clavax.ultrapen D/BluetoothGatt: discoverServices() - device: 00:13:43:07:98:D4
07-11 11:42:25.891 17649-17649/com.clavax.ultrapen D/BluetoothGatt 1: Discover Services started: true
07-11 11:42:25.891 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1:  connection created
07-11 11:42:26.081 17649-17660/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=6 status=0
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen D/BluetoothGatt: onSearchComplete() = Device=00:13:43:07:98:D4 Status=0
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: fdd6b4d3-046d-4330-bdec-1fd0c90cb43b enable: true
07-11 11:42:26.541 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP service discovered 2
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate1
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen D/BluetoothGatt: setCharacteristicNotification() - uuid: 18cda784-4bd3-4370-85bb-bfed91ec86af enable: true
07-11 11:42:26.551 17649-17660/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate notification1true
07-11 11:42:26.561 17649-17661/com.clavax.ultrapen D/BluetoothGatt: onClientConnParamsChanged() - Device=00:13:43:07:98:D4 interval=39 status=0
07-11 11:42:27.051 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: l;e scanningtrue
07-11 11:42:27.131 17649-17649/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate write1true
07-11 11:42:46.571 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP description write
07-11 11:42:46.571 17649-17661/com.clavax.ultrapen I/BluetoothGatt 1: BRSP initstate2
07-11 11:42:46.581 17649-13359/com.clavax.ultrapen D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=8 device=00:13:43:07:98:D4
07-11 11:42:46.581 17649-13359/com.clavax.ultrapen I/BluetoothGatt 1: BRSP disconnection created 0 8     ******//here is the error throw****** 

As in above Logs , the 1st display the data but in 2nd i got the issue of status 8 , it will happen after 3 or 4 successful connection .My thought is that, If there is issue in the code then it is not given me single success connection.

//Bluetooth Gatt callback in BluetoothServiceClass

@Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            int inputBufferSize = 1000;
            int outputBufferSize = 1000;
            _lastRTS = 0;
            inputBufferSize = (inputBufferSize < 1) ? DEFAULT_BUFFER_SIZE : inputBufferSize;
            inputBufferSize = (inputBufferSize < 1) ? DEFAULT_BUFFER_SIZE : inputBufferSize;

            setBuffers(inputBufferSize, outputBufferSize);


            Log.i("BluetoothGatt 1", "BRSP connection created " + newState + " " + status);
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            // Attempts to discover services after successful connection.

            Log.d(TAG, "Device connected");
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    boolean ans = mBluetoothGatt.discoverServices();
                    Log.d("BluetoothGatt 1", "Discover Services started: " + ans);
                }
            });


        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i("BluetoothGatt 1", "BRSP disconnection created " + newState + " " + status);

            //    refreshDeviceCache(gatt);
            mBluetoothGatt.close();

            _initState = 0;
            _lastRTS = 0;
            _lastRTS = 0;
            setBuffers(_inputBuffer.size() + _inputBuffer.remainingCapacity(), _outputBuffer.size() + _outputBuffer.remainingCapacity());
            if (status != 8 && status != 22)
                broadcastUpdate(intentAction);
            else
                connect(mBluetoothDeviceAddress);
            //  mBluetoothGatt.close();
        }
    }
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {

    Log.i("BluetoothGatt 1", "BRSP service discovered");
    BluetoothGattService brspService1 = gatt.getService(BRSP_SERVICE_UUID1);
    BluetoothGattService brspService = gatt.getService(BRSP_SERVICE_UUID);
    List<BluetoothGattService> brspservices = getSupportedGattServices();

    if (brspService != null) {
        // Call the first write descriptor for initializing the BRSP
        // serrvice.
        mBluetoothGatt.setCharacteristicNotification(brspService.getCharacteristic(BRSP_RTS_UUID), true);
        BluetoothGattDescriptor RTS_CCCD = brspService.getCharacteristic(BRSP_RTS_UUID).getDescriptor(
                UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        RTS_CCCD.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        mBluetoothGatt.writeDescriptor(RTS_CCCD);

        Log.i("BluetoothGatt 1", "BRSP service discovered 2");
    }
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
    Log.i("BluetoothGatt 1", "BRSP characteristic read 1");
    if (_initState < _initStepCount) {
        doNextInitStep();
    }
    if (characteristic.getUuid().equals(BRSP_INFO_UUID)) {
        characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
        Log.i("BluetoothGatt 1", "BRSP characteristic read 2");
    }
}

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        Log.i("BluetoothGatt 1", "BRSP characteristic write 1");
        if (_initState < _initStepCount) {
            doNextInitStep();
        }
        if (characteristic.getUuid().equals(BRSP_RX_UUID)) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
          /*      _lastBytes = null;
                if (_outputBuffer.isEmpty())
                    _brspCallback.onSendingStateChanged(Brsp.this);*/
                Log.i("BluetoothGatt 1", "BRSP characteristic write 2");
            }
            _sending = false;
            sendPacket();
            // debugLog("RX characteristic wrote");
        } else if (characteristic.getUuid().equals(BRSP_MODE_UUID)) {
            characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
            Log.i("BluetoothGatt 1", "BRSP characteristic write 3");
            _outputBuffer.clear();
            String intentAction = ACTION_Eco_Send;
            broadcastUpdate(intentAction);
        }
        if (status != 0) {
            if (status == 15) {
                // Can't figure out a fix to the pairing issues as of yet
                // _gatt.getDevice().createBond();
                // TODO: Resend last write once bonded?
            }
        }
    }

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        Log.i("BluetoothGatt 1", "BRSP description read");
        if (_initState < _initStepCount) {
            doNextInitStep();
        }
        super.onDescriptorRead(gatt, descriptor, status);
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        Log.i("BluetoothGatt 1", "BRSP description write");
        if (_initState < _initStepCount) {
            doNextInitStep();
        }
        super.onDescriptorWrite(gatt, descriptor, status);
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {

        Log.i("BluetoothGatt 1", "BRSP characteristic change 1");

        if (_initState < _initStepCount) {
            doNextInitStep();
        } else {
            if (characteristic.getUuid().equals(BRSP_TX_UUID)) {
                Log.i("BluetoothGatt 1", "BRSP characteristic change 2");

                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            } else if (characteristic.getUuid().equals(BRSP_RTS_UUID)) {
                Log.i("BluetoothGatt 1", "BRSP characteristic change ");
                _lastRTS = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
            }
        }
    }
};



  private void doNextInitStep() {
        _initState++;
        // debugLog("initState:" + _initState);
        Log.i("BluetoothGatt 1", "BRSP initstate" + _initState);
        BluetoothGattService brspService = mBluetoothGatt.getService(BRSP_SERVICE_UUID);
        if (brspService == null)
            Log.i("BluetoothGatt 1", "BRSP brsp service null");

        switch (_initState) {
            case 1:
                boolean initstateNotification = mBluetoothGatt.setCharacteristicNotification(brspService.getCharacteristic(BRSP_TX_UUID), true);
                Log.i("BluetoothGatt 1", "BRSP initstate notification" + _initState + initstateNotification);
                final BluetoothGattDescriptor TX_CCCD = brspService.getCharacteristic(BRSP_TX_UUID).getDescriptor(
                        UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                TX_CCCD.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                Log.d(TAG, "Device connected");
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        boolean initstatewrite = mBluetoothGatt.writeDescriptor(TX_CCCD);
                        Log.i("BluetoothGatt 1", "BRSP initstate write" + _initState + initstatewrite);
                    }
                }, 500);
                break;
            case 2:
                final BluetoothGattCharacteristic brspInfo = brspService.getCharacteristic(BRSP_INFO_UUID);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        boolean initstateread = mBluetoothGatt.readCharacteristic(brspInfo);
                        Log.i("BluetoothGatt 1", "BRSP initstate read" + _initState + initstateread);
                    }
                });

                break;
            case 3:
                setBrspMode(BRSP_MODE_DATA); // Important: Make sure this is the
                // last init step
                break;
            default:
                break;
        }
        if (_initState == _initStepCount) {
            // _brspState = BRSP_STATE_READY;
            // _brspCallback.onBrspStateChanged(this);
        }
    }

解决方案

The issue that happens is resolved by me.

I have seen there are many question related to status 8 and 22 that occurs when we connect to device. I am saying that this is exact answer for status 8 and 22, but in some case it will be reason.

This happens because when we successfully discover the services, we write the descriptor and immediately call the ondescripte override methods and then read and write operation. So what happen, duw to not excecution of one process may create these problems.

The solution that I have found is working great. Always try to make Looper Handler, so that I make some delay to complete one execution.

       new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mBluetoothGatt.writeDescriptor(RTS_CCCD); //for descriptor
  //  or
    mBluetoothGatt.readCharacteristic(brspInfo); //for read
//or
    mBluetoothGatt.writeCharacteristic(brspInfo); //for write
                            }
                        }, 500);

Why and what status 8 issue here.

Why and what status 22 issue here.

这篇关于蓝牙Gatt问题onClientConnectionState()-status = 8 clientIf = 29的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆