如何使用蓝牙和React Native在两部手机之间传输数据?

问题描述 投票:0回答:1

我的问题是:是否可以使用蓝牙和 React Native 在两部手机之间传输数据?

在过去的几天里,我一直在研究 React Native 和蓝牙协议。我已经能够在 React Native 中制作一个简单的 BLE 扫描器。完成此操作后,我想通过蓝牙将我的两部手机连接在一起,但我的扫描仪没有看到我的另一部手机。在这里我了解了经典蓝牙和 BLE 之间的区别。

我想要完成的是创建一个应用程序,可以通过蓝牙将小块数据发送到另一部手机。这可能吗?我可以为此使用 BLE 吗?或者我需要经典蓝牙吗?

如果我需要经典蓝牙,有人有代码片段或库来帮助我吗?我确实找到了这个存储库:https://github.com/kenjdavidson/react-native-bluetooth-classic 但我不明白如何安装/使用它。

我的简单 BLE 扫描器是使用此库制作的:https://github.com/dotintent/react-native-ble-plx 而且它非常易于使用且易于理解。

这是使我的应用程序运行的代码:

import React, {Component} from 'react';
import {Platform, View, PermissionsAndroid, Text, Button, ScrollView, StyleSheet} from 'react-native';
import {BleManager} from 'react-native-ble-plx';

export default class Ble_test extends Component {
    constructor() {
        super();
        this.manager = new BleManager();
        this.state = {
            text1: '',
            components: [],
        };
    }

    componentWillUnmount() {
        this.manager.stopDeviceScan();
        this.manager.destroy();
        delete this.manager;
    }

    UNSAFE_componentWillMount() {
        this.manager = new BleManager();
        if (Platform.OS === 'android' && Platform.Version >= 23) {
            PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
                if (result) {
                    console.log('Permission is OK');
                } else {
                    PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION).then((result) => {
                        if (result === 'denied') {
                            console.log('User refuse');
                        } else {
                            console.log('User accept');
                        }
                    });
                }
            });
        }
    }

    async scanAndConnect() {
        this.manager.stopDeviceScan();

        this.setState({text1: 'Scanning...'});
        this.manager.startDeviceScan(null, null, (error, device) => {
            if (error) {
                alert('Error in scan=> ' + error);
                this.setState({text1: ''});
                this.manager.stopDeviceScan();
                this.UNSAFE_componentWillMount();
                return;
            }
            if (device.name != null) {
                let contains = false;

                for (const o of this.state.components) {
                    if (device.id === o.id) {
                        contains = true;
                        break;
                    }
                }

                if (contains === false) {
                    this.state.components.push(device);
                    this.setState({components: this.state.components});
                }
            }
        });
    }

    render() {
        return (
            <View>
                <Button block onPress={() => this.scanAndConnect()} title="Scan for a device">
                </Button>
                <View style={{alignItems: 'center', marginVertical: 10}}>
                    <Text>{this.state.text1}</Text>
                </View>
                <ScrollView>
                    {this.state.components.map((item, index) => (
                        <Text key={index} style={styles.welcome}>{
                            item.name
                        }
                        </Text>
                    ))}
                </ScrollView>
            </View>
        );
    }
}

const styles = StyleSheet.create(
    {
        welcome: {
            flex: 1,
            textAlign: 'center',
            textAlignVertical: 'center',
            backgroundColor: 'orange',
            marginTop: 10,
            height: 70,
        },
    },
);

总而言之,是否可以使用React Native通过蓝牙在两部手机之间传输数据?如果可以,我可以使用 BLE 吗?或者我需要经典蓝牙吗?如果可能的话,有人可以向我发送代码片段来帮助我吗?

提前致谢!

编辑: 感谢您的回复 Michael Kotzjan!我查看了react-native-bluetooth-classic 项目,并尝试运行它,但我不知道如何运行。我只是不知道如何让该项目运行。我使用的是 Windows 10 电脑,我想在我的 Android 7.1.1 设备上运行它。谁能帮我解决这个问题。

javascript react-native bluetooth bluetooth-lowenergy
1个回答
1
投票

使用 BLE 的两部手机之间的通信绝对是可能的。您无法使用自己的 BLE 扫描仪发现第二部手机的原因是,BLE 设备只有在宣传其存在时才能被发现。您的第二部手机需要充当外围设备,并宣传服务以供您的第一部手机连接。

有一些免费应用程序(Ble 外设模拟器nRF Connect...)可让您使用手机模拟 BLE 外设,您可以用它来测试这一点。

不幸的是react-native-ble-plx明确指出

不支持:

  • 蓝牙经典设备。
  • 使用BLE(外设支持)在手机之间进行通信
  • 粘合外围设备

我发现的唯一可能适合您目的的库是react-native-ble-peripheral,但它已经有一段时间没有收到更新了。

关于使用react-native-bluetooth-classic的问题,请查看他们的示例应用程序,也许对您有帮助。请参阅自述文件了解说明

© www.soinside.com 2019 - 2024. All rights reserved.