package com.felhr.integrationapp; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.lang.ref.WeakReference; import java.util.Set; /** * Created by Felipe Herranz(felhr85@gmail.com) on 24/02/2019. */ public class MainActivity extends AppCompatActivity { /* * Notifications from UsbService will be received here. */ private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case UsbService.ACTION_USB_PERMISSION_GRANTED: // USB PERMISSION GRANTED Toast.makeText(context, "USB Ready", Toast.LENGTH_SHORT).show(); break; case UsbService.ACTION_USB_PERMISSION_NOT_GRANTED: // USB PERMISSION NOT GRANTED Toast.makeText(context, "USB Permission not granted", Toast.LENGTH_SHORT).show(); break; case UsbService.ACTION_NO_USB: // NO USB CONNECTED Toast.makeText(context, "No USB connected", Toast.LENGTH_SHORT).show(); break; case UsbService.ACTION_USB_DISCONNECTED: // USB DISCONNECTED Toast.makeText(context, "USB disconnected", Toast.LENGTH_SHORT).show(); break; case UsbService.ACTION_USB_NOT_SUPPORTED: // USB NOT SUPPORTED Toast.makeText(context, "USB device not supported", Toast.LENGTH_SHORT).show(); break; } } }; private UsbService usbService; private TextView display1, display2; private EditText editText1, editText2; private Button button1, button2; private MyHandler mHandler; private final ServiceConnection usbConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { usbService = ((UsbService.UsbBinder) arg1).getService(); usbService.setHandler(mHandler); } @Override public void onServiceDisconnected(ComponentName arg0) { usbService = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //TODO!! mHandler = new MyHandler(this); } @Override public void onResume() { super.onResume(); setFilters(); // Start listening notifications from UsbService startService(UsbService.class, usbConnection, null); // Start UsbService(if it was not started before) and Bind it } @Override public void onPause() { super.onPause(); unregisterReceiver(mUsbReceiver); unbindService(usbConnection); } private void startService(Class service, ServiceConnection serviceConnection, Bundle extras) { if (!UsbService.SERVICE_CONNECTED) { Intent startService = new Intent(this, service); if (extras != null && !extras.isEmpty()) { Set keys = extras.keySet(); for (String key : keys) { String extra = extras.getString(key); startService.putExtra(key, extra); } } startService(startService); } Intent bindingIntent = new Intent(this, service); bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE); } private void setFilters() { IntentFilter filter = new IntentFilter(); filter.addAction(UsbService.ACTION_USB_PERMISSION_GRANTED); filter.addAction(UsbService.ACTION_NO_USB); filter.addAction(UsbService.ACTION_USB_DISCONNECTED); filter.addAction(UsbService.ACTION_USB_NOT_SUPPORTED); filter.addAction(UsbService.ACTION_USB_PERMISSION_NOT_GRANTED); registerReceiver(mUsbReceiver, filter); } /* * This handler will be passed to UsbService. Data received from serial port is displayed through this handler */ private static class MyHandler extends Handler { private final WeakReference mActivity; public MyHandler(MainActivity activity) { mActivity = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { switch (msg.what) { //TODO!! } } } }