You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`id` Only one Bluetooth peripheral available so must always be 0
66
-
*`mode` currently the only supported mode is `Bluetooth.BLE`
67
-
*`antenna` selects between the internal and the external antenna. Can be either`Bluetooth.INT_ANT`, `Bluetooth.EXT_ANT`.
68
-
*`secure` enables or disables the GATT Server security features.
66
+
*`id` Only one Bluetooth peripheral available so must always be 0.
67
+
*`mode` currently the only supported mode is `Bluetooth.BLE`.
68
+
*`antenna` selects between the internal and the external antenna. Can be either `Bluetooth.INT_ANT`, `Bluetooth.EXT_ANT`.
69
+
*`modem_sleep` Enables or Disables BLE modem sleep, Disable modem sleep as a workaround when having Crashes due to flash cache being disabled, as this prevents BLE task saving data in external RAM while accesing external flash for R/W.
70
+
*`pin` a one to six digit number (`0`-`9`) to connect to the GATT Sever. Setting any valid pin, GATT Server security features are activated.
71
+
*`privacy` Enables or Disables local privacy settings so address will be random or public.
72
+
*`secure_connections` Enables or Disables Secure Connections and MITM Protection.
73
+
*`mtu` Maximum Transmission Unit (MTU) is the maximum length of an ATT packet. Value must be between `23` and `200`.
69
74
70
-
With our development boards it defaults to using the internal antenna, but in the case of an OEM module, the antenna pin (`P12`) is not used, so it's free to be used for other things.
75
+
With our development boards it defaults to using the internal antenna, but in the case of an OEM module, the antenna pin (`P12`) is not used, so it's free to be used for other things.
71
76
72
77
Initialises and enables the Bluetooth radio in BLE mode.
73
78
@@ -84,7 +89,7 @@ Pin('P12', mode=Pin.OUT)(True)
84
89
85
90
Disables the Bluetooth radio.
86
91
87
-
### bluetooth.start\_scan(timeout)
92
+
####bluetooth.start\_scan(timeout)
88
93
89
94
Starts performing a scan listening for BLE devices sending advertisements. This function always returns immediately, the scanning will be performed on the background. The return value is `None`. After starting the scan the function `get_adv()` can be used to retrieve the advertisements messages from the FIFO. The internal FIFO has space to cache 16 advertisements.
90
95
@@ -95,20 +100,19 @@ The arguments are:
95
100
Examples:
96
101
97
102
```python
98
-
99
103
bluetooth.start_scan(10) # starts scanning and stop after 10 seconds
100
104
bluetooth.start_scan(-1) # starts scanning indefinitely until bluetooth.stop_scan() is called
101
105
```
102
106
103
-
### bluetooth.stop\_scan()
107
+
####bluetooth.stop\_scan()
104
108
105
109
Stops an ongoing scanning process. Returns `None`.
106
110
107
-
### bluetooth.isscanning()
111
+
####bluetooth.isscanning()
108
112
109
113
Returns `True` if a Bluetooth scan is in progress. `False` otherwise.
110
114
111
-
### bluetooth.get\_adv()
115
+
####bluetooth.get\_adv()
112
116
113
117
Gets an named tuple with the advertisement data received during the scanning. The tuple has the following structure: `(mac, addr_type, adv_type, rssi, data)`
114
118
@@ -121,7 +125,6 @@ Gets an named tuple with the advertisement data received during the scanning. Th
121
125
Example for getting `mac` address of an advertiser:
122
126
123
127
```python
124
-
125
128
import ubinascii
126
129
127
130
bluetooth = Bluetooth()
@@ -131,11 +134,11 @@ adv = bluetooth.get_adv() #
131
134
ubinascii.hexlify(adv.mac) # convert hexadecimal to ascii
132
135
```
133
136
134
-
### bluetooth.get\_advertisements()
137
+
####bluetooth.get\_advertisements()
135
138
136
139
Same as the `get_adv()` method, but this one returns a list with all the advertisements received.
Parses the advertisement data and returns the requested `data_type` if present. If the data type is not present, the function returns `None`.
141
144
@@ -147,7 +150,6 @@ Arguments:
147
150
Example:
148
151
149
152
```python
150
-
151
153
import ubinascii
152
154
from network import Bluetooth
153
155
bluetooth = Bluetooth()
@@ -166,18 +168,20 @@ while bluetooth.isscanning():
166
168
print(ubinascii.hexlify(mfg_data))
167
169
```
168
170
169
-
### bluetooth.connect(mac\_addr)
171
+
####bluetooth.set\_pin()
170
172
171
-
Opens a BLE connection with the device specified by the `mac_addr` argument. This function blocks until the connection succeeds or fails. If the connections succeeds it returns a object of type `GATTCConnection`.
173
+
Configures a new PIN to be used by the device. The PIN is a 1-6 digit length decimal number, if less than 6 digits are given the missing leading digits are considered as 0. E.g. 1234 becomes 001234. When a new PIN is configured, the information of all previously bonded device is removed and the current connection is terminated. To restart advertisement the advertise() must be called after PIN is changed.
172
174
173
-
Connections are initiated by the central device. There is a maximum of 4 simultaneous connections.
175
+
#### bluetooth.connect(mac\_addr, timeout=None)
174
176
175
-
```python
177
+
*`mac_addr` is the address of the remote device to connect
178
+
*`timeout` specifies the amount of time in milliseconds to wait for the connection process to finish. If not given then no timeout is applied The function blocks until the connection succeeds or fails (raises OSError) or the given `timeout` expires (raises `Bluetooth.timeout TimeoutError`). If the connections succeeds it returns a object of type `GATTCConnection`.
176
179
180
+
```python
177
181
bluetooth.connect('112233eeddff') # mac address is accepted as a string
Create a new service on the internal GATT server. Returns a object of type `BluetoothServerService`.
240
255
@@ -246,20 +261,35 @@ The arguments are:
246
261
*`start` if `True` the service is started immediately.
247
262
248
263
```python
249
-
250
264
bluetooth.service('abc123')
251
265
```
252
266
253
-
### bluetooth.disconnect\_client()
267
+
####bluetooth.disconnect\_client()
254
268
255
269
Closes the BLE connection with the client.
256
270
271
+
### bluetooth.tx\_power(type, level)
272
+
273
+
Gets or sets the TX Power level.
274
+
If called with only `type` parameter it returns with the current value belonging to the given type.
275
+
If both `type` and `level` parameters are given, it sets the TX Power.
276
+
277
+
Valid values for `type`: `Bluetooth.TX_PWR_CONN` -> for handling connection, `Bluetooth.TX_PWR_ADV` -> for advertising, `Bluetooth.TX_PWR_SCAN` -> for scan, `Bluetooth.TX_PWR_DEFAULT` -> default, if others not set
Creates a callback that will be executed when any of the triggers occurs. The arguments are:
30
+
Creates a callback that will be executed when any of the triggers occur. The arguments are:
27
31
28
32
*`trigger` can be either `Bluetooth.CHAR_READ_EVENT` or `Bluetooth.CHAR_WRITE_EVENT`.
29
33
*`handler` is the function that will be executed when the callback is triggered.
30
34
*`arg` is the argument that gets passed to the callback. If nothing is given, the characteristic object that owns the callback will be used.
31
35
32
-
An example of how this could be implemented can be seen in the [`characteristic.events()`](gattscharacteristic.md#characteristic-events)section.
36
+
Beyond the `arg` a tuple (called `data`) is also passed to `handler`. The tuple consists of (event, value), where `event` is the triggering event and `value` is the value strictly belonging to the `event` in case of a WRITE event. If the `event` is not a WRITE event, the `value` has no meaning.
33
37
34
-
#### characteristic.events()
35
-
36
-
Returns a value with bit flags identifying the events that have occurred since the last call. Calling this function clears the events.
38
+
We recommend getting both the `event` and new `value` of the characteristic via this tuple, and not via `characteristic.event()` and `characteristic.value()` calls in the context of the `handler` to make sure no event and value is lost.
39
+
The reason behind this is that `characteristic.event()` and `characteristic.value()` return with the very last event received and with the current value of the characteristic, while the input parameters are always linked to the specific event triggering the `handler`. If the device is busy executing other operations, the `handler` of an incoming event may not be called before the next event occurs and is processed.
37
40
38
-
An example of advertising and creating services on the device:
41
+
An example of how this can be implemented is shown below, via an example of advertising and creating services on the device:
Copy file name to clipboardExpand all lines: content/firmwareapi/pycom/network/bluetooth/gattsservice.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,12 +27,11 @@ Stops the service if previously started.
27
27
Creates a new characteristic on the service. Returns an object of the class `GATTSCharacteristic`. The arguments are:
28
28
29
29
*`uuid` is the UUID of the service. Can take an integer or a 16 byte long string or bytes object.
30
-
*`permissions` configures the permissions of the characteristic. Takes an integer with a combination of the flags.
30
+
*`permissions` configures the permissions of the characteristic. Takes an integer with a combination of the flags. When bluetooth object is initialized with PIN, read and write permissions are set to encrypted. Setting PIN later with set_pin() call does not affect the permissions of the already existing characteristics, thus they will remain not secured.
31
31
*`properties` sets the properties. Takes an integer with an OR-ed combination of the flags.
32
32
*`value` sets the initial value. Can take an integer, a string or a bytes object.
0 commit comments