Android 如何连接到设备?
我正在尝试使用BLE连接到设备。 我需要自动连接到特定设备。 我不明白如何直接连接到设备。让我更好地解释一下,目前我可以扫描所有的蓝牙设备。相反,我希望直接连接到特定的设备。 -因此,我应该只选择一种类型的设备 -直接给我接通 你认为我能做什么 使用此代码,我选择名称 用这个代码我做扫描 您正在使用react-native ble管理器。根据他们的要求,您可以使用连接方法连接到外围设备: 您应该从扫描方法结果中获取外围设备UID: 我个人不使用react-native ble manager包,而是使用react-native ble plx包,但过程非常类似。我是这样做的:Android 如何连接到设备?,android,react-native,bluetooth-lowenergy,Android,React Native,Bluetooth Lowenergy,我正在尝试使用BLE连接到设备。 我需要自动连接到特定设备。 我不明白如何直接连接到设备。让我更好地解释一下,目前我可以扫描所有的蓝牙设备。相反,我希望直接连接到特定的设备。 -因此,我应该只选择一种类型的设备 -直接给我接通 你认为我能做什么 使用此代码,我选择名称 用这个代码我做扫描 您正在使用react-native ble管理器。根据他们的要求,您可以使用连接方法连接到外围设备: 您应该从扫描方法结果中获取外围设备UID: 我个人不使用react-native ble manager包,
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
})
// ............
}
})
您使用什么库进行BLE连接?@AdamGold'react-native-BLE-manager'首先感谢您的回复!我是否必须输入设备名或MAC地址而不是X?另一个问题,如果我需要同时连接两个设备,我可以使用相同的功能吗?你应该输入设备的地址。关于多个连接-我个人使用react-native-ble plx软件包,对react-native-ble-manager不太了解。好的,我看过你的编辑文章。所以我应该先扫描,甚至不向用户显示,然后使用results变量并将其传递给BleManager.connect函数?不完全是这样。结果变量应包含所有外围设备。您应该选择一个并使用其地址。关于react native ble plx,您可以读取设备发送的通知吗?
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 })
})
}
}
BleManager.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then(() => {
// Success code
console.log('Connected');
})
.catch((error) => {
// Failure code
console.log(error);
});
BleManager.scan([], 5, true)
.then(results => {
// Success code
console.log(results);
});
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
})
// ............
}
})