venerdì 22 maggio 2015

Creare un server PHP con Apache2 su Ubuntu 14.04 per inviare notifiche push tramite GCM ad un terminale android

Creiamo un server PHP con Apache2 su una macchina Ubuntu...


innanzitutto installare Apache2, seguendo le istruzioni di questo sito:


ricordiamoci che per il nostro esempio bisogna anche installare Curl, con il seguente comando sempre via terminale:

sudo apt-get install php5-curl


Quindi creiamo un file PHP nella nostra cartella (quella di base dovrebbe essere /var/www/ ) e lo nominiamo gcm.php

<?php
//Generic php function to send GCM push notification
   function sendMessageThroughGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "InserisciQuiLaTuaServerKey");
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }
?>
<?php

//Post message to GCM when submitted
$pushStatus = "GCM Status Message will appear here";
if(!empty($_GET["push"])) {
$gcmRegID  = file_get_contents("Id.txt");
$pushMessage = $_POST["message"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$pushStatus = sendMessageThroughGCM($gcmRegIds, $message);
}
}

//Get Reg ID sent from Android App and store it in text file
if(!empty($_GET["shareRegId"])) {
$gcmRegID  = $_POST["regId"]; 
file_put_contents("Id.txt",$gcmRegID);
echo "Done!";
exit;
}
?>
<html>
    <head>
        <title>Google Cloud Messaging (GCM) in PHP</title>
<style>
div#formdiv, p#status{
text-align: center;
background-color: #FFFFCC;
border: 2px solid #FFCC00;
padding: 10px;
}
textarea{
border: 2px solid #FFCC00;
margin-bottom: 10px;
text-align: center;
padding: 10px;
font-size: 25px;
font-weight: bold;
}
input{
background-color: #FFCC00;
border: 5px solid #fff;
padding: 10px;
cursor: pointer;
color: #fff;
font-weight: bold;
}
 
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function(){
$("textarea").val("");
});
function checkTextAreaLen(){
var msgLength = $.trim($("textarea").val()).length;
if(msgLength == 0){
alert("Please enter message before hitting submit button");
return false;
}else{
return true;
}
}
</script>
    </head>
<body>
<div id="formdiv">
<h1>Google Cloud Messaging (GCM) in PHP</h1>
<form method="post" action="/gcm/gcm.php/?push=true" onsubmit="return checkTextAreaLen()">                                                      
<textarea rows="5" name="message" cols="45" placeholder="Message to send via GCM"></textarea> <br/>
<input type="submit"  value="Invia le notifiche tramite GCM" />
</form>
</div>
<p id="status">
<?php echo $pushStatus; ?>
</p>        
    </body>

</html>

Nella stessa cartella dovrà esservi un file Id.txt dove dentro vi sarà scritto la key del device autorizzato...

venerdì 3 aprile 2015

CE Fake Reveal: sovrapporre un'immagine CE scalabile sullo sfondo della fotocamera.

All'avvio dell'applicazione si aprirà uno sfondo live della fotocamera e verrà sovrapposta una scritta CE con le misure conforme al marchio CE. Questa immagine è scalabile e con un minimo di alpha, mantenendo le proporzioni, in modo da confrontarla con l'immagine CE sull'oggetto da verificare.
Ricordarsi di aggiungere su manifest le seguenti righe per abilitare l'uso della fotocamera:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />


CE_Fake_Reveal.java :

package com.example.marco.cefakereveal;


import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.graphics.Matrix;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;


public class CE_Fake_Reveal extends Activity implements SurfaceHolder.Callback{

    ImageView img;
    private Matrix matrix = new Matrix();
    private float scale = 0.2f;
    private ScaleGestureDetector SGD;
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ce__fake__reveal);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        img = (ImageView)findViewById(R.id.imageView1);
        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        SGD = new ScaleGestureDetector(this,new ScaleListener());

        scale = 1.0f;

        img.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
        {
        updateScale();

        }
        });




     }



    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        SGD.onTouchEvent(ev);
        return true;
    }

    void updateScale()
    {
        final float imageWidth = (float)img.getDrawable().getIntrinsicWidth();
        final float imageHeight = (float)img.getDrawable().getIntrinsicHeight();
        float frameHeight = img.getHeight();
        float frameWidth = img.getWidth();

        matrix.reset();
        matrix.postTranslate(-imageWidth / 2.0f, -imageHeight / 2.0f);
        matrix.postScale(scale, scale);
        matrix.postTranslate(frameWidth / 2.0f, frameHeight / 2.0f );
        img.setImageMatrix(matrix);

    }





    private class ScaleListener extends ScaleGestureDetector.
            SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scale *= detector.getScaleFactor();
            scale = Math.max(0.03f, Math.min(scale, 0.5f));
            updateScale();
            return true;

        }
    }






