solinor / react-native-bluetooth-status

React-Native library to query and manage bluetooth state of the device (iOS and Android)
MIT License
102 stars 60 forks source link

BluetoothStatus.state is always false #55

Open gpawlik opened 4 years ago

gpawlik commented 4 years ago

No matter if the Bluetooth is switched on or off (both general settings and app settings) the state comes as false. Am I missing something?

import { BluetoothStatus } from 'react-native-bluetooth-status';
...
async checkBluetoothState() {
    const isEnabled = await BluetoothStatus.state();
    // false
}

When I inspect the state in the line https://github.com/solinor/react-native-bluetooth-status/blob/master/index.js#L28

this.subscription = bluetoothEvent.addListener(BT_STATUS_EVENT, state => {

state is undefined

pzehle commented 4 years ago

Library seems completely broken in my experience. iOS returns "Native module cannot be null", and Android returns always false, no matter what I do.

Did you find any workaround?

zabojad commented 3 years ago

It seems to happen on Samsung phones and tablets but not on a Google Pixel for example... Is this also what you observe?

pktippa commented 3 years ago

I started facing this issue after migrating from React Native 0.63.3 to 0.64.0

schtomas commented 3 years ago

I found a workeround for this. But its not a best solution for every app and case. But for my app as a temporary solution is acceptable. I am using this library for Android. I need to see bluetooth status of device because i need to reconnect last connected thermal printer automaticly from AsyncStorage if it was once already connected. I have the following issue: in developing mode works this package just fine but in production i am dealing with same problem as above. The bluetooth status is always false even if its on. I found out when i turn off and on manualy bluetooth on device, btStatus is updated correctly. So i decided to turn off and on bluetooth in parent component in useEffect this will reset bluetooth on device and status correctly updated. Problem is, if you have connected some other device to phone it will be disconected. Second thing is it takes some time but you can use spinner if you want until btState is not true. I am using this (React Native 0.63.3) app for Android POS terminal so i am ok with that for now. But I hope this library will be fixed or i find other solution. I am adding my code as example

` import { useBluetoothStatus } from 'react-native-bluetooth-status'; const [btStatus, isPending, setBluetooth] = useBluetoothStatus();

useEffect(() => { loadAuthData(); loadLanguageSetting();

if (!btStatus) {
  setBluetooth(false);
  setBluetooth(true);
}

if (btStatus) {
  if (printers.length === 0) {
    BLEPrinter.init()
      .then(() => {
        searchBluetoothPrinters();
      })
  };

  if (printers.length > 0 && !currentPrinter) {
    setStoredPrinter();
  };
};

if (!btStatus && printers.length > 0) {
  disconnectPrinter();
  setPrinters([]);
}

}, [printers, btStatus, isPending]); `

dmontecillo commented 3 years ago

Same issue but issue only found on Android im using below:

  1. react-native 0.64
  2. Android OS10
naingmahar commented 3 years ago

Same issue

ycldz commented 2 years ago

+1