0

I'm trying to create a native extension which can receive broadcasts, sent from a native android am as intent broadcasts.
The sending part works, I've tested this with a native app that has a broadcast receiver, but I cant get it to work in the native extension.

Here's what I have so far:
Here the java side of the ANE

public class ReceiverPhidget extends BroadcastReceiver {

    private FREContext mFREContext;

    public ReceiverPhidget(FREContext mFREContext) {
        this.mFREContext = mFREContext;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(IntentsKeys.INTENT_PHIDGET_CONNECTED)){
            //Send listener in ANE project with message that phidget connected (not must)
            System.out.println("Phidget connected");
            mFREContext.dispatchStatusEventAsync("Yes", Keys.KEY_CONNECTED);
        } else

        if (action.equals(IntentsKeys.INTENT_PHIDGET_DISCONNECTED)){
            //Send listener in ANE project with message that phidget disconnected (not must)
            System.out.println("Phidget disconnected");
            mFREContext.dispatchStatusEventAsync("Yes", Keys.KEY_DISCONNECTED);
        } else

        if (action.equals(IntentsKeys.INTENT_PHIDGET_GAIN_TAG)){
            //Send listener with data in ANE project with message that phidget gain receive
            String message = intent.getStringExtra(IntentsKeys.INTENT_PHIDGET_EXTRA_DATA);
            System.out.println("Phidget gain message: " + message);
            Log.d("TAG FOUND", message);
            mFREContext.dispatchStatusEventAsync(message, Keys.KEY_TAG_GAIN);
        }
    }

    public static IntentFilter getIntentFilter(){
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(IntentsKeys.INTENT_PHIDGET_CONNECTED);
        intentFilter.addAction(IntentsKeys.INTENT_PHIDGET_DISCONNECTED);
        intentFilter.addAction(IntentsKeys.INTENT_PHIDGET_GAIN_TAG);
        return intentFilter;
    }

}

And the FREExtension

public class ReceiverExtension implements FREExtension {

    private ReceiverPhidget mReceiverPhidget;
    private ReceiverExtensionContext mContext;

    @Override
    public void initialize() {
        mReceiverPhidget = new ReceiverPhidget(mContext);
        mContext.getActivity().registerReceiver(mReceiverPhidget, ReceiverPhidget.getIntentFilter());
    }

    @Override
    public FREContext createContext(String s) {
        return mContext = new ReceiverExtensionContext();
    }

    @Override
    public void dispose() {
        mContext.getActivity().unregisterReceiver(mReceiverPhidget);
    }
}

And here is the flash library side of the ANE

package nl.mediaheads.anetest.extension  {
    import flash.events.EventDispatcher;
    import flash.events.StatusEvent;
    import flash.external.ExtensionContext;

    public class RFIDController extends EventDispatcher {

        private var extContext:ExtensionContext;
        private var channel:int;
        private var scannedChannelList:Vector.<int>;

        public function RFIDController() {
            extContext = ExtensionContext.createExtensionContext(
                "nl.mediaheads.anetest.exntension.RFIDController", "");

            extContext.addEventListener(StatusEvent.STATUS, onStatus);
        }

        private function onStatus(event:StatusEvent):void {
            if (event.level == EventKeys.KEY_TAG_GAIN) {

                dispatchEvent (new TagEvent(TagEvent.TAG_GAINED, event.code) );
            }
        }
    }
}

And here is my test mobile project class to test the ANE

package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.text.TextField;
    import nl.mediaheads.anetest.extension.RFIDController;

    [SWF(width="1280", height="800", frameRate="60", backgroundColor="#ffffff")]
    public class AneTestApp extends Sprite
    {

        private var tf:TextField;
        private var rc:RFIDController;

        public function AneTestApp()
        {
            super();

            // support autoOrients
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.color = 0xFFFFFF;

            addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function onAdded(event:Event):void {


            //
            tf = new TextField();
            tf.width = 200;
            tf.height = 50;
            tf.x = 10;
            tf.y = 64;
            tf.mouseEnabled = false;
            tf.background = true;
            tf.backgroundColor = 0xF50000;
            addChild(tf);

            rc = new RFIDController();

            tf.text = "test 1";
            this.addEventListener( TagEvent.TAG_GAINED , onTagAdded);
            tf.text = "test 2";
            //
        }

        private function onTagAdded(event:TagEvent):void
        {
            tf.text = event.params;
        }

    }
}

I have signed the ANE accordingly, I also signed the test app it's self. I have a Log.d in the java part of the ANE which should pop up on log cat but it doesn't, also the textfield just becomes blank as soon as I initialized the RFIDController even without added the event listener.

If you need any more code or information to help me solve this problem feel free to ask.
I could really use some help because I'm completely lost, I've followed multiple tutorials and guide on how to do this, I should have done everything correctly, but I clearly have not.

UPDATE: 1 The extension xml

<extension xmlns="http://ns.adobe.com/air/extension/3.5"> <id>nl.mediaheads.anetest.exntension.RFIDController</id> <versionNumber>0.0.1</versionNumber> <platforms> <platform name="Android-ARM"> <applicationDeployment> <nativeLibrary>AneTest.jar</nativeLibrary> <initializer>nl.mediaheads.anetest.ReceiverExtension</initializer> <finalizer>nl.mediaheads.anetest.ReceiverExtension</finalizer> </applicationDeployment> </platform> </platforms> </extension>

UPDATE 2:

I fixed it, it was an context issue together with that flash somehow clean my custom event so I used status event to parse from the flash side of the ANE to the air application itself.

Maarten
  • 635
  • 1
  • 9
  • 29

1 Answers1

0

Currently you are creating your receiver at the initialisation point of the extension which will most likely be called before the context creation, so your context may be null at that point and causing your errors.

Try moving the creation of your ReceiverPhidget to the constructor of your ReceiverExtensionContext. Something like the following (I haven't tested this):

public class ReceiverExtensionContext extends FREContext
{
    private ReceiverPhidget mReceiverPhidget;

    public ReceiverExtensionContext()
    {
        mReceiverPhidget = new ReceiverPhidget( this );
        getActivity().registerReceiver( mReceiverPhidget, ReceiverPhidget.getIntentFilter() );
    }

    @Override
    public Map<String, FREFunction> getFunctions() 
    { 
        Map<String, FREFunction> functionMap = new HashMap<String, FREFunction>();
        return functionMap;
    }

    @Override
    public void dispose() 
    {
        getActivity().unregisterReceiver( mReceiverPhidget );
    }

}
Michael
  • 3,776
  • 1
  • 16
  • 27
  • I completely rewrote the receiver extension, the extension context, and added some function functions so it the receiver gets properly initialized. Thanks for the help either way. – Maarten Jan 26 '16 at 10:06