Android 使用回调替换codenameone中的局部变量

Android 使用回调替换codenameone中的局部变量,android,callback,bluetooth,codenameone,Android,Callback,Bluetooth,Codenameone,我使用的是从Codenameone调用的本地Android代码。我发现蓝牙设备,连接到它们等等。然而,我使用了局部变量(如bluetoothDevices)来设置由StateMachine调用的这些活动的返回值。代码运行良好,但我想用回调方法替换它们。我该怎么办 执行发现的printernavivecallsiml.java,依此类推 public class PrinterNativeCallsImpl { //#ifndef REMOVE_DEBUG private static f

我使用的是从Codenameone调用的本地Android代码。我发现蓝牙设备,连接到它们等等。然而,我使用了局部变量(如bluetoothDevices)来设置由StateMachine调用的这些活动的返回值。代码运行良好,但我想用回调方法替换它们。我该怎么办

执行发现的printernavivecallsiml.java,依此类推

public class PrinterNativeCallsImpl {
//#ifndef REMOVE_DEBUG
    private static final Logger logger = LoggerFactory.getLogger(PrinterNativeCallsImpl.class);
//#endif
    // ???? WANT TO AVOID DOING THIS
    public String bluetoothDevices = "";
    public String selprinter = "";
    public String errorMsg = "";
    private int result = 9;
    public String printerInfo = "";

    public void connect(String printer){

        final Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity();
        final Intent connectIntent = new Intent(ctx, BluetoothFilePrinter.class);

        result = PrinterNativeCalls.PENDING;

        Bundle bundle = new Bundle();
        bundle.putString(BTWrapperActivity.EXTRA_DEVICE_ADDRESS, printer);
        bundle.putBoolean("IS_CONNECTED", false);
        bundle.putInt(BluetoothFilePrinter.REQUEST_CODE, BluetoothFilePrinter.CONNECT_REQUEST);
        connectIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        connectIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        connectIntent.putExtras(bundle);
        startActivityForResult(connectIntent, BluetoothFilePrinter.CONNECT_REQUEST, new IntentResultListener(){

            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (data == null) {
                    data = connectIntent;
                }
                if (requestCode == BluetoothFilePrinter.CONNECT_REQUEST) {
                    if (resultCode == Activity.RESULT_OK) {
                        selprinter = data.getExtras().getString(BTWrapperActivity.EXTRA_DEVICE_ADDRESS);
                        result = PrinterNativeCalls.OK;
                    } else {
                        result = PrinterNativeCalls.ERROR;
                        errorMsg = data.getExtras().getInt(BluetoothFilePrinter.ERROR) 
                                +" - "+data.getExtras().getString(BluetoothFilePrinter.ERROR_MSG);
                    }
                }
            }
        });
   }

    /**
     * Start an intent for result
     */ 
    public static void startActivityForResult(Intent intent, int actRequestCode, final IntentResultListener listener){
        final com.codename1.impl.android.CodenameOneActivity act = (com.codename1.impl.android.CodenameOneActivity) com.codename1.impl.android.AndroidNativeUtil.getActivity();
        act.startActivityForResult(intent, actRequestCode);
        act.setIntentResultListener(new IntentResultListener() {

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                listener.onActivityResult(requestCode, resultCode, data);
                act.restoreIntentResultListener();
            }
        });
    }


    public int getResult() {
        return result;
    }

    public String getSelprinter() {
        return selprinter;
    }

    public String getErrorMsg(){
        return errorMsg;
    }

    public boolean isSupported() {
        return true;
    }

    public String getPrinterStatus() {
        return printerInfo;
    }

    public String getBluetoothDevices() {
        return bluetoothDevices;
    }

    public void discoverDevices(){
        //showDlg();
        final Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity();
        final Intent discIntent = new Intent(ctx, BluetoothFilePrinter.class);

        result = PrinterNativeCalls.PENDING;

        Bundle bundle = new Bundle();
        bundle.putInt(BluetoothFilePrinter.REQUEST_CODE, BluetoothFilePrinter.DISCOVER_REQUEST);
        discIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        discIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        discIntent.putExtras(bundle);
        startActivityForResult(discIntent, BluetoothFilePrinter.DISCOVER_REQUEST, new IntentResultListener(){

            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (data == null) {
                    data = discIntent;
                }
                if (requestCode == BluetoothFilePrinter.DISCOVER_REQUEST) {
                    if (resultCode == Activity.RESULT_OK) {
                        bluetoothDevices = data.getExtras().getString(BTWrapperActivity.DEVICES_DISCOVERED);
                        result = PrinterNativeCalls.OK;
                    } else {
                        result = PrinterNativeCalls.ERROR;
                        errorMsg = data.getExtras().getInt(BluetoothFilePrinter.ERROR) 
                                +" - "+data.getExtras().getString(BluetoothFilePrinter.ERROR_MSG);
                    }
                }
            }
        });

    }


}
使用它的StateMachine的示例代码:

private void discoverBTDevices(){

    final PrinterNativeCalls mPrinter = (PrinterNativeCalls) NativeLookup.create(PrinterNativeCalls.class);
    isPrinterMode = true;
    final Dialog okDialog = (Dialog) createContainer(fetchResourceFile(), "OkDialog");

    new Thread() {
        public void run() {

            mPrinter.discoverDevices();
            while (mPrinter.getResult() == PrinterNativeCalls.PENDING) {
                try {
                    sleep(100);
                } catch (InterruptedException ex) {
                    // Ignore interrupted exception
                }
            }
            int result = mPrinter.getResult();

            if (result == PrinterNativeCalls.ERROR) {
                String errMsg = mPrinter.getErrorMsg();
                if (errMsg!=null && errMsg.length()>=3) {
                    addGreenRedTitle(okDialog, false, errMsg);
                }else{
                    addGreenRedTitle(okDialog, false, "DISCOVERY did not complete due to an error. Please retry");
                }
                isConnected = false;
            } else if (result == PrinterNativeCalls.PENDING) {
                //Do nothing
            } else if (result == PrinterNativeCalls.OK) {
                final String devices = mPrinter.getBluetoothDevices();
                devicesVect = getAllDevices(devices);
                showFormInEDT("currency", null);

            }
        } // eof run
    }.start();


}

如何使用回调来避免mPrinter.getBluetoothDevices()类调用?

我建议在Codename One src目录中添加一个静态类,然后只调用该类上的静态方法。对于Android版本来说,这是非常简单的

例如,在您的代号中,有一个src目录(在正确的软件包下):

然后在本机代码中执行以下操作:

BTCallbacks.deviceDiscovered(...);
对于iOS版本,您需要使用某种程度上没有文档记录的XMLVM调用约定来调用这样一个静态方法,但它应该很容易实现

BTCallbacks.deviceDiscovered(...);