React-Native蓝牙扫描:设备名称始终为空

时间:2019-06-05 09:33:25

标签: android react-native bluetooth

我正在使用React-native构建一个Android应用程序,并且需要蓝牙与特定设备进行通信。

我正在使用该库扫描设备:https://polidea.github.io/react-native-ble-plx/这是代码:

import React from "react";
import { PermissionsAndroid, View, Text, Button } from "react-native";
import { BleManager } from 'react-native-ble-plx';    

const ProfileScreen = props => {
  async function requestCameraPermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
        {
          title: 'Cool Photo App Camera Permission',
          message: 'Cool Photo App needs access to your camera',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('You can use the camera');
        return true;
      } else {
        console.log('Camera permission denied');
        return false;
      }
    } catch (err) {
      console.warn(err);
      return false;
    }
  }

  scanAndConnect = () => {
    const permission = requestCameraPermission();

    if (permission) {
      const bluetoothInstance = new BleManager();

      bluetoothInstance.startDeviceScan(null, null, (error, device) => {
        if (error) {
          console.log(error);
          return
        } else {
          console.log(device);
        }
      });
    } else {
      console.log("permission not granted")
    }
  };

  return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
      <Text>Profile</Text>
      <Button 
        title="Start scanning"
        onPress={scanAndConnect}/>
      </View>
  )
}

最初我以为它可以正常工作,但是当我检查控制台时,我发现找到的所有设备都是这样的:

Device {
    id : "2C:F0:EE:19:27:CF"
    isConnectable : null 
    localName : null
    manufacturerData : "TAAQBQsUluNJ"
    mtu : 23
    name : null
    overflowServiceUUIDs : null
    rssi : -62 
    serviceData : null 
    serviceUUIDs : null 
    solicitedServiceUUIDs : null 
    txPowerLevel : null
    _manager : BleManager
    __proto__ : Object
}

这个结果正确吗?没有namelocalName之类的信息,我找不到任何方法来了解哪个设备。我想念什么吗?

0 个答案:

没有答案