Grégoire Antoine
Grégoire Antoine

Reputation: 11

BleManager react-native

I've a problem with the BleManager in React-Native. I have the right import, I think

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

And I try to start the Blemanager with this command.

I tried a lot of things, but none run correctly.

I just want to start the Blemanager. I've the right build.gradle . I saw a couple of function but none of them work.

I don't know if I use the function correctly. I don't find any kind of good documentation...

import { Stylesheet, View, Text, Button, TouchableOpacity,FlatList, Alert } from 'react-native'
import donnee from '../Donnee/data'
import DataItem from './DataItem'
import FilmDetail from './FilmDetail';
import BleManager from 'react-native-ble-manager';
import React, { useState, useEffect } from 'react';
//import BleManager from 'react-native-ble-plx';

const width_proportion = '60%';
const Lwidth_proportion = '30%';
const maxwidth="100%";
const heightinside="12%";
const paddleft = '10%';



 class Search extends React.Component {

render() {
  
function scan (){
    BleManager.start({ showAlert: false }).then((value) => {
      console.log("Module initialized", value);
    });
  }
      
       return (
        <View>

          <View style={styles.entete}></View>
          <View>
            <TouchableOpacity style = {styles.filtrebtn}  onPress={() => this.props.navigation.navigate("FilmDetail")}>
              <Text style = {styles.textfiltre}>FILTRES</Text>
            </TouchableOpacity>
           
            <TouchableOpacity style = {styles.scan}  onPress={() => scan()}>
              <Text style = {styles.textscan}>SCAN</Text>
            </TouchableOpacity>
          </View>
          <View>
               
               <FlatList 
               data={donnee} 
               keyExtractor={(item) => item.id.toString()} 
               renderItem={({item}) =><DataItem donnees={item}/> }  />
          </View>
        </View>
          
        )
    }
}

export default Search

But when I run my app, it says

Promise Unhandled rejection (id: 0): TypeError: null is not an object (evaluating 'bleManager.start'

Upvotes: 0

Views: 1346

Answers (1)

Obada Kabbani
Obada Kabbani

Reputation: 11

const SearchScreen = ({navigation}) => {
const [isScanning, setIsScanning] = useState(false);

useEffect(() => {

    BleManager.start({showAlert: false});
    console.log("Module initialized");
}
  }, []);

const startScan = () => {
    if (!isScanning) {
      BleManager.scan([], 10, false)
        .then(results => {
          console.log('Scanning...');
          setIsScanning(true);
        })
        .catch(err => {
          console.error(err);
        });
      console.log('The bluetooth is already enabled or the user confirm');
    }
    navigation.navigate('homeScreen', {position: position});
  };

Upvotes: 1

Related Questions