/* *************************************************/
/* Ora inizia il codice che riguarda la fotocamera */
/* *************************************************/





    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
// TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                camera.setParameters(parameters);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }




}

activity_ce_fake_reveal.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <SurfaceView
        android:id="@+id/camerapreview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ImageView
        android:id="@+id/imageView1"

        android:scaleType="matrix"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:src="@drawable/ce"
        android:layout_centerInParent="true"
        android:alpha="0.4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Confrontare la scritta CE per verificarne la validità"
        android:id="@+id/textView"
        android:textStyle="bold"
        android:textColor="#fffdff12"
        android:layout_alignParentTop="true"
        android:textAlignment="center"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

lunedì 23 marzo 2015

Android Dev: preview camera liscio

activity_ce_fake_reveal.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <SurfaceView
        android:id="@+id/camerapreview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.marco.cefakereveal" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".CE_Fake_Reveal"
           android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>

</manifest>

CE_Fake_Reveal.java
package com.example.marco.cefakereveal;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class CE_Fake_Reveal extends Activity implements SurfaceHolder.Callback{

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ce__fake__reveal);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
     //   surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }



    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
// TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                camera.setParameters(parameters);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}


giovedì 30 ottobre 2014

Android dev: creare un'applicazione per telecomandare il robot aspirapolvere Samsung Navibot

Se avete uno smartphone android con porta infrarossi e un robot aspirapolvere Samsung Navibot siete a posto!

Innanzitutto i codici IRDA per comandare il Navibot:
Frequency; 38000
Header Time On: 4508
Header Time Off; 4437
Bit One on: 605
Bit One off: 1621
Bit Zero on: 605
Bit Zero off: 509
Hex Code PreData 8181

Command:
On/Off 00ff
Home 807f
Start Auto 40bf
Spot c03f
Repeat 20df
Manual Start/Stop 10ef
Avanti a05f
Sinistra 609f
Destra e01f

Altri codici da testare:

50af
d02f
30cf
b04f
708f

Questo i listati:

Irda.java:

package com.example.cristina.IRda;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.hardware.ConsumerIrManager;
import android.widget.ImageButton;
import android.os.Handler;


public class Irda extends Activity implements View.OnTouchListener {

    private Handler handler1 = new Handler();
    private Runnable runnable1 = new Runnable() {

        @Override
        public void run() {
            su();
            handler1.postDelayed(this, 1); //ripete l'operazione ogni 500 millisecondi
        }

    };

    private Handler handler2 = new Handler();
    private Runnable runnable2 = new Runnable() {

        @Override
        public void run() {
            sinistra();
            handler2.postDelayed(this, 1); //ripete l'operazione ogni 500 millisecondi
        }

    };

    private Handler handler3 = new Handler();
    private Runnable runnable3 = new Runnable() {

        @Override
        public void run() {
            destra();
            handler3.postDelayed(this, 1); //ripete l'operazione ogni 500 millisecondi
        }

    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_irda);
        ImageButton power = (ImageButton) findViewById(R.id.power);
        ImageButton home = (ImageButton) findViewById(R.id.home);
        ImageButton auto = (ImageButton) findViewById(R.id.auto);
        ImageButton max = (ImageButton) findViewById(R.id.max);
        ImageButton spot = (ImageButton) findViewById(R.id.spot);
        ImageButton up = (ImageButton) findViewById(R.id.up);
        ImageButton left = (ImageButton) findViewById(R.id.left);
        ImageButton right = (ImageButton) findViewById(R.id.right);
        ImageButton manual = (ImageButton) findViewById(R.id.manual);
        ImageButton pausa = (ImageButton) findViewById(R.id.pausa);

        up.setOnTouchListener(this);
        left.setOnTouchListener(this);
        right.setOnTouchListener(this);


        power.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 19, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23,
                        62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 19, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23,
                        62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        auto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23,
                        62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        manual.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 19, 23,
                        19, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        62, 23, 62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        spot.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        max.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 19, 23,
                        62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23,
                        62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

/*        left.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23,
                        62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23,
                        19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19,
                        23, 19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62,
                        23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });
*/

        pausa.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19,
                        23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 19, 23, 19,
                        23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 19, 23, 62, 23, 62, 23, 62,
                        23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

    }
















