Skip to content

Commit df8db6d

Browse files
committed
Added details about BT serial communication
1 parent f46a7f8 commit df8db6d

File tree

2 files changed

+167
-5
lines changed

2 files changed

+167
-5
lines changed

_build/pages/android.markdown

+96-2
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ wend
223223

224224
The code snippet demonstrates how to use REQUEST to interact with a specified endpoint, providing necessary data and token for authentication.
225225

226-
## IoT Device Communication
226+
# IoT Device Communication
227227

228-
### USB Serial Support
228+
## USB Serial Support
229229

230230
These functions allow you to connect, send, and receive data over a USB serial connection.
231231

@@ -302,6 +302,100 @@ while 1
302302
wend
303303
```
304304

305+
## Bluetooth Support
306+
307+
These functions allow you to connect to Bluetooth serial devices, send and receive data, and manage the connection within your SmallBASIC program.
308+
309+
#### 🔌 Open a Bluetooth Serial Connection
310+
311+
```smallbasic
312+
const bt = android.openBluetooth(deviceName)
313+
```
314+
315+
Parameters:
316+
317+
- `deviceName`: (required) The Bluetooth device name to connected with.
318+
319+
Returns:
320+
321+
- A Bluetooth object to be used with other Bluetooth functions.
322+
323+
#### ❌ Close the Bluetooth Connection
324+
325+
```smallbasic
326+
bt.close()
327+
```
328+
329+
Closes the current Bluetooth connection.
330+
331+
Tip: Always close the connection when you're done to free resources.
332+
333+
#### 🔌 Check whether the Bluetooth Connection is open
334+
335+
```smallbasic
336+
cn = bt.connected()
337+
```
338+
339+
Returns true if the Bluetooth connection is currently open, otherwise false.
340+
341+
#### 📋 Get Device Description
342+
343+
```smallbasic
344+
bt.description()
345+
```
346+
347+
Returns a string with details about the Bluetooth connection inclusing name and address (if available).
348+
349+
#### 📥 Receive Data
350+
351+
```smallbasic
352+
dat = bt.receive()
353+
```
354+
355+
Reads incoming data from the connected Bluetooth device.
356+
357+
Returns:
358+
359+
A string containing the received data, or an empty string if no data is received before the timeout.
360+
361+
#### 📤 Send Data
362+
363+
```smallbasic
364+
n = bt.send(dat)
365+
```
366+
367+
Sends data to the connected Bluetooth device.
368+
369+
Parameters:
370+
371+
dat – The data to send as a string.
372+
373+
Returns:
374+
375+
The number of bytes successfully sent.
376+
377+
#### 📝 Example
378+
379+
```
380+
import android
381+
382+
bt = android.openBluetooth("TeensyBT")
383+
print "opened"
384+
print bt.description()
385+
for i = 0 to 1000
386+
if bt.connected() then
387+
n= bt.send(i)
388+
delay 10
389+
print bt.receive(); " "; i
390+
else
391+
print "waiting..."
392+
delay 3000
393+
endif
394+
next
395+
```
396+
397+
This example opens a connection to a device named "TeensyBT", then continuously sends and receives data in a loop.
398+
305399
## How to edit and run a program
306400

307401
### Internal editor:

pages/android.html

+71-3
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ <h2 id="https-web-request">HTTPS Web request</h2>
235235
<p>The code snippet demonstrates how to use REQUEST to interact with a
236236
specified endpoint, providing necessary data and token for
237237
authentication.</p>
238-
<h2 id="iot-device-communication">IoT Device Communication</h2>
239-
<h3 id="usb-serial-support">USB Serial Support</h3>
238+
<h1 id="iot-device-communication">IoT Device Communication</h1>
239+
<h2 id="usb-serial-support">USB Serial Support</h2>
240240
<p>These functions allow you to connect, send, and receive data over a
241241
USB serial connection.</p>
242242
<h4 id="open-a-usb-serial-connection">🔌 Open a USB Serial
@@ -289,6 +289,74 @@ <h4 id="example">📝 Example</h4>
289289
print &quot;sent &quot;; n
290290
print usb.receive()
291291
wend</code></pre>
292+
<h2 id="bluetooth-support">Bluetooth Support</h2>
293+
<p>These functions allow you to connect to Bluetooth serial devices,
294+
send and receive data, and manage the connection within your Smallbasic
295+
program.</p>
296+
<h4 id="open-a-bluetooth-serial-connection">🔌 Open a Bluetooth Serial
297+
Connection</h4>
298+
<div class="sourceCode" id="cb21"><pre
299+
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="dt">const</span> bt = android.openBluetooth(deviceName)</span></code></pre></div>
300+
<p>Parameters:</p>
301+
<ul>
302+
<li><code>deviceName</code>: (required) The Bluetooth device name to
303+
connected with.</li>
304+
</ul>
305+
<p>Returns:</p>
306+
<ul>
307+
<li>A Bluetooth object to be used with other Bluetooth functions.</li>
308+
</ul>
309+
<h4 id="close-the-bluetooth-connection">❌ Close the Bluetooth
310+
Connection</h4>
311+
<div class="sourceCode" id="cb22"><pre
312+
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>bt.<span class="fu">close</span>()</span></code></pre></div>
313+
<p>Closes the current Bluetooth connection.</p>
314+
<p>Tip: Always close the connection when you’re done to free
315+
resources.</p>
316+
<h4 id="check-whether-the-bluetooth-connection-is-open">🔌 Check whether
317+
the Bluetooth Connection is open</h4>
318+
<div class="sourceCode" id="cb23"><pre
319+
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>cn = bt.connected()</span></code></pre></div>
320+
<p>Returns true if the Bluetooth connection is currently open, otherwise
321+
false.</p>
322+
<h4 id="get-device-description-1">📋 Get Device Description</h4>
323+
<div class="sourceCode" id="cb24"><pre
324+
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>bt.description()</span></code></pre></div>
325+
<p>Returns a string with details about the Bluetooth connection
326+
inclusing name and address (if available).</p>
327+
<h4 id="receive-data-1">📥 Receive Data</h4>
328+
<div class="sourceCode" id="cb25"><pre
329+
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>dat = bt.receive()</span></code></pre></div>
330+
<p>Reads incoming data from the connected Bluetooth device.</p>
331+
<p>Returns:</p>
332+
<p>A string containing the received data, or an empty string if no data
333+
is received before the timeout.</p>
334+
<h4 id="send-data-1">📤 Send Data</h4>
335+
<div class="sourceCode" id="cb26"><pre
336+
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>n = bt.send(dat)</span></code></pre></div>
337+
<p>Sends data to the connected Bluetooth device.</p>
338+
<p>Parameters:</p>
339+
<p>dat – The data to send as a string.</p>
340+
<p>Returns:</p>
341+
<p>The number of bytes successfully sent.</p>
342+
<h4 id="example-1">📝 Example</h4>
343+
<pre><code>import android
344+
345+
bt = android.openBluetooth(&quot;TeensyBT&quot;)
346+
print &quot;opened&quot;
347+
print bt.description()
348+
for i = 0 to 1000
349+
if bt.connected() then
350+
n= bt.send(i)
351+
delay 10
352+
print bt.receive(); &quot; &quot;; i
353+
else
354+
print &quot;waiting...&quot;
355+
delay 3000
356+
endif
357+
next</code></pre>
358+
<p>This example opens a connection to a device named “TeensyBT”, then
359+
continuously sends and receives data in a loop.</p>
292360
<h2 id="how-to-edit-and-run-a-program">How to edit and run a
293361
program</h2>
294362
<h3 id="internal-editor">Internal editor:</h3>
@@ -358,7 +426,7 @@ <h2 id="privacy-policy">Privacy Policy <a
358426
<p><a href="privacy.html">Privacy Policy</a></p>
359427
</div>
360428
<div class="pagefooter">
361-
This page was last edited on Sun, 20 Apr 2025 08:02:09 +0930
429+
This page was last edited on Sun, 20 Apr 2025 09:34:07 +0930
362430
|
363431
<a href="https://en.wikipedia.org/wiki/Markdown" target="_blank" rel="nofollow">Markdown</a>
364432
processed with

0 commit comments

Comments
 (0)