THE ONE OF IoT,YOUR ONLY CHOICE!
P2P Listener
 

P2P


P2P Listner is one of Infairy broadcast channel which can get and control all the P2P device in Infairy.

Developer can listen P2P broadcast channel to get p2p devices of Infairy.
please follow the step as below:

First, import package as follows:
        import org.osgi.service.event.Event;
        import org.osgi.service.event.EventHandler;
        import com.infairy.cocina.SDK.gene.EventP2PDNA;
      
then, implement "EventHandler" in your bundle class:
        package yourBundle;

        public class CommData implements BundleActivator, EventHandler{
            ....

        }
      
Third, implement HandleEvent() method,
        public void handleEvent(Event evnt) {
          ...
        }
      
Last, do not forget listen SENSOR broadcast channel in start() method, because the hardware is a dna object,
        package yourBundle;

        public class CommData implements BundleActivator, EventHandler{
            ....

            public void start(BundleContext context) throws Exception {

                ...
                device.ListenBroadcast(context, this, device.BROADCAST_CHANNEL_P2P);

            }

        }
      
after done the process as above, you can get the data from all RXTX channel, but not every data is yours, so you have to filter the data by "RXTX_Device_Name", and then, check the data checksum value if it is necessary.
          public void handleEvent(Event evnt) {
              
            if(evnt==null) return;

            //cast EventP2PDNA from event property "BROADCAST_P2P_EVENTOBJECT"
            EventP2PDNA P2PEVT=(EventP2PDNA)evnt.getProperty(device.BROADCAST_P2P_EVENTOBJECT);
            if(P2PEVT!=null){
              String triggertype=P2PEVT.TYPE;

              //get the hardware device added nitification
              if(triggertype!=null && triggertype.equals(device.BROADCAST_P2P_HOMEAPPLIANCE_DEVICE_ADDED)){       

                ...

              }

              //get the hardware device removec nitification
              if(triggertype!=null && triggertype.equals(device.BROADCAST_P2P_HOMEAPPLIANCE_DEVICE_REMOVED)){
                ...
              }
            }
          }

      
The EventP2PDNA interface shows as below:
          /**
           * Auto start search p2p
           */
          public boolean autoStart=false;
          /**
           * P2P Object ID
           */
          public String ID="";
          /**
           * P2P Type: {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_P2POBJECT_ADDED} / {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_P2POBJECT_REMOVED}
           */
          public String TYPE="";
          
          /**
           * P2P Object Alias
           */
          public String ALIAS="";
          
          /**
           * P2P Icon name
           */
          public String IconName="";
          
          /**
           * Tip Images
           */
          public String[] TipImages=null;
          
          /**
           * Data byte array
           * 
           */
          public byte[] DATA=null;
      
Then, get the p2p device and filter you want:
  
        private void getP2PDevice(){
            ArrayList p2p=device.getP2PDevice();    
            for(int i=0; i < p2p.size(); i++){
              Object[] data=(Object[])p2p.get(i);  //get p2p object
              String id=(String)data[0];           //get id of p2p device
              String alias=(String)data[1];        //get id of p2p alias for special purpos2
              String icon=(String)data[2];         //get icon file path 
              Object p2pDevice=device.getP2PDeviceImpl(id);     
              //cast object to any object type, such as HomeApplianceDevice
              HomeApplianceInterface hai=(HomeApplianceInterface)p2pDevice;  
              .....
            }
        }