onDataReceived prints 8 times when receiving a signal #221
-
My device does an action, this action sends a byte array, which I then transform depending on the length of the array, but the device only sends that array 1 time, and on the device I receive the byte array 8 times. I don't understand what is happening, because in the arduino IDE the device only sends the byte array 1 time My code: Connection: const resp = await device.connect({
connectorType: 'rfcomm',
connectionType: 'binary',
readTimeout: 0,
}); On Data received function: const onDataReceived = ({ data }: BluetoothDeviceReadEvent) => {
const arrayBuffer = base64ToArrayBuffer(data);
// Transform array buffer to int array
// const arrayByte = new Uint8Array(arrayBuffer);
console.log(
new Uint8Array(arrayBuffer),
typeof new Uint8Array(arrayBuffer).byteLength,
);
// ...
}; |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
You'll need to do a little debugging to determine why the duplicate. I can't do much with custom code unless I have your app code, Arduino code and time to put it together.
Off the top of. My head I can't think of anything, but will try to dive deeper at some point.
I personally don't use the binary connection, but others have it working. I suggest starting in the on data Java code and see why it thinks there are 8 messages.
|
Beta Was this translation helpful? Give feedback.
-
If you're in use effect with a condition you shouldn't need an if statement. There may be an issue with the way react equals the condition? If you're seeing it set the listener multiple times. You should also be using a cleanup in use effect for things that need to unsubscribe. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for your help 🙏 Ok, I feel really dumb but I hope this post helps more people, my mistake was using my custom hook in several components. Please use your custom hook in a single component |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
If you're in use effect with a condition you shouldn't need an if statement. There may be an issue with the way react equals the condition? If you're seeing it set the listener multiple times.
You should also be using a cleanup in use effect for things that need to unsubscribe.