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
2nd log
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); } }