Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从这个JAVA文件中提取一个Delphi类用于Android?_Java_Android_Delphi - Fatal编程技术网

如何从这个JAVA文件中提取一个Delphi类用于Android?

如何从这个JAVA文件中提取一个Delphi类用于Android?,java,android,delphi,Java,Android,Delphi,我的DelphiXe7项目需要与。他们很有帮助地提供了一个Android演示,其中包括JAVA驱动程序FT311I2CInterface.JAVA(本文后面将显示)。假设我需要将此文件转换为OP,我按照说明进行必要的路径添加,以指向JDK(它似乎是由XE7安装的)SET path=%path%;C:\ProgramFiles\Java\jdk1.7.0\U 25\bin 使用同一文件夹中的所有内容运行工具时: java2op.exe -unit FT311I2CInterface.java

我的DelphiXe7项目需要与。他们很有帮助地提供了一个Android演示,其中包括JAVA驱动程序FT311I2CInterface.JAVA(本文后面将显示)。假设我需要将此文件转换为OP,我按照说明进行必要的路径添加,以指向JDK(它似乎是由XE7安装的)
SET path=%path%;C:\ProgramFiles\Java\jdk1.7.0\U 25\bin

使用同一文件夹中的所有内容运行工具时:

java2op.exe  -unit FT311I2CInterface.java
未收到任何错误,并获得了输出文件
FT311I2CInterface.java.pas
。但是,此输出文件中有一个空类,如下所示:

{*******************************************************}
{                                                       }
{           CodeGear Delphi Runtime Library             }
{ Copyright(c) 2014 Embarcadero Technologies, Inc.      }
{                                                       }
{*******************************************************}

unit FT311I2CInterface.java;

interface

uses
  Androidapi.JNIBridge;

type
// ===== Forward declarations =====


// ===== Interface declarations =====

implementation

procedure RegisterTypes;
begin
end;

initialization
  RegisterTypes;