//************************************************************************






















    public void su() {
        ConsumerIrManager mCIR;
        mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
        int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 19, 23,
                62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
        mCIR.transmit(38000, pattern);
    }

    public void sinistra() {
        ConsumerIrManager mCIR;
        mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
        int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23,
                62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23,
                19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
        mCIR.transmit(38000, pattern);
    }

    public void destra() {
        ConsumerIrManager mCIR;
        mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
        int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19,
                23, 19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62,
                23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
        mCIR.transmit(38000, pattern);
    }


    public void start1() {
        handler1.postDelayed(runnable1, 1); // avvia il loop
    }

    public void stop1() {
        handler1.removeCallbacks(runnable1); // cancella il loop
    }

    public void start2() {
        handler2.postDelayed(runnable2, 1); // avvia il loop
    }

    public void stop2() {
        handler2.removeCallbacks(runnable2); // cancella il loop
    }

    public void start3() {
        handler3.postDelayed(runnable3, 1); // avvia il loop
    }

    public void stop3() {
        handler3.removeCallbacks(runnable3); // cancella il loop
    }


    @Override
    public void onPause() { // quando va in pausa ferma il loop
        super.onPause();
        handler1.removeCallbacks(runnable1);
        handler2.removeCallbacks(runnable2);
        handler3.removeCallbacks(runnable3);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (v.getId() == R.id.up) {
                start1();
            }

            if (v.getId() == R.id.left) {
                start2();
            }

            if (v.getId() == R.id.right) {
                    start3();
                }

            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (v.getId() == R.id.up) {
                    stop1();
                }
                if (v.getId() == R.id.left) {
                    stop2();
                }
                if (v.getId() == R.id.right) {
                    stop3();
                }
            }

                return false;
            }}

Qui invece il file Activity_irda.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000"
    android:id="@+id/dd">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Power"
        android:id="@+id/textView6"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textColor="#ffffffff"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="35dp" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/power"
        android:layout_below="@+id/textView6"
        android:layout_alignParentStart="true"
        android:background="@drawable/power"
        android:layout_marginLeft="55dp"
        android:focusableInTouchMode="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Home"
        android:id="@+id/textView7"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:textColor="#ffffffff"
        android:layout_marginTop="35dp"
        android:layout_marginRight="55dp" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/home"
        android:layout_below="@+id/textView7"
        android:layout_alignParentEnd="true"
        android:background="@drawable/home"
        android:layout_marginRight="55dp"
        android:focusableInTouchMode="false" />


    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/up"
        android:background="@drawable/up"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/left"
        android:background="@drawable/left"
        android:layout_toEndOf="@+id/auto"
        android:layout_below="@+id/up" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/right"
        android:background="@drawable/right"
        android:layout_below="@+id/up"
        android:layout_toEndOf="@+id/up" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/power"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:id="@+id/linearLayout2">

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Auto"
            android:id="@+id/textView9"
            android:textColor="#ffffffff"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Manual"
            android:id="@+id/textView10"
            android:textColor="#ffffffff"
            android:layout_alignParentStart="false"
            android:layout_alignParentBottom="false"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Spot"
            android:id="@+id/textView11"
            android:textColor="#ffffffff"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Max"
            android:id="@+id/textView8"
            android:textColor="#ffffffff"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />
    </LinearLayout>

    <ImageButton

        android:layout_height="25dp"
        android:id="@+id/auto"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_below="@+id/linearLayout2"
        android:layout_marginLeft="27dp" />

    <ImageButton
        android:layout_height="25dp"
        android:id="@+id/manual"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_alignBottom="@+id/auto"
        android:layout_toEndOf="@+id/power"
        android:layout_marginLeft="8dp" />

    <ImageButton
        android:layout_height="25dp"
        android:id="@+id/spot"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_below="@+id/linearLayout2"
        android:layout_toStartOf="@+id/home"
        android:layout_marginRight="8dp" />

    <ImageButton
        android:layout_height="25dp"
        android:id="@+id/max"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_marginLeft="25dp"
        android:layout_alignBottom="@+id/spot"
        android:layout_alignStart="@+id/textView7" />

    <ImageView
        android:layout_width="187dp"
        android:layout_height="28dp"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:background="@drawable/samsunglogo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="by Marco Giglio 2014"
        android:id="@+id/textView"
        android:textColor="#fff9f9f9"
        android:textAlignment="center"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/manual" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/pausa"
        android:background="@drawable/pause"
        android:layout_below="@+id/left"
        android:layout_toStartOf="@+id/right" />

</RelativeLayout>


Qui invece i file originali pronti da usare con Android Studio:
https://www.mediafire.com/folder/b202qgggvup4h/

Il ritorno!

Dopo mesi e mesi di inattività causa terremoto sentimentale/lavorativo/abitativo, sono tornato a dilettarmi nel mondo dell'elettronica!