To initiate a project involving connected devices, I must establish a Bluetooth connection among the different devices.
The objective is to develop an application using React Native and then transmit data from this application to my Raspberry Pi. The Raspberry Pi is equipped with an HC-08 module responsible for managing Bluetooth communication.
Currently, I am looking into utilizing the react-native-ble-plx library to facilitate data transmission via Bluetooth. While I have successfully established a connection between my Android device and the module, I am struggling to comprehend the process of sending data...
Here is the code snippet I am working with:
constructor() {
super()
this.manager = new BleManager()
}
componentWillMount() {
console.log("mounted")
const subscription = this.manager.onStateChange((state) => {
if (state === 'PoweredOn') {
this.scanAndConnect();
subscription.remove();
}
}, true);
}
scanAndConnect() {
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
// Handle error (scanning will be stopped automatically)
return
}
console.log(device.name)
if (device.name === 'SH-HC-08') {
this.manager.stopDeviceScan();
console.log(`Found ${device.name}`)
this.setState({
device: device
})
device.connect()
.then((device) => {
console.log(device)
return device.discoverAllServicesAndCharacteristics()
})
.then((device) => {
console.log(device)
})
.then((result) => {
console.log(result)
console.log("connected")
})
.catch((error) => {
console.log(error)
});
}
});
}
send() {
this.manager.writeCharacteristicWithResponseForDevice("58:7A:62:4F:EF:6D",
this.device.serviceUUIDs[0],
this.manager.characteristicsForDevice(this.device.id),
"ok")
.catch((error) => {
console.log('error in writing data');
console.log(error);
})
}
I am seeking assistance in creating a send method that can efficiently transmit data as needed. If anyone could provide guidance or offer an example, I would greatly appreciate it.
Many thanks in advance.