@Override public void run() { if(mState==State.Idle){ updateNodeStatus(State.Lost); }//if }//run };
/** * store the new rssi, and call the call back if needed * @param rssi new rssi to store */ protected void updateRssi(int rssi){ mRssi =rssi; mLastRssiUpdate = new Date(); // now if(mState==State.Lost) updateNodeStatus(State.Idle); for(BleConnectionParamUpdateListener listener : mBleConnectionListeners){ listener.onRSSIChanged(this, rssi); }//for }//updateRssi
/** * close the node connection */ public void disconnect(){ if(!isConnected()) return; mUserAskToDisconnect=true; updateNodeStatus(State.Disconnecting); // run the waitCompleteAllWriteRequest in a different handler for avoid to block the // ble/main thread, we use the handle used for update the rssi, since during disconnection // is not used mHandler.post(new Runnable() { @Override public void run() { waitCompleteAllDescriptorWriteRequest(mDisconnectTask); mHandler.postDelayed(mSetNodeLost, NODE_LOST_TIMEOUT_MS); } }); }//disconnect
@Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { //descriptor write -> remove from the queue if(status == BluetoothGatt.GATT_SUCCESS) dequeueWriteDesc(new WriteDescCommand(descriptor,descriptor.getValue())); else{ if(!isPairing()) { Log.e(TAG,"onDescriptorWrite Error writing the descriptor: "+descriptor .getCharacteristic()); Node.this.updateNodeStatus(State.Dead); }//if }//if-else }
public void connect(Context c, ConnectionOption options){ //if we are already connected or we are connecting avoid do to send again the command if(mState == State.Connected || mState==State.Connecting){ return; } updateNodeStatus(State.Connecting); if(options == null) options = ConnectionOption.buildDefault(); //we start the connection so we will stop to receive advertise, so we delete the timeout if(mHandler!=null) mHandler.removeCallbacks(mSetNodeLost); mUserAskToDisconnect=false; /* HandlerThread thread = new HandlerThread("NodeConnection"); thread.start(); mBleThread = new Handler(thread.getLooper()); */ mBleThread = new Handler(Looper.getMainLooper()); mContext=c; setBoundListener(c.getApplicationContext()); mConnectionOption = options; addExternalCharacteristics(options.getUserDefineFeature()); mBleThread.post(mConnectionTask); }
Node.this.updateNodeStatus(State.Idle); }else{ Node.this.updateNodeStatus(State.Unreachable); newState == BluetoothGatt.STATE_DISCONNECTED && mConnectionOption.enableAutoConnect()){ Node.this.updateNodeStatus(State.Unreachable); return; Node.this.updateNodeStatus(State.Dead);
/** * create a new node * @param device android ble device * @param rssi rssi of the advertise message * @param advertise advertise message for this node * @throws InvalidBleAdvertiseFormat if the advertise is not well formed */ public Node(BluetoothDevice device,int rssi,byte advertise[]) throws InvalidBleAdvertiseFormat{ mAdvertise = new BleAdvertiseParser(advertise); mDevice = device; mExternalCharFeatures= new HashMap<>(); updateRssi(rssi); updateNodeStatus(State.Idle); initHandler(); addNodeStateListener(mNotifyCommandChange); Log.i(TAG, mAdvertise.toString()); }
/** * receive the data after a reading * @param gatt connection with the device * @param characteristic characteristics that contain the response data * @param status command status */ @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.d(TAG,"Read Char: "+characteristic.getUuid().toString()); if(status == BluetoothGatt.GATT_SUCCESS) { if (mDebugConsole != null && BLENodeDefines.Services.Debug.isDebugCharacteristics(characteristic.getUuid())) mDebugConsole.receiveCharacteristicsUpdate(characteristic); else if (mConfigControl != null && characteristic.getUuid().equals(BLENodeDefines.Services.Config.REGISTERS_ACCESS_UUID)) mConfigControl.characteristicsUpdate(characteristic); else updateFeature(characteristic); }else{ if(!isPairing()) { Log.e(TAG,"Error reading the characteristics: "+characteristic); Node.this.updateNodeStatus(State.Dead); }//if }//if-else }
Node.this.updateNodeStatus(State.Dead); mNScanRequest.decrementAndGet(); return; Node.this.updateNodeStatus(State.Dead); mNScanRequest.decrementAndGet(); return; Node.this.updateNodeStatus(State.Connected);
/** * create a new node from the advertise, the advertise have to contain the mac address * @param advertise advertise message for this node * @throws InvalidBleAdvertiseFormat if the advertise is not well formed, for this method is * mandatory that the advertise contains also the mac address */ public Node(byte advertise[]) throws InvalidBleAdvertiseFormat{ mAdvertise = new BleAdvertiseParser(advertise); String bleAddress = mAdvertise.getAddress(); if(bleAddress==null){ throw new InvalidBleAdvertiseFormat("Device Address non present in the advertise"); } mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(bleAddress); mExternalCharFeatures= new HashMap<>(); updateNodeStatus(State.Idle); initHandler(); addNodeStateListener(mNotifyCommandChange); Log.i(TAG, mAdvertise.toString()); }