I took your code as a starting point and also used some tips from 
StackOverflow and other sites and made a basic example of a IOIO service 
handing requests and replys from multiple activities, and also having the 
service broadcast events, like connect and disconnect and having the 
activities respond to the broadcast messages. You can get it here: 
https://github.com/ksmiller99/HelloIOIOServiceIPC 

If anyone has any suggestions on how to make it better, please reply. The 
two activities are nearly the same except that the Main starts the service. 
I did this for simplicity and clarity - even the line numbers are the same 
in the two activities.

Kevin

On Wednesday, April 11, 2012 at 12:18:33 PM UTC-4, Trevor White wrote:
>
> Hi. I am trying to get the IOIO board to run from a service and actually 
> be able to fire messages between that and the main activity. I read some 
> tutorials and kinda got it working but my understanding is still lacking. 
>
> I have attached my current code at the bottom. It is just code I am 
> playing with at the moment. I do not think it is quite right. What I would 
> like to do is send messages to the ioio service that can be sent out on a 
> usart. It is more for learning at the moment and I do not have an end goal 
> as such in mind. To start with I thought I would just toggle the LED pin 
> based on messages coming in. 
>
> The problem with the code below is that I cant handle a message and act on 
> the led directly. I have to set a 'led_state' and look for that in the main 
> loop of the service. Is there a way to have the messages know of the led 
> and act from within handleMessage code?
>
> Thanks
>
> Trev
>
> public class IOIO_Service extends IOIOService {
> private static final String TAG = "IOIOService";
> // Used to receive messages from the Activity
> final Messenger inMessenger = new Messenger(new IncomingHandler());
> // Use to send message to the Activity
> private Messenger outMessenger;
>
> public static final int LED_ON  = 1;
> public static final int LED_OFF = 2;
> public static  int led_state = LED_ON;
>
> private boolean led_changed = true;
> private Handler handler;
> @Override
> public void onCreate() {
> super.onCreate();
> }
> class IncomingHandler extends Handler {
> @Override
> public void handleMessage(Message msg) {
> Log.e("MESSAGE", "Got message");
> Bundle data = msg.getData();
>
> //String urlPath = data.getString(DownloadServiceMessenger.URLPATH);
> //String fileName = data.getString(DownloadServiceMessenger.FILENAME);
> //String outputPath = download(urlPath, fileName);
>
> Message backMsg = Message.obtain();
> if ( led_state == LED_ON ) {
> led_state = LED_OFF;
> }
> else {
> led_state = LED_ON;
> }
> led_changed = true;
> backMsg.arg1 = led_state;
> Bundle bundle = new Bundle();
> backMsg.setData(bundle);
> try {
> outMessenger.send(backMsg);
> } catch (android.os.RemoteException e1) {
> Log.w(getClass().getName(), "Exception sending message", e1);
> }
> }
> }
>
> @Override
> protected IOIOLooper createIOIOLooper() {
> return new BaseIOIOLooper() {
> private DigitalOutput led_;
> /*--- this pin indicates to the ioio monitor processor that we are alive 
> -----*/
> private DigitalOutput alive_pin;
> private Uart uart_;
> private InputStream reading_;
>
> @Override
> protected void setup() throws ConnectionLostException,
> InterruptedException {
> Log.d(TAG,"setup");
> led_ = ioio_.openDigitalOutput(IOIO.LED_PIN);
> alive_pin = ioio_.openDigitalOutput(24);
> uart_ = ioio_.openUart( 7, 10, 115200, Uart.Parity.NONE,Uart.StopBits.ONE 
> );
> reading_ = uart_.getInputStream();
> }
>
> @Override
> public void loop() throws ConnectionLostException,
> InterruptedException {
> if ( led_changed == true ) {
> if ( led_state == LED_ON ) { 
> led_.write(true); 
> Log.d(TAG, "LED_ON");
> }
> else if ( led_state == LED_OFF ){
> led_.write(false);
> Log.d(TAG, "LED_OFF"); 
> }
> led_changed = false;
> }
> Thread.sleep(50);
> //led_.write(false); 
> //Thread.sleep(500);
> }
> };
> }
>
> @Override
> public void onStart(Intent intent, int startId) {
> super.onStart(intent, startId);
> }
>
> @Override
> public IBinder onBind(Intent intent) {
> Bundle extras = intent.getExtras();
> // Get messager from the Activity
> if (extras != null) {
> outMessenger = (Messenger) extras.get("MESSENGER");
> }
> // Return our messenger to the Activity to get commands
> return inMessenger.getBinder();
> }
> }
>

-- 
You received this message because you are subscribed to the Google Groups 
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to