Transmitting Data

To send out data through the BLE interface, use the bt.setdata method to store data into the TX buffer, and bt.send to commit this data for sending. Prior to bt.send invocation, the newly stored data will be uncommitted. Calling bt.send commits the data, and only committed data is sent out. You can check the amount of committed data in the buffer through the bt.txlen read-only property. The amount of uncommitted data can be checked via bt.newtxlen . Bt.txfree returns free buffer space, but doesn't take into the account uncommitted data, so the real free space is bt.txfree – bt.newtxlen.

For example, here is how to send out a "Hello, World!" message on a press of the MD button (this code snipped assumes that you have an active BLE connection, i.e. bt.connected = 1- YES):

** Tibbo Basic **

sub on_button_pressed
 bt.setdata("Hello,World!")
 bt.send
end sub

The code above would work, but only if at the moment of this code's execution the necessary amount of free space was already available in the TX buffer (otherwise the data would get truncated). So, obviously, you need to make sure that the TX buffer has the necessary amount of free space before sending. A simple polling solution would look like this:

** Tibbo Basic **

sub on_button_pressed
 dim s as string="This is a long string waiting to be sent. Send me already!"
 while bt.txfree - bt.newtxlen < len(s) 'we will wait for the necessary amount of free space to become available
         doevents
 wend
 bt.setdata(s)
 bt.send
end sub

Again, this is not a recommended way of sending data. Instead of doing that, employ a code that uses the bt.notifysent property and the on_bt_data_sent event:

** Tibbo Basic **

dim s as string
s = "This is a long string waiting to be sent. Send me already!"

sub on_button_pressed
 bt.notifysent(bt.txbuffsize-len(s)) ' causes the on_bt_data_sent event to fire when the TX buffer gets enough free space for the string
end sub

sub on_bt_data_sent
 bt.setdata(s)
 bt.send
end sub

As the WA2000 Wi-Fi/BLE add-on module sends out the data you placed into the TX buffer, it will be gradually removing this data from the buffer. Please note that TX buffer becoming empty does not indicate that all data has been received by the other end of the BLE link. This only means that BLE hardware of the WA2000 has already removed the data from the buffer and at least started sending it. Regrettably, there is no mechanism to let your app know when the data you send actually reaches the other end of the BLE link.

You can delete all data in the TX buffer using the bt.txclear method.