我目前正在API18上开发USB Android Open Accessory v2.0以与微控制器进行通信。当我从主要活动发送数据时,我已经成功连接了微控制器和android。但是,当我尝试从主要活动中的片段发送数据时,通信似乎没有建立。以下是我的项目结构和代码:
包装结构:
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();
}
}
};
我可以使用片段编写的代码吗?或者我必须创建界面并在通过USB发送之前将其传递给主要活动。谢谢。
答案 0 :(得分:0)
我找到了问题的解决方案,所以我必须在我的片段上创建接口以将数据传递给活动。然后从活动通过USB启动Android Open Accessory协议。