...
Javascript objektet som injiseres har tydelig definerte interfaces, som dokumenteres i Interfaces eksempelet på InterfacesInc eksempelet på GitHub kontoen vår. Denne koden er med i appen, så du trenger ikke å laste opp denne filen når du laster opp kode til appen.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
/** * PLabBridgePLabOrientationEvent is the interfaceevent weobject havereceived forwhen communicatingphone withchanges the plaborientation app. */ interfaceclass PLabBridgePLabOrientationEvent { /** * getsalpha the- widthmagnetic wedirection have(in available to draw ondegrees) */ public int getWidth ();float alpha; /** * beta gets- the height we have available to draw ontilt front-to-back, front is positive (in degrees) */ public int getHeight ();float beta; /** * sendsgamma a- stringtilt left-to-right, aright connectedis bluetooth device. Can not send more than 20 characters. */ public void write (String string); /** * register callback function that will get the data sent from the connected device */positive(in degrees) */ float gamma; } /** * PLabAccelerationEvent is the event object received at regular intervals, telling what acceleration is */ class PLabAccelerationEvent { /** * x - acceleration in x direction */ float x; /** * y - acceleration in y direction */ float y; /** * z - acceleration in z direction */ float z; /** * timestamp - when the acceleration was meassured */ float timestamp; } /** * PLabBridge is the interface we have for communicating with the plab app. */ interface PLabBridge { /** * gets the width we have available to draw on */ public int getWidth (); /** * gets the height we have available to draw on */ public int getHeight (); /** * sends a string to a connected bluetooth device. Can not send more than 20 characters. */ public void send (String string); /** * register callback function that will get the data sent from the connected device */ public void subscribeMessages (PLabRecv sub); /** * Disconnects the device and returns to main menu */ public void disconnect(); /** * hides the back button */ public void hideBackButton(); /** * display the back button */ public void showBackButton(); /** * make the device vibrate for given amount of time. On iOS time will be ignored. */ public void vibrate(int milliseconds); /** * Listen for device orientation changes */ public void addDeviceOrientationListener(PLabOrientationListener listener); /** * remove device orientation changes listener */ public void removeDeviceOrientationListener(PLabOrientationListener listener); /** * Listen for device acceleration changes */ public void subscribeRead addDeviceAccelerationListener(PLabReadPLabAccelerationListener sublistener); /** * register callback function that will get error messages if any from the app /** * remove device acceleration changes listener */ public void subscribeError removeDeviceAccelerationListener(PLabReadPLabAccelerationListener sublistener); /** * DisconnectsSet themilliseconds devicebetween andeach returnsacceleration toupdate main menu */ public void disconnectsetDeviceAccelerationUpdateInterval(int milliseconds); } /** * A simple interface that defines the callback read function */ interface PLabReadPLabRecv { /** * The callback function. Will be called when the connected device sends something to this program. */ public void readreceive(String stringmessage); } /** * APLabOrientationListener variableis tothe holdinterface thedescribing injectedan bridgeorientation tochange the plablistener app */ private PLabBridge plabBridge; interface PLabOrientationListener { public void deviceOrientation(PLabOrientationEvent event); } /** * InjectionPLabAccelerationListener function. Needed to get is the functionalityinterface provideddescribing byan ouracceleration app. Use as setup.event listener */ voidinterface bindPLabBridgePLabAccelerationListener (PLabBridge{ bridge) { public plabBridgevoid = bridge;deviceAcceleration(PLabAccelerationEvent event); } |
Her har vi to interfaces, PLabBridge og PLabRead. PLabRead er callback funksjon når vi mottar data fra bluetooth enheten.
...