gpt4 book ai didi

android - 使用 BLE Android 连接到设备

转载 作者:行者123 更新时间:2023-11-29 00:54:15 24 4
gpt4 key购买 nike

我正在尝试在 Android 设备上使用 React-Native 中的 BLE 连接来连接到设备。我需要连接到具有特定名称的设备:例如“deviceName”。我正在使用 react-native-ble-plx。

import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
ScrollView,
FlatList,
TextInput,
Platform,
Alert
} from 'react-native';
import { BleManager } from 'react-native-ble-plx';


export default class Main extends Component {
constructor(props) {
super(props);
this.state={
scaning:false,
isConnected:false,
text:'',
writeData:'',
receiveData:'',
readData:'',
bleManager: new BleManager(),
data:[],
isMonitoring:false,

}
this.bluetoothReceiveData = [];
this.deviceMap = new Map();

}



scan() {
if(!this.state.scaning) {
this.setState({scaning:true});
this.deviceMap.clear();
const { bleManager } = this.state;
bleManager.startDeviceScan(null, null, async (error, device) => {
console.log("scanning bluetooth...")

if (device.name === "Proximity") {
bleManager.connectToDevice(device.id, {
autoconnect: true,
timeout: BLUETOOTH_TIMEOUT,
isConnected: true
})
// ............
}
})
}
}

disconnect(){
bleManager.disconnect()
.then(res=>{
this.setState({data:[...this.deviceMap.values()],isConnected:false});
})
.catch(err=>{
this.setState({data:[...this.deviceMap.values()],isConnected:false});
})
}

render(){
return(
<View>
<TouchableOpacity
activeOpacity={0.7}
style={[styles.buttonView,{marginHorizontal:10,height:40,alignItems:'center'}]}
onPress={this.state.isConnected?this.disconnect.bind(this):this.scan.bind(this)}>
<Text style={styles.buttonText}>{this.state.scaning?'Search':this.state.isConnected?'Disconnect Bluetooth':'Search Bluetooth'}</Text>
</TouchableOpacity>
</View>
);
}


}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:'white',
marginTop:Platform.OS == 'ios'?20:0,
},
item:{
flexDirection:'column',
borderColor:'rgb(235,235,235)',
borderStyle:'solid',
borderBottomWidth:StyleSheet.hairlineWidth,
paddingLeft:10,
paddingVertical:8,
},
buttonView:{
height:30,
backgroundColor:'rgb(33, 150, 243)',
paddingHorizontal:10,
borderRadius:5,
justifyContent:"center",
alignItems:'center',
alignItems:'flex-start',
marginTop:10
},
buttonText:{
color:"white",
fontSize:12,
},
content:{
marginTop:5,
marginBottom:15,
},
textInput:{
paddingLeft:5,
paddingRight:5,
backgroundColor:'white',
height:50,
fontSize:16,
flex:1,
},
})

此时我收到此错误:“undefined is not an object (evaluating 'b.default.startDeviceScan').

如何解决这个错误?你认为代码可以直接连接到设备吗?谢谢

最佳答案

您错误地导出了 BleManager。你必须像这样把它放在大括号之间:

import { BleManager } from 'react-native-ble-plx';

你也用错了 BleManager。你必须在某个地方实例化它,我习惯在状态下使用它,以确保我只有 1 个 BleManager 并像这样创建一个新的 BleManager 对象:

 constructor {
....
this.state = {
....
bleManager: new BleManager(),
....
};

然后像这样使用 this.state.bleManager 而不是 BleManager:

const { bleManager } = this.state;
bleManager.startDeviceScan(...)

关于android - 使用 BLE Android 连接到设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56130737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com