[Unhandled Promise rejection: Typeerror: null is not an Object (evaluating 'bleManager.scan')]

Asked

Viewed 84 times

0

I have the following error in bluetooth connection with React Native, I am using the library react-native-ble-manager and the expo for the construction of this app.

The error occurs when I try to call the method startScan().

I searched some places and explained that I should delete the project folders and rebuild them. As it is a lib installation problem react-native-ble-manager. However, I could not solve based on these surveys.

Follows code below:

import BleManager from 'react-native-ble-manager';

const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);

function HomeScreen({ navigation }) {

  return (
    <View style={{ flex: 1, alignItems: 'flex-start', justifyContent: 'flex-start' }}>
      <View style={{ flex: 0.05, alignItems: 'flex-start', justifyContent: 'flex-start' }}>
      
      </View>
      <Button
        onPress={navigation.openDrawer}
        title="Open navigation drawer"
      />

      <Button title="Buscar aparelhos" onPress={() => bt.startScan() } />
    </View>
    
  );
}

Follow bluetooth connection code:

export class Blue extends Component {
  
  constructor(){
    super()

    this.state = {
      scanning:false,
      peripherals: new Map(),
      //appState: ''
    }

    this.handleDiscoverPeripheral = this.handleDiscoverPeripheral.bind(this);
    this.handleStopScan = this.handleStopScan.bind(this);
    this.handleUpdateValueForCharacteristic = this.handleUpdateValueForCharacteristic.bind(this);
    this.handleDisconnectedPeripheral = this.handleDisconnectedPeripheral.bind(this);
    this.handleAppStateChange = this.handleAppStateChange.bind(this);
  
    }
  componentDidMount() {
    AppState.addEventListener('change', this.handleAppStateChange);

    BleManager.start({showAlert: false});

    this.handlerDiscover = bleManagerEmitter.addListener('BleManagerDiscoverPeripheral', this.handleDiscoverPeripheral );
    this.handlerStop = bleManagerEmitter.addListener('BleManagerStopScan', this.handleStopScan );
    this.handlerDisconnect = bleManagerEmitter.addListener('BleManagerDisconnectPeripheral', this.handleDisconnectedPeripheral );
    this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic );



    if (Platform.OS === 'android' && Platform.Version >= 23) {
        PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
            if (result) {
              console.log("Permission is OK");
            } else {
              PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
                if (result) {
                  console.log("User accept");
                } else {
                  console.log("User refuse");
                }
              });
            }
      });
    }

  }

  startScan() {

    if (!this.state.scanning) {
      //this.setState({peripherals: new Map()});
      BleManager.scan([], 3,true)
        .then((results) => {
          console.log('Scanning...');
          this.setState({scanning:true});
        });
    }
  }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.