<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Explain BLE Example Code]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I am trying to connect the WiPy via BLE to an Android application, and I am at the stage where I am trying to understand how BLE works currently.</p>
<p dir="auto">I was looking at the example advertising and connection code that is here: <a href="https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/gattscharacteristic.html" target="_blank" rel="noopener noreferrer nofollow">https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/gattscharacteristic.html</a></p>
<p dir="auto">Can someone explain to me how the code here works. My comments are how I interpret the code, based on what I've read about BLE, and basically my questions are as the comments state.</p>
<pre><code> from network import Bluetooth

 bluetooth = Bluetooth()
 bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890123456')

 def conn_cb (bt_o):
     events = bt_o.events()     # Here we are checking if a device is connected.
     if  events &amp; Bluetooth.CLIENT_CONNECTED:
         print(&quot;Client connected&quot;)
     elif events &amp; Bluetooth.CLIENT_DISCONNECTED:
         print(&quot;Client disconnected&quot;)

 bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED |  # Can someone explain this?
 Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)

 bluetooth.advertise(True)   #Enable BLE advertising

 srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True) # what is &quot;isPrimary&quot;?

 chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5)

 char1_read_counter = 0
 def char1_cb_handler(chr):
     global char1_read_counter
     char1_read_counter += 1  # Everytime we want to read the first characteristic, increment.

     events = chr.events()
     if  events &amp; Bluetooth.CHAR_WRITE_EVENT: # When client tries to overwrite characteristic?
         print(&quot;Write request with value = {}&quot;.format(chr.value()))
     else:
         if char1_read_counter &lt; 3:
             print('Read request on char 1')
         else:
             return 'ABC DEF'  # If we try to read more than three times, we return this? Why?

 # What does the line below do?
 char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | 
                          Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)

 srv2 = bluetooth.service(uuid=1234, isprimary=True)

 chr2 = srv2.characteristic(uuid=4567, value=0x1234)
 char2_read_counter = 0xF0
 def char2_cb_handler(chr):
     global char2_read_counter
     char2_read_counter += 1
     if char2_read_counter &gt; 0xF1:
         return char2_read_counter

 char2_cb = chr2.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char2_cb_handler)             
 #What does the line above do?
</code></pre>
<p dir="auto">Thank you all for your help and time.</p>
]]></description><link>https://forum.pycom.io/topic/2389/explain-ble-example-code</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 18:16:45 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/2389.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 06 Jan 2018 12:06:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Explain BLE Example Code on Sat, 06 Jan 2018 12:06:01 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I am trying to connect the WiPy via BLE to an Android application, and I am at the stage where I am trying to understand how BLE works currently.</p>
<p dir="auto">I was looking at the example advertising and connection code that is here: <a href="https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/gattscharacteristic.html" target="_blank" rel="noopener noreferrer nofollow">https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/gattscharacteristic.html</a></p>
<p dir="auto">Can someone explain to me how the code here works. My comments are how I interpret the code, based on what I've read about BLE, and basically my questions are as the comments state.</p>
<pre><code> from network import Bluetooth

 bluetooth = Bluetooth()
 bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890123456')

 def conn_cb (bt_o):
     events = bt_o.events()     # Here we are checking if a device is connected.
     if  events &amp; Bluetooth.CLIENT_CONNECTED:
         print(&quot;Client connected&quot;)
     elif events &amp; Bluetooth.CLIENT_DISCONNECTED:
         print(&quot;Client disconnected&quot;)

 bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED |  # Can someone explain this?
 Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)

 bluetooth.advertise(True)   #Enable BLE advertising

 srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True) # what is &quot;isPrimary&quot;?

 chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5)

 char1_read_counter = 0
 def char1_cb_handler(chr):
     global char1_read_counter
     char1_read_counter += 1  # Everytime we want to read the first characteristic, increment.

     events = chr.events()
     if  events &amp; Bluetooth.CHAR_WRITE_EVENT: # When client tries to overwrite characteristic?
         print(&quot;Write request with value = {}&quot;.format(chr.value()))
     else:
         if char1_read_counter &lt; 3:
             print('Read request on char 1')
         else:
             return 'ABC DEF'  # If we try to read more than three times, we return this? Why?

 # What does the line below do?
 char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | 
                          Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)

 srv2 = bluetooth.service(uuid=1234, isprimary=True)

 chr2 = srv2.characteristic(uuid=4567, value=0x1234)
 char2_read_counter = 0xF0
 def char2_cb_handler(chr):
     global char2_read_counter
     char2_read_counter += 1
     if char2_read_counter &gt; 0xF1:
         return char2_read_counter

 char2_cb = chr2.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char2_cb_handler)             
 #What does the line above do?
</code></pre>
<p dir="auto">Thank you all for your help and time.</p>
]]></description><link>https://forum.pycom.io/post/14162</link><guid isPermaLink="true">https://forum.pycom.io/post/14162</guid><dc:creator><![CDATA[panos29]]></dc:creator><pubDate>Sat, 06 Jan 2018 12:06:01 GMT</pubDate></item></channel></rss>