question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not finding any device...

See original GitHub issue

Hi, I’ve tried this plugin, but it doesn’t work on my phone (Huawei P9 Lite, Android 7). I checked out all the possible problems but I’m not able to find out where the bug could be. This is my app code:

import React, { Component } from 'react';
import { DrawerLayoutAndroid, AppRegistry, Text, View, Alert, StatusBar, TouchableHighlight, ToolbarAndroid, Image, ScrollView, Button, ActivityIndicator,
Platform, PermissionsAndroid, NativeAppEventEmitter } from 'react-native';
import { StackNavigator, } from 'react-navigation';
import { OneBtnMode } from './screens/oneBtnMode';
import { drawerStyles, mainStyles } from './styles/common.styles';
import { homeStyles } from './styles/home.styles';
import { CommonBars } from './common/common.components';
import { evaluateActionBarTitle } from './common/common.functions';
import { BluetoothService } from './services/bluetooth.service';

export class AppHome extends React.Component {

    constructor(props) {
        super(props);
        // ricevo l'emitter e gli aggiungo il listener per controllare lo stato on/off del ble == e altri eventi
        this.bleStateHandler = NativeAppEventEmitter.addListener('BleManagerDidUpdateState', this.handleEnablingBluetooth.bind(this));
        this.bleDiscoveredPer = NativeAppEventEmitter.addListener('BleManagerDiscoverPeripheral', this.handleNewDevice.bind(this));
        this.bleScanEnded = NativeAppEventEmitter.addListener('BleManagerStopScan', this.handleScanEnded.bind(this));

        // stato inizialmente nullo
        this.state = {
            blthOn: undefined,
            loading: false,
        };
        // controllo lo stato iniziale del bluetooth -- handleEnablingBluetooth gestirà l'evento
        BluetoothService.getStatus();
    }

    componentDidMount() {
        // inizializzo il modulo bluetooth una volta per tutte
        BluetoothService.initialize();
        // richiesta permessi localizzazione
        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.requestPermission(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
                        if (result) {
                            Alert.alert("User accept");
                        } else {
                            Alert.alert("User refuse");
                        }
                    });
                }
            });
        }
    }

    static navigationOptions = {
        drawerLabel: 'Connect devices',
        drawerIcon: ({ tintColor }) => (
            <Image
                source={require('./images/homeIcon.png')}
                style={[drawerStyles.buttonIcon, { tintColor: tintColor }]}
            />
        ),
    };

    render() {
        const { navigate } = this.props.navigation;
        const { state } = this.props.navigation;

        return (
            <View style={{ flex: 1 }}>
                {/*Passo il navigatore alla barra principale in modo poi da poter aprire il drawer*/}
                <CommonBars navigate={navigate} title={evaluateActionBarTitle(state.routeName)} />
                <View style={homeStyles.mainPage}>
                    <View style={homeStyles.upContent}>
                        {/*Se lo stato indica che il bluetooth è spento allora mostrera l'immagine BN altrimenti quella blu*/}
                        <Image source={this.state.blthOn ? require('./images/blthOn.png') : require('./images/blthOff.png')} style={homeStyles.upContentImage} />
                        <View style={homeStyles.upContentButtons}>
                            {/*Stile e funzione determinati dallo stato del ble*/}
                            <TouchableHighlight underlayColor="#64B5F6" style={!this.state.blthOn ? homeStyles.upContentSingleBtn : homeStyles.upContentSingleBtnOff} onPress={this.state.blthOn ? null : this.enableBletooth}>
                                <View style={homeStyles.singleBtnContent}>
                                    <Image style={homeStyles.singleBtnIcon} source={require('./images/enable.png')} />
                                    <Text style={homeStyles.singleBtnText}>Enable bluetooth</Text>
                                </View>
                            </TouchableHighlight>
                            <TouchableHighlight underlayColor="#64B5F6" style={!this.state.loading && this.state.blthOn ? homeStyles.upContentSingleBtn : homeStyles.upContentSingleBtnOff} onPress={this.state.loading  && this.state.blthOn ? null : this.initScan.bind(this)}>
                                <View style={homeStyles.singleBtnContent}>
                                    {this.state.loading ? <ActivityIndicator color='white' /> : <Image style={homeStyles.singleBtnIcon} source={require('./images/scan.png')} />}
                                    <Text style={homeStyles.singleBtnText}>Scan</Text>
                                </View>
                            </TouchableHighlight>
                        </View>
                    </View>
                    <View style={homeStyles.downContent}>
                    </View>
                </View>
            </View>
        );

    }

    // FUNZIONI DEI BOTTONI
    // Funzione di abilitazione del BLE
    enableBletooth() {
        BluetoothService.enableBlth().then(() => {
        }).catch(() => {
            Alert.alert("Bluetooth", "Bluetooth off!");
        })
    };

    initScan() {
        BluetoothService.initScan().then(() => {
            console.log("Scanning....");
            this.setState(previousState => {
                // scan STARTED!
                return { loading: true };
            });
        })
    };

    // EVENT LISTENERS ====================================================
    // gestore eventi di cambiamento del bluetooth
    handleEnablingBluetooth(args) {
        if (args.state == 'on') {
            this.setState(previousState => {
                return { blthOn: true };
            });
        }
        else {
            this.setState(previousState => {
                return { blthOn: false };
            });
        }
    }

    // gestione evento di fine scansione
    handleScanEnded(args) {
        this.setState(previousState => {
            // scan ENDED!
            return { loading: false };
        });
        console.log(BluetoothService.getDevices());
    }

    handleNewDevice(peripheral) {
        console.log(peripheral);
        Alert.alert("New BLE device", "ID: " + peripheral.id + "\nNAME: " + peripheral.name);
    }
}

And this is the service class:

import BleManager from 'react-native-ble-manager';
import { Alert, NativeEventEmitter, NativeModules } from 'react-native';

const BleManagerModule = NativeModules.BleManager;

export class BluetoothService {
    static initialize() {
        // inizlalizzazione del modulo del bluetooth
        BleManager.start({ showAlert: true, forceLegacy: true }).then(() => {
            console.log("Module initialized");
        });
    }

    static enableBlth() {
        return BleManager.enableBluetooth();
    }

    static initScan() {
        return BleManager.scan([], 30, true);
    }

    static getDevices() {
        return BleManager.getDiscoveredPeripherals([]);
    }

    static getStatus() {
        BleManager.checkState()
    }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
anwarhamrcommented, Feb 27, 2019

I have an app that has been working for months up until this week or last. I run on multiple devices from android to ios. Running on a onePlus 3T device it stopped working. Scans return nothing. I’ve since tried on the onePlus 5T which also does not work. It is running on Samsung S5, S8; the app works as expected. I’ve also run it on the Pixel 3, this it also works. Do you have any idea how I can track this down?
I was running I was running ble-mgr 6.2x and have since upgraded to latest with nothing different than what is mentioned above. I’ll be back next week to handle any thoughts or responses. Thanks in advance.

0reactions
jiten23commented, Aug 25, 2018

hey I implemented ble manager. but not scan my bluetooth connected device. I am using my car bluetooth. have any solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bluetooth's not working in Windows 10/11: How to fix?
Bluetooth not connecting – If Bluetooth does not seem to connect, the problem has probably something to do with your Wi-Fi. In some...
Read more >
Windows 10 not detecting Bluetooth device
Method 1: Run the Hardware and Devices Troubleshooter. 1. Press Windows Key > type "Troubleshoot" > Click Troubleshoot > Under Find and fix ......
Read more >
[Fixed] Bluetooth not detecting devices on Windows 10
Fix 1: Remove all Bluetooth devices · 1) On your keyboard, press the Windows Logo Key. · 2) Type control panel, then hit...
Read more >
Fix Bluetooth Not Detecting Devices on Windows 10 in 9 Steps
How can I fix Bluetooth if it's not finding devices on Windows 10? · 1. Add the Bluetooth device again · 2. Run...
Read more >
How to Fix Bluetooth Pairing Problems - Techlicious
Some devices have smart power management that may turn off Bluetooth if the battery level is too low. If your phone or tablet...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found