我正在与一个 React-Native JS 开发人员团队合作,他们要求我给他们一个本地模块来监听蓝牙状态的变化。
我创建了一个接受回调的 ReactMethod:
@ReactMethod
public void registerForBlueToothStateChanges(Callback listener){
mainActivity = (MainActivity)getCurrentActivity();
mainActivity.registerBTStateChangeListener(listener);
}
此方法本质上旨在设置将由我的广播接收器使用的回调,同时将当前蓝牙状态发送回回调。一切正常,但在我的实际广播接收器中我收到错误: “回调 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);
}
任何指示我如何解决这个问题?