end.
//User must modify the below package with their package name
package com.I2CDemo; 
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;
import android.widget.Toast;


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

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

    private byte [] usbdata; 
    private byte [] writeusbdata;
    private int readcount;
    private byte status;
    private byte  maxnumbytes = 60;
    public boolean datareceived = false;


    public Context global_context;

    public static String ManufacturerString = "mManufacturer=FTDI";
    public static String ModelString = "mModel=FTDII2CDemo";
    public static String VersionString = "mVersion=1.0";    

        /*constructor*/
     public FT311I2CInterface(Context context){
            super();
            global_context = context;
            /*shall we start a thread here or what*/
            usbdata = new byte[64]; 
            writeusbdata = new byte[64];

            /***********************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] = 0x34;
            writeusbdata[1] = 0x00;
            writeusbdata[2] = 0x00;
            writeusbdata[3] = 0x00;
            writeusbdata[4] = 0x00;

            /*send the packet over the USB*/
            SendPacket(5);
        }


        /*SetFrequency*/
        public void SetFrequency(byte freq)
        {
            /*check for maximum and minimum freq*/
            if(freq > 92)
                freq = 92;

            if (freq < 23)
                freq = 23;

            /*create the packet*/
            writeusbdata[0] = 0x31;
            switch(freq)
            {
            case 23:
                writeusbdata[1] = 10;
                break;
            case 44:
                writeusbdata[1] = 21;
                break;
            case 60:
                writeusbdata[1] = 30;
                break;
            default:
                writeusbdata[1] = 56;
                break;
            }
            writeusbdata[2] = 0x00;
            writeusbdata[3] = 0x00;
            writeusbdata[4] = 0x00;

            /*send the packet over the USB*/
            SendPacket(5);
        }


        /*write data*/
        public byte WriteData(byte i2cDeviceAddress,byte transferOptions,
                             byte numBytes, byte[] buffer, 
                             byte [] actualNumBytes)
        {

            status = 0x01; /*error by default*/
            /*
             * if num bytes are more than maximum limit
             */
            if(numBytes < 1){

                actualNumBytes[0] = (byte)0x00;
                /*return the status with the error in the command*/
                return status;
            }

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

            }


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

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

            }

            /*prepare the usbpacket*/
            writeusbdata[0] = 0x32;
            writeusbdata[1] = i2cDeviceAddress;
            writeusbdata[2] = transferOptions;
            writeusbdata[3] = numBytes;

            SendPacket((int)(numBytes+4));

            datareceived = false;
            /*wait while the data is received*/
            /*FIXME, may be create a thread to wait on , but any
             * way has to wait in while loop
             */
            while(true){

                if(datareceived == true){
                    break;
                }
            }

            /*by default it error*/
            status = 0x01;
            /*check for the received data*/
            if(usbdata[0] == 0x32)
            {
                /*check for return error status*/
                status = usbdata[1];


                /*updated the written bytes*/
                actualNumBytes[0] = usbdata[3];
            }
            datareceived = false;
            return status;
        }

        /*read data*/
        public byte ReadData(byte i2cDeviceAddress,byte transferOptions,
                              byte numBytes, byte[] readBuffer,
                              byte [] actualNumBytes)
        {
                status = 0x01; /*error by default*/

                /*should be at least one byte to read*/
                if(numBytes < 1){
                    return status;
                }

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


                /*prepare the packet to send this command*/
                writeusbdata[0] = 0x33; /*read data command*/
                writeusbdata[1] = i2cDeviceAddress; /*device address*/
                writeusbdata[2] = transferOptions; /*transfer options*/
                writeusbdata[3] = numBytes; /*number of bytes*/


                /*send the data on USB bus*/
                SendPacket(4);

                datareceived = false;
                /*wait for data to arrive*/
                while(true)
                {
                    if(datareceived == true){
                        break;
                    }
                }





                /*check the received data*/
                if(usbdata[0] == 0x33)
                {
                    /*check the return status*/
                    status = usbdata[1];

                    /*check the received data length*/
                    numBytes = usbdata[3];
                    if(numBytes > maxnumbytes){
                        numBytes = maxnumbytes;
                    }

                    for(int count = 0; count<numBytes;count++)
                    {
                        readBuffer[count] = usbdata[4+count];
                    }
                    /*update the actual number of bytes*/
                    actualNumBytes[0] = numBytes;
                    datareceived = false;
                }
            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();
            if(accessories != null)
            {
                Toast.makeText(global_context, "Accessory Attached", Toast.LENGTH_SHORT).show();
            }
            else
            {
                accessory_attached = false;
                return;
            }

            UsbAccessory accessory = (accessories == null ? null : accessories[0]);
            if (accessory != null) {
                if( -1 == accessory.toString().indexOf(ManufacturerString))
                {
                    Toast.makeText(global_context, "Manufacturer is not matched!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if( -1 == accessory.toString().indexOf(ModelString))
                {
                    Toast.makeText(global_context, "Model is not matched!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if( -1 == accessory.toString().indexOf(VersionString))
                {
                    Toast.makeText(global_context, "Version is not matched!", Toast.LENGTH_SHORT).show();
                    return;
                }

                Toast.makeText(global_context, "Manufacturer, Model & Version are matched!", Toast.LENGTH_SHORT).show();
                accessory_attached = true;

                if (usbmanager.hasPermission(accessory)) {
                    OpenAccessory(accessory);
                } 
                else
                {
                    synchronized (mUsbReceiver) {
                        if (!mPermissionRequestPending) {
                            Toast.makeText(global_context, "Request USB Permission", Toast.LENGTH_SHORT).show();
                            usbmanager.requestPermission(accessory,
                                    mPermissionIntent);
                            mPermissionRequestPending = true;
                        }
                }
            }
            } else {}

        }

        /*destroy accessory*/
        public void DestroyAccessory(){
            global_context.unregisterReceiver(mUsbReceiver);
            if(accessory_attached == true)
            {
                READ_ENABLE = false;
                byte i2cDeviceAddress = 1;
                byte transferOptions = bOption.START_BIT;
                byte numBytes = 2;
                byte [] actualNumBytes = new byte[1];
                byte [] readBuffer = new byte[60];
                //byte deviceAddress = 1;
                readBuffer[0] = 1;

                ReadData(i2cDeviceAddress,transferOptions,
                          numBytes, readBuffer,
                          actualNumBytes);
                try{Thread.sleep(10);}
                catch(Exception e){}
            }
            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(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))
                        {
                            Toast.makeText(global_context, "Allow USB Permission", Toast.LENGTH_SHORT).show();
                            OpenAccessory(accessory);
                        } 
                        else 
                        {
                            Toast.makeText(global_context, "Deny USB Permission", Toast.LENGTH_SHORT).show();
                            Log.d("LED", "permission denied for accessory "+ accessory);

                        }
                        mPermissionRequestPending = false;
                    }
                } 
                else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) 
                {
                        CloseAccessory();
                }else
                {
                    Log.d("LED", "....");
                }
            }   
        };


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

            handler_thread(FileInputStream stream ){
                instream = stream;
            }

            public void run()
            {

                while(READ_ENABLE == true)
                {
                    try
                    {
                        /*dont overwrite the previous buffer*/
                        if((instream != null) && (datareceived==false))
                        {
                            readcount = instream.read(usbdata,0,64);
                            if(readcount > 0)
                            {
                                datareceived = true;
                                /*send only when you find something*/   
                            }
                        }
                    }catch (IOException e){}
                }
            }
        }



    }
