我目前正在 API18 上开发 USB Android Open Accessory v2.0 以与微控制器通信。当我从主要活动发送数据时,我已成功连接微控制器和安卓。但是,当我尝试从主要活动中的片段发送数据时,似乎没有建立通信。以下是我的项目结构和代码:
封装结构:
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;
}
}
}
AOA接口.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();
}
}
};
我可以做我在片段中编写的代码吗?或者我必须创建接口并将其传递给主要活动,然后再通过 USB 发送出去。谢谢你。