Android Open Accessory - Communicate From Fragment

287 views Asked by At

I'm currently developing USB Android Open Accessory v2.0 on API18 to communicate with microcontroller. I have successfully connected microcontroller and android when I send data from my main activity. However, when I tried to send data from fragment within my main activity the communication did not seem to establish. Following is my project structure and code:

Package Structure:

com.main
    MainActivity.java
    AOA                   -----> (Package)
       AOAInterface.java
    Fragments             -----> (Package)
       HomeFragment.java

MainActivity.java:

public class MainActivity extends Activity implements OnClickListener{

    AOAInterface mAOAMain;

    /*********************************************************************************
    * Private variables
    ********************************************************************************/

   private ImageButton Home;
   public static final int IO     = 1;
   public static final int Buzzer = 0x10;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Home = (ImageButton) this.findViewById(R.id.button_home);
      mAOAMain = new AOAInterface(this);
      Home.setOnClickListener(this);
   } //end onCreate

   @Override
   protected void onResume(){
       super.onResume();
       mAOAMain.vAOA_Resume();
   } //end onResume

   @Override
   protected void onPause(){
       super.onPause();
   } //end onPause

   @Override
   protected void onDestroy(){
       super.onDestroy();
       mAOAMain.vAOA_Destroy();
   } //end onDestroy

   @Override
   public void onClick(View v) {
       byte[] data = new byte[2];
       data[1] = 0;
       switch (v.getId()){
           case R.id.button_home:
                   data[0] = IO;
                   data[1] |= Buzzer;

                   mAOAMain.vAOA_Send(data);    
           break;
       } //end switch
   } //end onClick
}

HomeFragment.java

public class HomeFragment extends Fragment implements OnClickListener{
    AOAInterface mAOAHome;
    ImageButton  Light;
    public static final int IO     = 1;
    public static final int Buzzer = 0x10;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){

        View view = inflater.inflate(R.layout.fragment_home, container, false);

        //mAOAHome = new AOAInterface(getActivity());
        Light     = (ImageButton) view.findViewById(R.id.button_light);

        Light.setOnClickListener(this);
        return view;
    }

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        mAOAHome = new AOAInterface(activity);

    }

    @Override
        public void onClick(View v) {
        byte[] ucCommandPacket = new byte[2];
        switch(v.getId()){
            case R.id.button_light:
                    ucCommandPacket[0] = IO;
                    ucCommandPacket[1] = Buzzer;
                    mAOAHome.vAOA_Send(ucCommandPacket);
                    Log.d(TAG, "Data[0]"+ucCommandPacket[0]);
                    Log.d(TAG, "Data[1]"+ucCommandPacket[1]);
                break;
        }
    }
}

AOAInterface.java

//Class Constructor
public AOAInterface(Context context){
        super();
        GlobalContext = context;
        ucPACKET_RX   = new byte[2];
        ucPACKET_TX   = new byte[2];

        mString = context.getPackageName() + ".USB_PERMISSION";
        mAOAManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        //mAOAPermissionIntent = PendingIntent.getBroadcast(context,0, new Intent(ACTION_USB_PERMISSION),0);
        mAOAPermissionIntent = PendingIntent.getBroadcast(context,0, new Intent(mString),0);
        //IntentFilter mFilter = new IntentFilter(ACTION_USB_PERMISSION);
        IntentFilter mFilter = new IntentFilter(mString);
        mFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        context.registerReceiver(mAOAReceiver, mFilter);

        mAOAInputStream  = null;
        mAOAOutputStream = null;
    }

public void vAOA_Open(UsbAccessory vAccessory){
        mAOAFileDescriptor = mAOAManager.openAccessory(vAccessory);

        if(mAOAFileDescriptor != null){
            mAOAAccessory = vAccessory;

            FileDescriptor mFileDescriptor = mAOAFileDescriptor.getFileDescriptor();
            mAOAInputStream  = new FileInputStream(mFileDescriptor);
            mAOAOutputStream = new FileOutputStream(mFileDescriptor);

            // Check if data in streams are null
            if(mAOAInputStream == null || mAOAOutputStream == null){
                return;
            }
        }

        mAOAThread = new AOAThreadHandler(mAOAInputStream);
        mAOAThread.start();
    }

    public void vAOA_Resume(){
        if(mAOAInputStream != null && mAOAOutputStream != null){
            return;
        }

        UsbAccessory[] mAccessories = mAOAManager.getAccessoryList();
        UsbAccessory mAccessory = (mAccessories == null ? null : mAccessories[0]);
        if(mAccessory != null){
           if(mAOAManager.hasPermission(mAccessory)){
               vAOA_Open(mAccessory);
           }
            else{
                synchronized (mAOAReceiver){
                    if(!mPermissionRequestPending){
                        mAOAManager.requestPermission(mAccessory, mAOAPermissionIntent);
                        mPermissionRequestPending = true;
                    }
                }
           }
        }

    }

private final BroadcastReceiver mAOAReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String string = context.getPackageName() + ".USB_PERMISSION";
            String action = intent.getAction();
            Toast.makeText(GlobalContext, "Open AOA", Toast.LENGTH_LONG).show();

            //if(ACTION_USB_PERMISSION.equals(action)){
            if(string.equals(action)){
                synchronized (this){
                    UsbAccessory accessory = (UsbAccessory) intent
                                                     .getParcelableExtra(UsbManager.EXTRA_ACCESSORY);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(accessory != null){
                            // Call method to set up accessory communication
                            Toast.makeText(GlobalContext, "Allow USB Permission", Toast.LENGTH_LONG)
                                    .show();
                            vAOA_Open(accessory);
                            //Log.d(ac)
                        }
                    }
                    else {
                        //Log.d(TAG, "permission denied for accessory " + accessory);
                        Toast.makeText(GlobalContext, "Deny USB Permission", Toast.LENGTH_LONG)
                                .show();
                        Log.d(TAG, "Deny USB Permission");
                    }
                }
            }
            else if(UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)){
                vAOA_Close();
            }
        }
    };

Can I do the code I wrote in fragment? Or do I have to create interface and pass it to main activity before sending out through USB. Thank you.

1

There are 1 answers

0
Ahmad Ridwan Mushoffa On

I've found the solution for my problem, so I have to create interface on my fragment to pass data to activity. Then from activity initiate Android Open Accessory protocol through USB.