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

标签 android callback bluetooth codenameone

我正在使用从 Codenameone 调用的 native Android 代码。我正在发现蓝牙设备,连接到它们等等。但是,我使用了像 bluetoothDevices 这样的局部变量来设置这些由 StateMachine 调用的 Activity 的返回值。代码工作正常,但我想用回调方法替换它们。我该怎么做?

执行发现的 PrinterNativeCallsImpl.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 版本来说是非常微不足道的。

例如在你的 Codename One src 目录中(在正确的包下):

public class BTCallbacks {
    public static void deviceDiscovered(String name) {
        // handle this in code, keep in mind its the Android thread so make sure to use callSerially()!
    }
}

然后在原生代码中做:

BTCallbacks.deviceDiscovered(...);

对于 iOS 版本,您将需要使用有些未记录的 XMLVM 调用约定来调用此类静态方法,但它应该很容易实现。

关于android - 使用回调替换codenameone中的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20230708/

相关文章:

java - 如何将参数(用户名和密码)传递给函数signinactivity

android - 如何在Android模拟器中显示通知栏

java - 为什么这个方法必须是回调方法

javascript - javascript 中回调的多次返回

java - java中的回调是什么?

iOS蓝牙后台模式

android - 如何通过更改文件来切换手机的蓝牙状态?

java - 无法启动 Activity ComponentInfo : Error inflating class android. support.design.widget.CoordinatorLayout

安卓蓝牙连接问题

java - 使用proGuard时如何保持java.lang.reflect