有人能告诉我我做错了什么吗

提供的原始JAVA文件如下所示:

{*******************************************************}
{                                                       }
{           CodeGear Delphi Runtime Library             }
{ Copyright(c) 2014 Embarcadero Technologies, Inc.      }
{                                                       }
{*******************************************************}

unit FT311I2CInterface.java;

interface

uses
  Androidapi.JNIBridge;

type
// ===== Forward declarations =====


// ===== Interface declarations =====

implementation

procedure RegisterTypes;
begin
end;

initialization
  RegisterTypes;
end.
//User must modify the below package with their package name
package com.I2CDemo; 
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;
import android.widget.Toast;


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

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

    private byte [] usbdata; 
    private byte [] writeusbdata;
    private int readcount;
    private byte status;
    private byte  maxnumbytes = 60;
    public boolean datareceived = false;


    public Context global_context;

    public static String ManufacturerString = "mManufacturer=FTDI";
    public static String ModelString = "mModel=FTDII2CDemo";
    public static String VersionString = "mVersion=1.0";    

        /*constructor*/
     public FT311I2CInterface(Context context){
            super();
            global_context = context;
            /*shall we start a thread here or what*/
            usbdata = new byte[64]; 
            writeusbdata = new byte[64];

            /***********************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] = 0x34;
            writeusbdata[1] = 0x00;
            writeusbdata[2] = 0x00;
            writeusbdata[3] = 0x00;
            writeusbdata[4] = 0x00;

            /*send the packet over the USB*/
            SendPacket(5);
        }


        /*SetFrequency*/
        public void SetFrequency(byte freq)
        {
            /*check for maximum and minimum freq*/
            if(freq > 92)
                freq = 92;

            if (freq < 23)
                freq = 23;

            /*create the packet*/
            writeusbdata[0] = 0x31;
            switch(freq)
            {
            case 23:
                writeusbdata[1] = 10;
                break;
            case 44:
                writeusbdata[1] = 21;
                break;
            case 60:
                writeusbdata[1] = 30;
                break;
            default:
                writeusbdata[1] = 56;
                break;
            }
            writeusbdata[2] = 0x00;
            writeusbdata[3] = 0x00;
            writeusbdata[4] = 0x00;

            /*send the packet over the USB*/
            SendPacket(5);
        }


        /*write data*/
        public byte WriteData(byte i2cDeviceAddress,byte transferOptions,
                             byte numBytes, byte[] buffer, 
                             byte [] actualNumBytes)
        {

            status = 0x01; /*error by default*/
            /*
             * if num bytes are more than maximum limit
             */
            if(numBytes < 1){

                actualNumBytes[0] = (byte)0x00;
                /*return the status with the error in the command*/
                return status;
            }

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

            }


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

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

            }

            /*prepare the usbpacket*/
            writeusbdata[0] = 0x32;
            writeusbdata[1] = i2cDeviceAddress;
            writeusbdata[2] = transferOptions;
            writeusbdata[3] = numBytes;

            SendPacket((int)(numBytes+4));

            datareceived = false;
            /*wait while the data is received*/
            /*FIXME, may be create a thread to wait on , but any
             * way has to wait in while loop
             */
            while(true){

                if(datareceived == true){
                    break;
                }
            }

            /*by default it error*/
            status = 0x01;
            /*check for the received data*/
            if(usbdata[0] == 0x32)
            {
                /*check for return error status*/
                status = usbdata[1];


                /*updated the written bytes*/
                actualNumBytes[0] = usbdata[3];
            }
            datareceived = false;
            return status;
        }

        /*read data*/
        public byte ReadData(byte i2cDeviceAddress,byte transferOptions,
                              byte numBytes, byte[] readBuffer,
                              byte [] actualNumBytes)
        {
                status = 0x01; /*error by default*/

                /*should be at least one byte to read*/
                if(numBytes < 1){
                    return status;
                }

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


                /*prepare the packet to send this command*/
                writeusbdata[0] = 0x33; /*read data command*/
                writeusbdata[1] = i2cDeviceAddress; /*device address*/
                writeusbdata[2] = transferOptions; /*transfer options*/
                writeusbdata[3] = numBytes; /*number of bytes*/


                /*send the data on USB bus*/
                SendPacket(4);

                datareceived = false;
                /*wait for data to arrive*/
                while(true)
                {
                    if(datareceived == true){
                        break;
                    }
                }





                /*check the received data*/
                if(usbdata[0] == 0x33)
                {
                    /*check the return status*/
                    status = usbdata[1];

                    /*check the received data length*/
                    numBytes = usbdata[3];
                    if(numBytes > maxnumbytes){
                        numBytes = maxnumbytes;
                    }

                    for(int count = 0; count<numBytes;count++)
                    {
                        readBuffer[count] = usbdata[4+count];
                    }
                    /*update the actual number of bytes*/
                    actualNumBytes[0] = numBytes;
                    datareceived = false;
                }
            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();
            if(accessories != null)
            {
                Toast.makeText(global_context, "Accessory Attached", Toast.LENGTH_SHORT).show();
            }
            else
            {
                accessory_attached = false;
                return;
            }

            UsbAccessory accessory = (accessories == null ? null : accessories[0]);
            if (accessory != null) {
                if( -1 == accessory.toString().indexOf(ManufacturerString))
                {
                    Toast.makeText(global_context, "Manufacturer is not matched!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if( -1 == accessory.toString().indexOf(ModelString))
                {
                    Toast.makeText(global_context, "Model is not matched!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if( -1 == accessory.toString().indexOf(VersionString))
                {
                    Toast.makeText(global_context, "Version is not matched!", Toast.LENGTH_SHORT).show();
                    return;
                }

                Toast.makeText(global_context, "Manufacturer, Model & Version are matched!", Toast.LENGTH_SHORT).show();
                accessory_attached = true;

                if (usbmanager.hasPermission(accessory)) {
                    OpenAccessory(accessory);
                } 
                else
                {
                    synchronized (mUsbReceiver) {
                        if (!mPermissionRequestPending) {
                            Toast.makeText(global_context, "Request USB Permission", Toast.LENGTH_SHORT).show();
                            usbmanager.requestPermission(accessory,
                                    mPermissionIntent);
                            mPermissionRequestPending = true;
                        }
                }
            }
            } else {}

        }

        /*destroy accessory*/
        public void DestroyAccessory(){
            global_context.unregisterReceiver(mUsbReceiver);
            if(accessory_attached == true)
            {
                READ_ENABLE = false;
                byte i2cDeviceAddress = 1;
                byte transferOptions = bOption.START_BIT;
                byte numBytes = 2;
                byte [] actualNumBytes = new byte[1];
                byte [] readBuffer = new byte[60];
                //byte deviceAddress = 1;
                readBuffer[0] = 1;

                ReadData(i2cDeviceAddress,transferOptions,
                          numBytes, readBuffer,
                          actualNumBytes);
                try{Thread.sleep(10);}
                catch(Exception e){}
            }
            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(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))
                        {
                            Toast.makeText(global_context, "Allow USB Permission", Toast.LENGTH_SHORT).show();
                            OpenAccessory(accessory);
                        } 
                        else 
                        {
                            Toast.makeText(global_context, "Deny USB Permission", Toast.LENGTH_SHORT).show();
                            Log.d("LED", "permission denied for accessory "+ accessory);

                        }
                        mPermissionRequestPending = false;
                    }
                } 
                else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) 
                {
                        CloseAccessory();
                }else
                {
                    Log.d("LED", "....");
                }
            }   
        };


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

            handler_thread(FileInputStream stream ){
                instream = stream;
            }

            public void run()
            {

                while(READ_ENABLE == true)
                {
                    try
                    {
                        /*dont overwrite the previous buffer*/
                        if((instream != null) && (datareceived==false))
                        {
                            readcount = instream.read(usbdata,0,64);
                            if(readcount > 0)
                            {
                                datareceived = true;
                                /*send only when you find something*/   
                            }
                        }
                    }catch (IOException e){}
                }
            }
        }



    }
