Closed blood-romantic closed 4 months ago
Hi @blood-romantic, can you share the whole class component? I want to know:
From what I checked, if you change permissions when the app is on the background this listener won't trigger the event - it will only work if the app is in the foreground.
@intent-kacper-cyranowski
If I disallow the blue tooth permission on the app, the onstatechange will never be triggered whatever the blue tooth on iPhone is toggled to PowerOn or PowerOff
import PropTypes from 'prop-types';
import MicroPaymentsAPI from '../MicroPaymentsAPI/index';
import Icon from 'react-native-vector-icons/MaterialIcons';
import Spinner from 'react-native-spinkit';
import {ble} from '../BLEManager/index';
import {scanForMachines} from '../BLEManager/discoveryManager';
import {
scanningMessages as scanMsg,
cardMessages,
loginMessages,
noRoomsMessages,
genericErrorMessage,
serverDownMessages,
sessionMessages,
normalMessages,
} from '../constants/copy';
import STYLES, {COLORS, navStyling} from '../styles/index';
import {BLE_STATE} from '../BLEManager/discoveryManager';
import {
clearTimer,
checkCurrentSession,
} from '../utilities';
import {showSupportRequestAlert} from '../navUtils';
// import {logCustom} from '../fabricUtils';
import {
captureException,
} from '../sentryUtils';
import PushNotification from 'react-native-push-notification';
import WC from '../WaveController';
// import {logFirebaseEvent} from '../loggingUtils';
import Rate, {AndroidMarket} from 'react-native-rate';
// import {captureFirebaseEvent} from '../firebaseUtils';
// import perf from '@react-native-firebase/perf';
import {connect} from 'react-redux';
import {
getUser,
getUserData,
getUserLocationData,
logout,
updateUserData,
updateUserBalance,
getUserRooms,
updateUserRoom,
updateUserLocation,
resetDifferentUser,
} from '../redux/reducers/user';
import TCPError from '../MicroPaymentsAPI/tcpError';
import * as CONSTANTS from '../constants/ble';
import {SCREENS} from '../Chores/Routes';
/*
The screen that manages a displayed `MachineList` component, the user's room selection,
scanning lifecycle for machines, and the states of the bluetooth status, scanning status,
and room (controller ID) filter status.
*/
class ScanningScreen extends Component {
static propTypes = {
componentId: PropTypes.node,
getUser: PropTypes.func,
getUserData: PropTypes.func,
logout: PropTypes.func,
updateUserData: PropTypes.func,
session: PropTypes.object,
notificationsOn: PropTypes.bool,
carrierId: PropTypes.string,
userName: PropTypes.string,
deviceModel: PropTypes.string,
controllerId: PropTypes.string,
availableRooms: PropTypes.array,
selectedRoom: PropTypes.string,
};
static options = {
statusBar: {
backgroundColor: COLORS.BLUE,
style: 'light',
},
};
constructor(props) {
super(props);
this.AppStateSubscription = null;
this.NavigationFocusEventSubScription = null;
this.NavigationBlurEventSubScription = null;
this.backHandler = null;
this.bleStateHandler = null;
this.state = {
bleState: BLE_STATE.POWERED_OFF,
};
this.trace = null;
this.needRetryGetRooms = true;
this.needRetryGetInfo = true;
}
// --------- LIFE CYCLE METHODS ---------------
// ********************************************
_handleBackPress = () => {
BackHandler.exitApp();
};
async componentDidMount() {
this.bleStateHandler = ble.onStateChange(state => {
console.log('__BLE-STATE__', state);
if (state === 'PoweredOn') {
this.setState({
bleState: BLE_STATE.POWERED_ON,
});
} else if (state === 'PoweredOff') {
this.setState({
bleState: BLE_STATE.POWERED_OFF,
});
} else if (
Platform.OS === 'android' &&
state !== 'PoweredOn' &&
state !== 'PoweredOff'
) {
this.setState({grantedBluetooth: false});
}
}, true);
// Step 7: Set app event listener for app changes
}
async componentWillUnmount() {
this.AppStateSubscription?.remove();
this.NavigationFocusEventSubScription?.remove();
this.NavigationBlurEventSubScription?.remove();
this.bleStateHandler?.remove();
clearTimer(this);
await this.trace.stop();
}
render() {
...
}
const mapStateToProps = state => {
return {
session: state.userReducer.session,
user: state.userReducer.user,
mfaAuthenticated: state.userReducer.mfaAuthenticated,
appUserId: state.userReducer.appUserId,
notificationValue: state.userReducer.notificaitonValue,
notificationText: state.userReducer.notificationText,
mfaSystemAvailable: state.userReducer.mfaSystemAvailable,
mfaUserEnrolled: state.userReducer.mfaUserEnrolled,
mfaSessionValid: state.userReducer.mfaSessionValid,
mfaRequiredFlag: state.userReducer.mfaRequiredFlag,
mfaUserId: state.userReducer.mfaUserId,
mfaEmailFactorId: state.userReducer.mfaEmailFactorId,
mfaEmail: state.userReducer.Email,
mfaPhoneFactorId: state.userReducer.mfaPhoneFactorId,
mfaPhone: state.userReducer.mfaPhone,
mfaPhoneStatus: state.userReducer.mfaPhoneStatus,
mfaEmailStatus: state.userReducer.mfaEmailStatus,
notificationsOn: state.userReducer.notificationsOn,
carrierId: state.userReducer.carrierId,
userName: state.userReducer.userName,
waveController: state.userReducer.waveController,
existLocationData: state.userReducer.existLocationData,
locationId: state.userReducer.locationId,
deviceModel: state.appReducer.deviceModel,
availableRooms: state.userReducer.availableRooms,
selectedRoom: state.userReducer.selectedRoom,
controllerId: state.userReducer.controllerId,
};
};
const mapDispatchToProps = {
getUser,
getUserData,
getUserLocationData,
logout,
updateUserData,
updateUserBalance,
getUserRooms,
updateUserRoom,
updateUserLocation,
resetDifferentUser,
};
export default connect(mapStateToProps, mapDispatchToProps)(ScanningScreen);
As I mentioned, while you are in settings, your app is in background. It’s not possible to handle events in background as the app execution is stopped. If you want to handle this case you could use AppState and manually read the state when app enters the foreground
Hi @blood-romantic Do you still need help with the issue?
Prerequisites
Expected Behavior
The onstatechange callback is fired even if the blue permission is disabled
Current Behavior
The onstatechange was not fired if the app's blue permission was disabled
Library version
2.0.3
Device
Iphone14 IOS 16.0.3
Environment info
Steps to reproduce
Formatted code sample or link to a repository
Relevant log output
Additional information
No response