简体   繁体   English

在 ios 设备上使用 react-native 检测 ibeacon

[英]Detecting ibeacon with react-native on ios device

I have an IoT device which is sending iBeacon advertisements and want to detect these signals on my react-native application.我有一个物联网设备正在发送 iBeacon 广告,并希望在我的 react-native 应用程序上检测这些信号。

I found this library react-native-kontaktio.我找到了这个库 react-native-kontaktio。 It works fine on android, but on ios it does not look like the "didDiscoverDevices" event gets triggered since the console.log is not showing up in my terminal.它在 android 上运行良好,但在 ios 上,它看起来不像“didDiscoverDevices”事件被触发,因为 console.log 没有出现在我的终端中。 The app is running and i get no error message.该应用程序正在运行,我没有收到任何错误消息。

To configure this library for iOS i did the following:要为 iOS 配置此库,我执行了以下操作:

  1. yarn add react-native-kontaktio yarn add react-native-kontaktio
  2. cd ios cd ios
  3. pod install吊舱安装

I have also included these permissions in info.plist: NSBluetoothAlwaysUsageDescription, NSLocationAlwaysAndWhenInUseUsageDescription我还在 info.plist 中包含了这些权限:NSBluetoothAlwaysUsageDescription、NSLocationAlwaysAndWhenInUseUsageDescription

This is my react-native version: react-native-cli: 2.0.1, react-native: 0.66.4.这是我的 react-native 版本:react-native-cli:2.0.1,react-native:0.66.4。

This is the code which is working on android, but not on iOS.这是适用于 android 的代码,但不适用于 iOS。

 import React, {useEffect, useState} from 'react';
    import {
      Alert,
      DeviceEventEmitter,
      NativeEventEmitter,
      Platform,
      PermissionsAndroid,
      SafeAreaView,
      Text,
      StyleSheet,
      Button,
    } from 'react-native';

    import Kontakt, {KontaktModule} from 'react-native-kontaktio';
    
    const {connect, init, startDiscovery, startScanning} = Kontakt;
    
    const kontaktEmitter = new NativeEventEmitter(KontaktModule);
    
    const isAndroid = Platform.OS === 'android';
    const isIOS = Platform.OS === 'ios';
    
    const App = () => {
      /**
       * Android Marshmallow (6.0) and above need to ask the user to grant certain permissions.
       * This function does just that.
       */
      const requestLocationPermission = async () => {
        try {
          const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_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();
          } else {
            Alert.alert(
              'Permission error',
              'Location permission not granted. Cannot scan for beacons',
              [{text: 'OK', onPress: () => console.log('OK Pressed')}],
              {cancelable: false},
            );
          }
        } else {
          // iOS
          console.log('IOS', isIOS);
          await init();
          await startDiscovery();
        }
    
    // Add beacon listener
    if (isAndroid) {
      DeviceEventEmitter.addListener(
        'beaconsDidUpdate',
        ({beacons, region}) => {
          console.log('beaconsDidUpdate', beacons, region);
          console.log('REGION: ', region);
          console.log('BEACONS', beacons);
        }
      );
    } else {
      console.log('IOS ADD LISTENER');
      kontaktEmitter.addListener('didDiscoverDevices', ({beacons}) => {
        console.log('didDiscoverDevices', beacons);
        console.log('IOS DISCOVERED BEACON');
      });
    }
  };

      useEffect(() => {
        beaconSetup();
        return () => {
          DeviceEventEmitter.removeAllListeners();
        };
      }, []);

I just solved the problem.我刚刚解决了这个问题。

I called startRangingBeaconsInRegion(region) instead of startDiscovery() method and then it worked.我调用了 startRangingBeaconsInRegion(region) 而不是 startDiscovery() 方法,然后它起作用了。 I understood this after i read this sentence in documentation: "Discovery (ie didDiscoverDevices) can only detect Kontakt.io beacons. Ranging and monitoring also works with beacons of other manufacturers."在我阅读文档中的这句话后,我明白了这一点:“发现(即 didDiscoverDevices)只能检测 Kontakt.io 信标。测距和监控也适用于其他制造商的信标。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM