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.

Android scan does not discover any peripherals

See original GitHub issue

Version

Tell us which versions you are using:

  • react-native-ble-manager v6.6.6
  • react-native v59.9
  • Android v.8.1.0 on Nexus 6P phone

Expected behaviour

Would expect to see one or more Bluetooth peripherals to be discovered.

Actual behaviour

No peripherals are found whatsoever on Android Nexus 6P with Android 8.1.0. (Note that the ‘stopScan’ event does get triggered after the expected 5 seconds)

Works fine on iOS and on Android-based Kindle Fire HD10 7th generation.

Steps to reproduce

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

const eventEmitter = new NativeEventEmitter(NativeModules.BleManager)

export default class App extends React.Component {

  async componentDidMount() {
    
    eventEmitter.addListener(
      'BleManagerDiscoverPeripheral',
      async (args) => {
        console.log("found: " + args.id)
      }
    )

    eventEmitter.addListener(
      'BleManagerStopScan',
      () => {
        console.log("done")
      }
    )

    console.log("started")
    await BleManager.start( { forceLegacy: true } )
    console.log("scanning")
    await BleManager.scan([], 5)
  }

  render() {
    return null
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
Gra55h0ppercommented, Aug 25, 2019

Found the problem! 😃

I had missed the Note in the manual stating: “Android API >= 23 require the ACCESS_COARSE_LOCATION permission to scan for peripherals”. (and apparently, a lack of permissions does not cause the ‘scan’ method to trow an exception).

Here’s updated code with the fix:

import React from 'react'
import { PermissionsAndroid, NativeModules, NativeEventEmitter } from 'react-native'
import BleManager from 'react-native-ble-manager'

const eventEmitter = new NativeEventEmitter(NativeModules.BleManager)

async function getBluetoothScanPermission() {
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
    {
      title: 'Bluetooth Permission',
      message: 
        'In the next dialogue, Android will ask for permission for this ' +
        'App to access your location. This is needed for being able to ' +
        'use Bluetooth to scan your environment for peripherals.',
      buttonPositive: 'OK'
      },
  )
  if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
    console.log('BleManager.scan will *NOT* detect any peripherals!')
  }
}

export default class App extends React.Component {

  async componentDidMount() {
    
    eventEmitter.addListener(
      'BleManagerDiscoverPeripheral',
      async (args) => {
        console.log("found: " + args.id)
      }
    )

    eventEmitter.addListener(
      'BleManagerStopScan',
      () => {
        console.log("done")
      }
    )

    console.log("started")
    await BleManager.start( { forceLegacy: true } )
    console.log("check location access permission")
    await getBluetoothScanPermission()
    console.log("scanning")
    await BleManager.scan([], 5)
  }

  render() {
    return null
  }
}
1reaction
Gra55h0ppercommented, Aug 23, 2019

@rayjadore the scan method just starts the scan. It doesn’t return anything (other than a promise). I would expect your code to return undefined on iOS also.

Use the BleManagerDiscoverPeripheral event to report peripherals found. Also, make sure to call BleManager.start({ forceLegacy: true }) first.

If it still doesn’t work, then perhaps we face the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bluetooth LE Scanning Sometimes Doesn't Find Devices
I have a companion iOS device running similar code (both scanning for peripherals and acting as a peripheral), and when the Android scanning...
Read more >
Find BLE devices - Android Developers
To scan for only specific types of peripherals, you can instead call startScan(List<ScanFilter>, ScanSettings, ScanCallback) , providing a list ...
Read more >
Bluetooth peripheral device is not scannable by new Android ...
We discovered that if scan response is padded with 0, Android wont be able to parse it correctly. In other words, make sure...
Read more >
DA14683: iOS BLE scan cannot find advertising peripheral ...
Everything works correctly on Android. Android devices are able to scan and find the device and see the DA14683 peripheral advertisements ...
Read more >
Find & set up devices near you - Android Help
You can find and set up some devices close to you using your Android phone. ... other Android phones and tablets, or accessories...
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