/** * if not already connected, show the dialog and stat the connection with the node */ @Override public void onResume() { super.onResume(); if (mNode != null && !mNode.isConnected()) { mConnectionWait.show(); //show the dialog and set the listener for hide it mNode.addNodeStateListener(mNodeStateListener); mNode.connect(getActivity()); }//if }//onResume
/** * stop all the enabled notification */ private void disableNeedNotification() { List<Feature> features = mNode.getFeatures(); for (Feature f : features) { if (mNode.isEnableNotification(f)) mNode.disableNotification(f); }//for sTestFeature }//disableNeedNotification
public void enableNotification(){ mParent.enableNotification(this); }
/** * if the node is connected enable the gui otherwise attach a listener for do it when the node * connects */ @Override protected void onResume() { super.onResume(); if (mNode.isConnected()) { setUpConsoleService(mNode.getDebug()); } else mNode.addNodeStateListener(mNodeStateChangeListener); }//onResume
/** * get the device name with some product tag info (BLE Address) * @return the device friendly Name */ public String getFriendlyName(){ if (mFriendlyName == null) { String strTagClean = "NA"; if (getTag() != null && getTag().length() > 0) strTagClean = getTag().replace(":", ""); mFriendlyName = getName() + " @" + strTagClean.substring(strTagClean.length() - Math.min(6, strTagClean.length()), strTagClean.length()); } return mFriendlyName; }//getFriendlyName
shadowDevice.addService(dataService); Node n = createNode(device); n.connect(RuntimeEnvironment.application); Feature f = n.getFeature(FakeFeature.class); Assert.assertFalse(n.isEnableNotification(f)); Assert.assertTrue(n.enableNotification(f)); Assert.assertTrue(n.isEnableNotification(f)); Assert.assertTrue(n.disableNotification(f)); Assert.assertFalse(n.isEnableNotification(f));
synchronized (mDiscoverNode) { for (Node node : mDiscoverNode) { if (node.getTag().equals(deviceAddr)) { node.isAlive(rssi); node.upDateAdvertising(advertisedData); return; final Node newNode = new Node(device, rssi, advertisedData); newNode.addNodeStateListener(mDebugNodeStatus); addNode(newNode); } catch (InvalidBleAdvertiseFormat e) {
/** * When a user select a row we enable/disable the notification for that feature * * @param adapterView list view * @param view selected view * @param position selected row * @param l selected id */ @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Feature selectedFeature = mFeatureListAdapter.getItem(position); if(selectedFeature==null) return; if (mNode.isEnableNotification(selectedFeature)) { selectedFeature.removeFeatureListener(mGenericUpdate); mNode.disableNotification(selectedFeature); ((TextView) view).setText(selectedFeature.getName()); //reset the cell name } else { //create a listener that will update the selected view mGenericUpdate = new GenericFragmentUpdate((TextView) view); selectedFeature.addFeatureListener(mGenericUpdate); mNode.enableNotification(selectedFeature); }//if-else }//onItemClick
/** * compare two nodes. two nodes are equals if the tag function return the same value * @param node object to compare * @return true both object have the same tag value */ @Override public boolean equals(Object node) { return (node instanceof Node) && (node == this || getTag().equals(((Node) node).getTag())); }//equals
/** * ask to the node to notify when the feature change its value * @param feature feature to look * @return false if the feature is not handle by this node or disabled */ public boolean enableNotification(Feature feature){ if(!feature.isEnabled() || feature.getParentNode()!=this) return false; if(isEnableNotification(feature)) return true; BluetoothGattCharacteristic featureChar = getCorrespondingChar(feature); if(charCanBeNotify(featureChar)) { mNotifyFeature.add(feature); return changeNotificationStatus(featureChar, true); } return false; }//enableNotification
/** * 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()); }
@Override public void onStateChange(Node node, Node.State newState, Node.State prevState) { Log.d("NodeStateListener", node.getName() + " " + prevState + "->" + newState); } };
@Test public void connectNodeWithDebug(){ BluetoothDevice device = spy(Shadow.newInstanceOf(BluetoothDevice.class)); BluetoothDeviceShadow shadowDevice = Shadow.extract(device); BluetoothGattService debugService = new BluetoothGattService(BLENodeDefines.Services .Debug.DEBUG_SERVICE_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY); debugService.addCharacteristic( new BluetoothGattCharacteristic(BLENodeDefines.Services.Debug.DEBUG_STDERR_UUID, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic .PROPERTY_NOTIFY)); debugService.addCharacteristic( new BluetoothGattCharacteristic(BLENodeDefines.Services.Debug.DEBUG_TERM_UUID, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic .PROPERTY_NOTIFY) ); shadowDevice.addService(debugService); Node node = createNode(device); Assert.assertEquals(Node.State.Idle, node.getState()); node.connect(RuntimeEnvironment.application); TestUtil.execAllAsyncTask(); verify(device).connectGatt(eq(RuntimeEnvironment.application), eq(false), any(BluetoothGattCallback.class)); Assert.assertEquals(Node.State.Connected, node.getState()); Assert.assertTrue(node.getDebug()!=null); }
/** * if the node is connected enable the gui, otherwise set a listener that will do that */ @Override protected void onResume() { super.onResume(); if (mNode.isConnected()) { populateFeatureList(); invalidateOptionsMenu(); //enable/disable the settings options } else mNode.addNodeStateListener(mNodeStatusListener); }//onResume
public boolean isExportingFeature(Class<? extends Feature> featureClass) { SparseArray<Class<? extends Feature>> decoder = Manager.getNodeFeatures( getTypeId()); long advertiseBitMask = getAdvertiseBitMask(); for (int i = 0 ; i< decoder.size(); i++){ if(featureClass == decoder.valueAt(i)){ long mask = decoder.keyAt(i); if((advertiseBitMask & mask) != 0) return true; } } return false; }
/** * open a gatt connection * @param c context to use for open the connection */ public void connect(Context c){ connect(c, ConnectionOption.builder().build()); }//connect
@Override public void onStateChange(Node node, State newState, State prevState) { if(newState == State.Connected && mFeatureCommand !=null) { changeNotificationStatus(mFeatureCommand,true); }//if //error during connection (connecting ->dead or connected -> dead ) if((prevState == State.Connecting || prevState== State.Connected ) && newState==State .Dead){ Log.e(TAG,"Error connecting to the node:"+node.getName()); //we disconnect -> free the gatt resource and connect again if(mConnection!=null){ mConnection.close();} cleanConnectionData(); } if(newState==State.Dead || newState==State.Disconnecting){ if(mBoundStateChange !=null) { //clean the broadcast receiver mContext.getApplicationContext().unregisterReceiver(mBoundStateChange); mBoundStateChange = null; } } }//onStateChange };
/** * 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()); }