如何连接到设备BLE?

时间:2019-05-13 09:22:11

标签: android react-native bluetooth-lowenergy

我正在尝试与BLE设备建立连接。 我需要自动连接到特定设备。 我不知道如何才能直接连接到设备。让我更好地解释一下,此刻我可以扫描并得到所有的蓝牙设备。相反,我想直接连接到特定设备。 -因此,我应该只选择一种设备 -直接联系我

您认为我能做什么?

使用此代码,我选择名称

return (

        <View style={{borderBottomColor: 'lightgrey', borderBottomWidth: 1, alignItems: 'stretch'}}>
            <View style={{flexDirection: 'row', alignItems: 'center', padding: 15}}>
                <View style={{flex: 1}}>
                    <Text style={{fontSize: 20, color: '#888'}}>{`${peripheral.name}`}</Text>
                </View>
                  <View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
                    <Btn onPress={() => onConnect(peripheral)}>
                        {`Connect`}
                    </Btn>

                  </View>
            </View>
        </View>
    )

使用此代码,我进行扫描

return (

            <View style={{flex: 1 , paddingTop: Theme.navbarHeight }}>

                <StatusBar
                  /*  backgroundColor={Theme.color}*/
                  /*  barStyle="light-content" */
                />


                {
                    Object.keys(peripherals).length === 0 ? (
                        <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
                            { uiState === UiState.scanning && <ActivityIndicator size='large' color={Theme.color}/> }
                            <Text style={{marginBottom: 10, fontSize: 18, color: '#999'}}>
                                Nessun Risultato
                            </Text>
                            { uiState === UiState.idle && <Btn onPress={this._doScan}>Scansione</Btn> }
                        </View>
                    ) : (
                        <ScrollView style={{ flex: 1 }} contentContainerStyle={{ alignItems: 'stretch' }}>
                            {
                                Object.keys(peripherals).map(key => peripherals[key]).map(
                                    peripheral => (
                                        <Peripheral
                                            key={peripheral.id}
                                            peripheral={peripheral}
                                            onConnect={onConnect}
                                        />
                                    )
                                )
                            }
                        </ScrollView>
                    )
                }
            </View>
        )
    }

    _renderScanButton = () => {
        let {uiState} = this.state;
        if (uiState === UiState.scanning) {
            return <ActivityIndicator color='white'/>
        }
        return (
            <TouchableOpacity onPress={this._doScan}>
                <Text style={{color: 'white', width: 100, textAlign: 'right'}}>Scansione</Text>
            </TouchableOpacity>
        )
    }


    _doScan = () => {
        if (this.state.uiState !== UiState.idle) {
            return;
        }

        this.setState({ uiState: UiState.scanning, peripherals: {} });
        BleManager.scan([], 5, true)
            .then(results => {
                console.log('Scansione in corso...');
            })
            .catch(() => {
                this.setState({ uiState: UiState.idle })
            })
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用react-native-ble-manager。根据他们的docs,您可以使用connect方法连接到外围设备:

BleManager.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
  .then(() => {
    // Success code
    console.log('Connected');
  })
  .catch((error) => {
    // Failure code
    console.log(error);
  });

您应该从扫描方法results获取外围UID:

BleManager.scan([], 5, true)
  .then(results => {
    // Success code
    console.log(results);
  });

我个人不使用react-native-ble-manager软件包,而是使用react-native-ble-plx软件包,但是过程非常相似。这是我的方法:

manager.startDeviceScan(null, null, async (error, device) => {
    console.log("scanning bluetooth...")

    if (device.name === "MY_DEVICE_NAME") {
        manager
            .connectToDevice(device.id, {
                autoconnect: true,
                timeout: BLUETOOTH_TIMEOUT
            })
        // ............
    }
})
相关问题