0

我正在尝试使用 eclipse 调试应用程序。我正在使用 USB 连接到附件,因此 USB 无法调试。我正在使用 ADB 连接进行无线调试,但是对于这个应用程序,我必须拔下并插入 USB 电缆才能运行应用程序,当我这样做时,我会失去与 Eclipse 的连接。

所以我唯一能想到的就是尝试使用 Toast 来弄清楚课堂上发生了什么。

也许有更好的方法?

但是当我尝试调用 toast 应用程序崩溃时.. 我希望能够以各种方法(如 BroadcastReceiver 和 ResumeAccessory

打电话给任何人告诉我如何在这个类中实现 toast?:

//

User must modify the below package with their package name
package com.UARTDemo; 
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.util.Log;



/******************************FT311 GPIO interface class******************************************/
public class FT311UARTInterface extends Activity
{

    private static final String ACTION_USB_PERMISSION =    "com.UARTDemo.USB_PERMISSION";
    public UsbManager usbmanager;
    public UsbAccessory usbaccessory;
    public PendingIntent mPermissionIntent;
    public ParcelFileDescriptor filedescriptor;
    public FileInputStream inputstream;
    public FileOutputStream outputstream;
    public boolean mPermissionRequestPending = true;
    public handler_thread handlerThread;

    private byte [] usbdata; 
    private byte [] writeusbdata;
    private byte  [] readBuffer; /*circular buffer*/
    private int readcount;
    private int totalBytes;
    private int writeIndex;
    private int readIndex;
    private byte status;
    private byte  maxnumbytes = (byte)64;

    public boolean datareceived = false;



        /*constructor*/
     public FT311UARTInterface(Context context){
            super();
            /*shall we start a thread here or what*/
            usbdata = new byte[64]; 
            writeusbdata = new byte[64];
            /*128(make it 256, but looks like bytes should be enough)*/
            readBuffer = new byte [maxnumbytes];


            readIndex = 0;
            writeIndex = 0;
            /***********************USB handling******************************************/

            usbmanager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
           // Log.d("LED", "usbmanager" +usbmanager);
            mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
           filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
           context.registerReceiver(mUsbReceiver, filter);

           inputstream = null;
           outputstream = null;
        }

        /*reset method*/
        public void Reset()
        {
            /*create the packet*/
            writeusbdata[0] = 0x49;
            writeusbdata[1] = 0x00;
            writeusbdata[2] = 0x00;
            writeusbdata[3] = 0x00;
            /*send the packet over the USB*/
            SendPacket(4);
        }



        public void SetConfig(int baud, byte dataBits, byte stopBits,
                             byte parity, byte flowControl)
        {

            /*prepare the baud rate buffer*/
            writeusbdata[0] = (byte)baud;
            writeusbdata[1] = (byte)(baud >> 8);
            writeusbdata[2] = (byte)(baud >> 16);
            writeusbdata[3] = (byte)(baud >> 24);

            /*data bits*/
            writeusbdata[4] = dataBits;
            /*stop bits*/
            writeusbdata[5] = stopBits;
            /*parity*/
            writeusbdata[6] = parity;
            /*flow control*/
            writeusbdata[7] = flowControl;

            /*send the UART configuration packet*/
            SendPacket((int)8);
        }


        /*write data*/
        public byte SendData(byte numBytes, char[] buffer) 

        {

            status = 0x00; /*success by default*/
            /*
             * if num bytes are more than maximum limit
             */
            if(numBytes < 1){
                /*return the status with the error in the command*/
                return status;
            }

            /*check for maximum limit*/
            if(numBytes > 64){
                numBytes = 64;

            }


            /*prepare the packet to be sent*/
            for(int count = 0;count<numBytes;count++)
            {

                writeusbdata[count] = (byte)buffer[count];

            }
            SendPacket((int)numBytes);
            return status;
        }

        /*read data*/
        public byte ReadData(byte numBytes,char[] buffer, byte [] actualNumBytes)
        {
                status = 0x00; /*success by default*/

                /*should be at least one byte to read*/
                if((numBytes < 1) || (totalBytes == 0)){
                    actualNumBytes[0] = 0x00;
                    return status;
                }

                /*check for max limit*/
                if(numBytes > 64){
                    numBytes = 64;
                }
                if(numBytes > 64){
                    numBytes = 64;
                }

                if(numBytes > totalBytes)
                    numBytes = (byte)totalBytes;



                /*update the number of bytes available*/
                totalBytes -= numBytes;

                actualNumBytes[0] = numBytes;   

                /*copy to the user buffer*/ 
                for(int count = 0; count<numBytes;count++)
                {
                    buffer[count] = (char)readBuffer[readIndex];
                    readIndex++;
                    /*shouldnt read more than what is there in the buffer,
                     *  so no need to check the overflow
                     */
                    readIndex %= maxnumbytes;
                }
                return status;
        }





        /*method to send on USB*/
        private void SendPacket(int numBytes)
        {


            try {
                if(outputstream != null){
                    outputstream.write(writeusbdata, 0,numBytes);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }




        /*resume accessory*/
        public void ResumeAccessory()
        {
            // Intent intent = getIntent();
            if (inputstream != null && outputstream != null) {
                return;
            }

            UsbAccessory[] accessories = usbmanager.getAccessoryList();
            UsbAccessory accessory = (accessories == null ? null : accessories[0]);
            if (accessory != null) {
                if (usbmanager.hasPermission(accessory)) {
                    OpenAccessory(accessory);
                } 
                else
                {
                    synchronized (mUsbReceiver) {
                        if (!mPermissionRequestPending) {
                            usbmanager.requestPermission(accessory,
                                    mPermissionIntent);
                            mPermissionRequestPending = true;
                        }
                }
            }
            } else {}

        }

        /*destroy accessory*/
        public void DestroyAccessory(){
            unregisterReceiver(mUsbReceiver);
            CloseAccessory();
        }



/*********************helper routines*************************************************/     

        public void OpenAccessory(UsbAccessory accessory)
        {
            filedescriptor = usbmanager.openAccessory(accessory);
            if(filedescriptor != null){
                usbaccessory = accessory;
                FileDescriptor fd = filedescriptor.getFileDescriptor();
                inputstream = new FileInputStream(fd);
                outputstream = new FileOutputStream(fd);
                /*check if any of them are null*/
                if(inputstream == null || outputstream==null){
                    return;
                }
            }

            handlerThread = new handler_thread(handler, inputstream);
            handlerThread.start();
        }

        private void CloseAccessory()
        {
            try{
                if(filedescriptor != null)
                    filedescriptor.close();

            }catch (IOException e){}

            try {
                if(inputstream != null)
                        inputstream.close();
            } catch(IOException e){}

            try {
                if(outputstream != null)
                        outputstream.close();

            }catch(IOException e){}
            /*FIXME, add the notfication also to close the application*/

            filedescriptor = null;
            inputstream = null;
            outputstream = null;

            System.exit(0);

        }


        /***********USB broadcast receiver*******************************************/
        private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() 
        {
            @Override
            public void onReceive(Context context, Intent intent) 
            {
                String action = intent.getAction();
                if (ACTION_USB_PERMISSION.equals(action)) 
                {
                    synchronized (this)
                    {
                        UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
                        {
                            OpenAccessory(accessory);
                        } 
                        else 
                        {
                            Log.d("LED", "permission denied for accessory "+ accessory);

                        }
                        mPermissionRequestPending = false;
                    }
                } 
                else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) 
                {
                    UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    if (accessory != null )//&& accessory.equals(usbaccessory))
                    {
                        CloseAccessory();
                    }
                }else
                {
                    Log.d("LED", "....");
                }
            }   
        };





        final Handler handler =  new Handler()
        {
            @Override 
            public void handleMessage(Message msg)
            {
                for(int count = 0;count<readcount;count++){

                    readBuffer[writeIndex] = usbdata[count];
                    /*move to the next write location*/
                    writeIndex++;
                    writeIndex %= maxnumbytes;
                    /*FIXME,check for overflow*/
                    //if(writeIndex == readIndex){

                    //}

                }

                /*caluclate the available bytes to read*/
                if(writeIndex >= readIndex)
                    totalBytes = writeIndex-readIndex;
                else
                    totalBytes = (maxnumbytes-readIndex)+writeIndex;


            }

        };


        /*usb input data handler*/
        private class handler_thread  extends Thread {
            Handler mHandler;
            FileInputStream instream;

            handler_thread(Handler h,FileInputStream stream ){
                mHandler = h;
                instream = stream;
            }

            public void run()
            {

                while(true)
                {
                    Message msg = mHandler.obtainMessage();
                    try{
                        if(instream != null)
                        {   
                        readcount = instream.read(usbdata,0,64);
                        if(readcount > 0)
                        {
                            datareceived = true;
                            msg.arg1 = usbdata[0];
                            msg.arg2 = usbdata[1];
                        }
                        mHandler.sendMessage(msg);
                        }
                        }catch (IOException e){}
                }
            }
        }



    }
4

1 回答 1

0

它更好地使用Log.d(TAG, text)

或者

在任何活动子类中,您都可以:

Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();

于 2013-02-14T21:45:46.317 回答