使用React-Native - Android收听BlueTooth状态更改

时间:2016-07-06 19:11:02

标签: react-native-android

我正在与一群React-Native JS开发人员合作,他们让我给他们一个本机模块来监听蓝牙状态的变化。

我创建了一个带回调的ReactMethod:

 @ReactMethod
public void registerForBlueToothStateChanges(Callback listener){
    mainActivity = (MainActivity)getCurrentActivity();
    mainActivity.registerBTStateChangeListener(listener);
}

此方法主要用于设置我的广播接收器将使用的Callback,同时将当前蓝牙状态发送回回调..这一切都正常,但在我的Actual BroadCast Receiver中,我得到了错误:"回调registerForBlueToothStateChanges()存在于模块TripManager中,但只有一个回调可能注册到本机模块中的函数"

主要活动相关代码:

@Subscribe
    public void onBluetoothStateChanged(BluetoothStateChange state){
        //check against current state to avoid duplicate offs
        Log.v(TAG, "STATE CHANGED WOOT");
        if(!state.equals(lastBluetoothState)){
            lastBluetoothState = state;
            Log.v(TAG, "BT STATE CHANGE***: "+state +" "+state.equals(BluetoothStateChange.ON));
            if(blueToothStateListener != null) {
                int i = 0;
                if (state.equals(BluetoothStateChange.ON)) {
                    i = 1;
                }
                sendBTStateChange(i);
            }
        }
        lastBluetoothState = state;
    }

    public void registerBTStateChangeListener(Callback listener){
        blueToothStateListener = listener;
        int i = 0;
        if(lastBluetoothState.equals(BluetoothStateChange.ON)){
           i = 1;
        }
        sendBTStateChange(i);
    }

    private void sendBTStateChange(int code){
        blueToothStateListener.invoke(code);
    }

任何指示我如何解决这个问题?

0 个答案:

没有答案