react-native-bluetooth-classic icon indicating copy to clipboard operation
react-native-bluetooth-classic copied to clipboard

onWrite complete event?

Open PietroGranati opened this issue 2 years ago • 5 comments

Hi Ken, I'm working with you library, finally get to work with iOS device, my project in a certain point I write an "ESC/POS" file to a thermal printer and they have limited buffer to 1024, before react-native I was working on the same project on Appcelerator and I cannot know when the "write to device is over". With this library everything is working fine and I got this log:

(BluetoothDevice:writeToDevice) Writing (BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 1024 bytes to the device
(BluetoothDevice:writeData) Sent 1024 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) Attempting to send 859 bytes to the device
(BluetoothDevice:writeData) Sent 859 bytes to the device
Stream <EAOutputStream: 0x282ed61c0> has space available
(BluetoothDevice:writeData) No buffer data scheduled for deliver
Stream <EAInputStream: 0x10d39e9c0> has bytes available
(BluetoothDevice:readFromDevice) Reading device 7005358A until delimiter 

Stream <EAInputStream: 0x10d39e9c0> has bytes available
(BluetoothDevice:readFromDevice) Reading device 7005358A until delimiter 

Do you think is possible to have a callback or some events to listen to the end of the write?

PietroGranati avatar Jan 18 '22 14:01 PietroGranati

Anything is possible, but it depends on what method of doing this would benefit the most people in the most specific way, it would also need to be completed on Android as well.

In most cases the write buffer, is just a buffer, it has no way to know how many write requests you've sent. For example, what if you did:

  • Send 4000 bytes
  • Send 4000 bytes

So we would need to keep track of the number of bytes for each write and while writing accumulate them. The problem being after:

  • 1024
  • 1024
  • 1024
  • 1024 (this would be parts of write 1 and write 2)

In your case, you may not send new write requests until the previous is done, but not everyone would do that, they're probably sending delimited strings which will eventually get sent and not need to worry about it.

To do what you want (properly) we would need to:

  • Write a custom Connection that handled sending only X bytes, notifying, then continuing.
  • Get the IOS version of the custom Package working

If you feel like taking a stab at the second part (the ability to override the auto loading feature) I would certainly accept a pull request.

kenjdavidson avatar Jan 18 '22 15:01 kenjdavidson

Or we would need to create a queue of output buffers and have the the write method automatically switch them when the primary output buffer becomes empty.

Write 1

  • outBuffer is empty so add there
  • write

Write 2

  • outBuffer is not empty
  • add to queue
  • write not required as processing should still be handling outBuffer

onSpaceAvailable

  • write out buffer
  • If nothing left in out buffer move queue into outbuffer
  • Send write_finished event
  • allow processing to continue as normal

kenjdavidson avatar Jan 18 '22 15:01 kenjdavidson

I was thinking about it and in the previous implementation I made x 1024 packets and send them, in this way I can keep track of how many but need a complete again. I'll think about it, thank you for the answers!

PietroGranati avatar Jan 18 '22 15:01 PietroGranati

Right on. I have zero time right now. So it would need to be you looking at it and submitting a pr.

Keep me posted.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Pietro Granati @.> Sent: Tuesday, January 18, 2022 10:22:54 AM To: kenjdavidson/react-native-bluetooth-classic @.> Cc: Ken Davidson @.>; Comment @.> Subject: Re: [kenjdavidson/react-native-bluetooth-classic] onWrite complete event? (Issue #164)

I was thinking about it and in the previous implementation I made x 1024 packets and send them, in this way I can keep track of how many but need a complete again. I'll think about it, thank you for the answers!

— Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkenjdavidson%2Freact-native-bluetooth-classic%2Fissues%2F164%23issuecomment-1015518489&data=04%7C01%7C%7Cb3eac88caadf46eb2adc08d9da966712%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637781161773787275%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=VpP602iIHuN4dMn8%2BqPiuboAUvFDRP6fXDmeQUpD2Mk%3D&reserved=0, or unsubscribehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABPMJL7FON6H3MKJLA7AGZLUWWAU5ANCNFSM5MHLZW4Q&data=04%7C01%7C%7Cb3eac88caadf46eb2adc08d9da966712%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637781161773787275%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=sxOch9mDk%2FYvNpiUnOG%2BGEN2SIkxLegdvU21I6OZVOI%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7Cb3eac88caadf46eb2adc08d9da966712%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637781161773787275%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=VsGJ5%2FSM5P11RnNxwq02J2U62XKNV3DUN7GPO3cz38s%3D&reserved=0 or Androidhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7Cb3eac88caadf46eb2adc08d9da966712%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637781161773787275%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=y1nCmmukmxpHUyaKMc%2Bhoq3aP%2FZ%2B74lgLcvPJ45YZUw%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

kenjdavidson avatar Jan 18 '22 15:01 kenjdavidson

Abolutely! Thank you!

PietroGranati avatar Jan 18 '22 15:01 PietroGranati