//用户必须使用其包名修改下面的包
包com.I2CDemo;
导入java.io.FileDescriptor;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入android.app.Activity;
导入android.app.pendingent;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.hardware.usb.UsbAccessory;
导入android.hardware.usb.UsbManager;
导入android.os.Handler;
导入android.os.Message;
导入android.os.ParcelFileDescriptor;
导入android.util.Log;
导入android.widget.Toast;
/******************************FT311 GPIO接口类******************************************/
公共类FT311I2C接口扩展活动
{
私有静态最终字符串操作\u USB\u PERMISSION=“com.I2CDemo.USB\u PERMISSION”;
公共UsbManager UsbManager;
公共许可证;
公众悬而未决的犯罪意图;
公共ParcelFileDescriptor文件描述符;
公共文件输入流输入流;
公共文件输出流输出流;
公共布尔值mPermissionRequestPending=false;
公共布尔READ_ENABLE=true;
公共布尔值附件_attached=false;
公共处理器线程处理器线程;
专用字节[]usbdata;
私有字节[]writeusbdata;
私有整数读取计数;
专用字节状态;
专用字节maxnumbytes=60;
公共布尔值datareceived=false;
公共语境&全球语境;
公共静态字符串ManufacturerString=“mManufacturer=FTDI”;
公共静态字符串ModelString=“mModel=FTDII2CDemo”;
公共静态字符串VersionString=“mVersion=1.0”;
/*建造师*/
公共FT311I2C接口(上下文){
超级();
全局上下文=上下文;
/*我们是从这里开始还是怎么开始*/
usbdata=新字节[64];
writeusbdata=新字节[64];
/***********************USB处理******************************************/
usbmanager=(usbmanager)context.getSystemService(context.USB_服务);
//日志d(“LED”、“usbmanager”+usbmanager);
mPermissionIntent=pendingent.getBroadcast(上下文,0,新意图(操作权限),0);
IntentFilter筛选器=新建IntentFilter(操作\u USB\u权限);
filter.addAction(UsbManager.ACTION_USB_附件_分离);
registerReceiver(mUsbReceiver,filter);
inputstream=null;
outputstream=null;
}
/*重置方法*/
公共无效重置()
{
/*创建数据包*/
writeusbdata[0]=0x34;
writeusbdata[1]=0x00;
writeusbdata[2]=0x00;
writeusbdata[3]=0x00;
writeusbdata[4]=0x00;
/*通过USB发送数据包*/
发送包(5);
}
/*设定频率*/
公共无效设置频率(字节频率)
{
/*检查最大和最小频率*/
如果(频率>92)
频率=92;
中频(频率<23)
频率=23;
/*创建数据包*/
writeusbdata[0]=0x31;
开关(频率)
{
案例23:
writeusbdata[1]=10;
打破
案例44:
writeusbdata[1]=21;
打破
案例60:
writeusbdata[1]=30;
打破
违约:
writeusbdata[1]=56;
打破
}
writeusbdata[2]=0x00;
writeusbdata[3]=0x00;
writeusbdata[4]=0x00;
/*通过USB发送数据包*/
发送包(5);
}
/*写入数据*/
公共字节写入数据(字节i2cDeviceAddress、字节传输选项、,
字节数,字节[]缓冲区,
字节[]实际单位(字节)
{
默认情况下,状态=0x01;/*错误*/
/*
*如果num字节数超过最大限制
*/
如果(单位<1){
实际单位[0]=(字节)0x00;
/*返回命令中包含错误的状态*/
返回状态;
}
/*检查最大限制*/
如果(numBytes>maxnumbytes){
numBytes=最大numBytes;
}
/*准备要发送的数据包*/
对于(整数计数=0;计数最大数字节){
numBytes=最大numBytes;
}
/*准备数据包以发送此命令*/
writeusbdata[0]=0x33;/*读取数据命令*/
writeusbdata[1]=i2cDeviceAddress;/*设备地址*/
writeusbdata[2]=传输选项;/*传输选项*/
writeusbdata[3]=numBytes;/*字节数*/
/*通过USB总线发送数据*/
发送包(4);
datareceived=假;
/*w
Android.JNI.FT311I2C.pas
UsbManager := TJUsbManager.Wrap(SharedActivityContext.getSystemService(TJContext.JavaClass.USB_SERVICE));
    AccessoryList := UsbManager.getAccessoryList();
    if ( AccessoryList <> nil ) then begin
        if ( AccessoryList.Length > 0 ) then begin
            Accessory := AccessoryList.Items[0];  <<<<<<< raise an illegal instruction , access violation..
        end;
    end;
my jar is ft311lib.jar java2op -jar ft311lib.jar -unit com.poco.ft311lib