Skip to content

Not working listeners #108

@ssshopyak

Description

@ssshopyak
const Ibeacon = () => {
const linkvalue = items.location.identifiers.map(({ value }) => value)
const [loaded, setLoaded] = useState(true)

const requestLocationPermission = async () => {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: 'Location Permission',
        message:
          'This example app needs to access your location in order to use bluetooth beacons.',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      return true;
    } else {
      // permission denied
      return false;
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
};

const beaconSetup = async () => {
  if (isAndroid) {
    // Android
    const granted = await requestLocationPermission()
    if (granted) {
      await connect()
      await startScanning()
      console.log('finding')
    } else {
      Alert.alert(
        'Permission error',
        'Location permission not granted. Cannot scan for beacons',
        [{ text: 'OK', onPress: () => console.log('OK Pressed') }],
        { cancelable: false },
      )
    }
  } else {
    // iOS
    await init()
    await startRangingBeaconsInRegion()
  }

  // Add beacon listener
  if (isAndroid) {
    DeviceEventEmitter.addListener(
      'beaconsDidUpdate',
      ({ beacons, region }) => {
        console.log('beaconsDidUpdate', beacons, region)
        //setBeacons(beacons)
        console.log(beacons[0].uuid)
        console.log('ANDROID DISCOVERED BEACON')
        if (linkvalue.includes(beacons[0].uuid)) {
          console.log('true')
          if (items.state === 'available' && loaded) {
            api
              .post('events/subtask_started', {
                id: items.id,
              })
              .finally(() => {
                setLoaded(false)
                setIdentVisible(false)
                setBeaconVisible(false)
                stopScanning()
                getSubtask()
              })
          }
        } else {
          onToggleSnackBar('Beacon UUID неправильный')
        }
      },
    )
  } else {
    kontaktEmitter.addListener('didRangeBeacons', ({ beacons, region }) => {
      console.log('didRangeBeacons', beacons)
      console.log('IOS DISCOVERED BEACON')
      if (linkvalue.includes(beacons[0].uuid)) {
        console.log('true')
        if (items.state === 'available' && loaded) {
          api
            .post('events/subtask_started', {
              id: items.id,
            })
            .finally(() => {
              setLoaded(false)
              stopRangingBeaconsInRegion()
              setIdentVisible(false)
              setBeaconVisible(false)
              getSubtask()
            })
        }
      } else {
        onToggleSnackBar('Beacon UUID неправильный')
      }
    })
  }
}
useEffect(() => {
  Promise.resolve().then(beaconSetup);

return () => {
  // remove event listeners
  if (isAndroid) {
    kontaktEmitter.removeAllListeners('beaconsDidUpdate');
  } else {
    kontaktEmitter.removeAllListeners('didDiscoverDevices');
    kontaktEmitter.removeAllListeners('didRangeBeacons');
  }
};
}, [])

Its my code when it was on 3.1.0 version all works only on 7 android, but not on 12. Now after update to 4.0.0 its not working on 7 and 12 all permisions is allowed. It`s very important for me please may someone help me? also RN: 0.69.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions