rusel1989 / react-native-bluetooth-serial

Port of https://github.com/don/BluetoothSerial for react native
476 stars 293 forks source link

Device list always returns empty #57

Closed MagganKoolman closed 7 years ago

MagganKoolman commented 7 years ago

I must have missed an essential part for this module. Whenever discoverUnpairDevices is called it returns an empty list. No error messages or warnings are thrown. I used the following code.

import BluetoothSerial from 'react-native-bluetooth-serial'

BluetoothSerial.enable()
.then((res) => console.log(res))
.catch((err) => console.log(err.message))

BluetoothSerial.discoverUnpairedDevices()
.then((unpairedDevices) => {
  console.log(unpairedDevices)
})
.catch((err) => console.log(err.message))
natrey commented 7 years ago

I was trying to run the example app on android 6.0 and did also receive an empty list. In my case, i had to add additional config lines to AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

as mentioned here

joshuapinter commented 6 years ago

More than that, you need to ask for permissions in the Activity as well. The SO answer you referenced shows how to do that.

gabrielhpugliese commented 6 years ago

@joshuapinter is it enough doing this before calling BluetoothSerial.discoverUnpairedDevices()?

    const granted = await PermissionsAndroid.requestMultiple(
      [
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
      ],
      {
        'title': 'Permission requested',
        'message': 'Give it',
      }
    );

Or should it be implemented on the java part?

EDIT: damn, worked! :D thanks

joshuapinter commented 6 years ago

@gabrielhpugliese Nice job and thanks for posting - good for everybody else landing here! 👍