微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

react-native-ble-manager 没有显示移动蓝牙和蓝牙耳机

如何解决react-native-ble-manager 没有显示移动蓝牙和蓝牙耳机

我正在尝试实现我的应用程序的蓝牙服务。但是当扫描盯着我的应用程序时,没有显示移动蓝牙和蓝牙耳机蓝牙。

只在控制台中显示没有名称的蓝牙。

请帮忙解决这个问题。

import React,{ Component } from 'react';
import { NativeModules,NativeEventEmitter,View,Text } from 'react-native';
import BleManager from 'react-native-ble-manager';

const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);


export default class App extends Component{
  state = {
    peripherals: new Map(),};

  componentDidMount() {
    BleManager.start({ showAlert: false })
  
    this.handlerdiscover = bleManagerEmitter.addListener(
      'BleManagerdiscoverPeripheral',this.handlediscoverPeripheral
    );
  
    this.handlerStop = bleManagerEmitter.addListener(
      'BleManagerStopScan',this.handleStopScan
    );
  
    this.scanForDevices(); // I chose to start scanning for devices here
  }
  
  scanForDevices() {
    BleManager.scan([],30);
  }
  
  handlediscoverPeripheral = (peripheral) => {
    console.log('Got ble peripheral',peripheral);
    const { peripherals } = this.state;
  
    if (peripheral.name) {
      peripherals.set(peripheral.id,peripheral.name);
    }
    this.setState({ peripherals });
  };

  handleStopScan = () => {
    console.log('Scan is stopped. Devices: ',this.state.peripherals);
  }

  render(){
    return(
      <View>
        <Text>Bluetooth</Text>
      </View>
    )
  }
}

解决方法

react-native-ble-manager 可让您的手机与低功耗蓝牙 (BLE) 设备之间进行通信。 BLE 设备需要通告它们的存在才能被检测到。请记住,BLE 与蓝牙经典不同。

您只能使用 react-native-ble-manager 检测 BLE 设备。如果您找不到您的设备,它们可能不是 BLE 设备,而是蓝牙经典设备。其他手机通常不宣传 BLE 服务,需要应用程序才能这样做。蓝牙耳机可能会使用 BLE 来实现某些功能,但音频传输由蓝牙经典处理。

您可以使用通用 BLE 扫描仪应用(例如 nRF Connect)来扫描附近的 BLE 设备。该应用程序甚至允许您在另一部手机上启动 GATT 服务器,以便使用 BLE 进行访问。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。