【发布时间】:2021-06-23 06:00:58
【问题描述】:
我正在尝试在我的应用程序中实现蓝牙服务。但是当扫描时盯着我的应用程序到那个时候没有显示移动蓝牙和蓝牙耳机。
在控制台中只显示没有名称的蓝牙。
请帮助解决这个问题。
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 bluetooth bluetooth-lowenergy