Bonjour à tous,

J'essaye de faire communiquer mon android avec mon port USB (relié a une arduino), j'ai suivi les instructions de android.com mais ma variable device est toujours égale à null et donc mon application ne me demande jamais l'authorisation d'utiliser le port USB et je ne comprend pas pourquoi ma variable "device" est toujours égale à null. Avez vous une idée du probleme? Je précise que mon smartphone détecte bien quand je branche ou débranche mon USB.

Voici mon code. Je précise que je code en Kotlin.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var ACTION_USB_ATTACHED :String = “android.hardware.usb.action.USB_DEVICE_ATTACHED”
var ACTION_USB_DETACHED :String = “android.hardware.usb.action.USB_DEVICE_DETACHED”
var ACTION_USB_PERMISSION :String = “com.android.example.USB_PERMISSION”
 
class MainActivity : AppCompatActivity(){
    var switch_on_off :Switch? = null
    var tv_info : TextView? = null
 
    //USB
    var mUsbReceiver :UsbReceiver? = null
    var mfilter :IntentFilter? = null
    var mUsbManager :UsbManager? = null
    var mPermissionIntent : PendingIntent? = null
    var device :UsbDevice? = null
 
    override fun onCreate(savedInstanceState: Bundle?)  {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        tv_info = findViewById<TextView>(R.id.textView)
        switch_on_off = findViewById<Switch>(R.id.switch1)
        switch_on_off!!.setChecked(false)
        switch_on_off!!.setText("OFF")
 
        //USB
        mUsbReceiver = UsbReceiver()
        mfilter = IntentFilter()
        mfilter!!.addAction(ACTION_USB_ATTACHED)
        mfilter!!.addAction(ACTION_USB_DETACHED)
        registerReceiver(mUsbReceiver, mfilter) 
        mUsbManager = getSystemService(Context.USB_SERVICE) as UsbManager
 
        //threads
        t_switchOnOff.start()
    }
    val t_switchOnOff = thread(start = false, priority = 10)
    {
        while(true){
            Thread.sleep(300)
            this@MainActivity.runOnUiThread(java.lang.Runnable {
                if(switch_on_off!!.isChecked()){
                    switch_on_off!!.setText("ON")
                    device = intent?.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE) 
                    if(device != null) 
                    {
                        if(!mUsbManager!!.hasPermission(device)){
                            mPermissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0);
                            mUsbManager!!.requestPermission(device, mPermissionIntent);
                        }
                        else{
                            tv_info!!.setText("")
                            tv_info!!.setText("device USB detected : ${device!!.deviceName} \n
                                & Authorization accorded")
                        }
                    }
                    else{
                        tv_info!!.setText("device USB NOT detected \n& usb Detached")
                    }
                }
                else{
                    tv_info!!.setText("")
                    switch_on_off!!.setText("OFF")
                }
            })//end this@MainActivity.runOnUiThread(java.lang.Runnable ...
        }
    }//end thread
    class UsbReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) { 
            val action = intent?.action
            var ACTION_USB_PERMISSION :String = "com.android.example.USB_PERMISSION"
            if (action!!.equals(ACTION_USB_DETACHED)) {}
            if (action!!.equals(ACTION_USB_ATTACHED)){}
            if (action!!.equals(ACTION_USB_PERMISSION)){
                if (!intent!!.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) 
                {
                    //Authoization refused
                } else {
                    //Authorization accepted
                }
            }
        }
    }//end class UsbReceiver
}
Merci d'avance pour votre aide