Skip to content

Commit f70ec3b

Browse files
committed
Update readme with service based scanning
for 2.6.0
1 parent 73d79ba commit f70ec3b

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

README.md

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,16 @@ usage: ble-scan [-h] [-t SEC] [-d ADDR]
114114

115115
Scanner for BLE devices and service/characteristics.
116116

117-
optional arguments:
117+
options:
118118
-h, --help show this help message and exit
119119
-t SEC, --scan-time SEC
120120
Duration of the scan in seconds (default: 5.0)
121+
-i ADAPTER, --interface ADAPTER
122+
BLE host adapter number to use (default: hci0)
121123
-d ADDR, --deep-scan ADDR
122-
Try to connect to device and read out service/characteristic UUIDs
123-
(default: None)
124+
Try to connect to device and read out service/characteristic UUIDs (default: None)
125+
-s SERVICE_UUID, --service-uuid SERVICE_UUID
126+
The service used for scanning of potential devices (default: None)
124127
```
125128

126129
On Bluetooth 2.0 there was a "serial port profile", with 4.0 - 5.2 (BLE) there is unfortunately no standardized mode anymore, every chip manufacturer chooses their own UUIDs to implement the features.
@@ -129,7 +132,7 @@ On Bluetooth 2.0 there was a "serial port profile", with 4.0 - 5.2 (BLE) there i
129132
'0000ff02-0000-1000-8000-00805f9b34fb', # LithiumBatteryPCB adapter: write
130133
'0000ffe1-0000-1000-8000-00805f9b34fb', # TI CC245x (HM-10, HM-11)
131134
```
132-
Some usual UUIDs are included in ble-serial, these will be tried automatically if nothing is specified.
135+
Some usual read/write UUIDs are included in ble-serial, these will be tried automatically if nothing is specified.
133136
You might skip this part and start directly with the connection.
134137

135138
Otherwise to find the correct UUID, use the deep scan option. It expects a device MAC/ID, connects to it and reads out all services/characteristic/descriptors:
@@ -147,7 +150,7 @@ SERVICE 0000ffe0-0000-1000-8000-00805f9b34fb (Handle: 16): Vendor specific
147150

148151
Completed deep scan of 20:91:48:4C:4C:54
149152
```
150-
Now the interesting parts are the characteristics, grouped into services. The ones belows the first service starting with `00002` are not interesting in this case, because they are standard values (for example the device name), if you want to know more look at [this list](https://gist.github.com/sam016/4abe921b5a9ee27f67b3686910293026#file-allgattcharacteristics-java-L57).
153+
Now the interesting parts are the characteristics, grouped into services. The ones belows the first service starting with `00002` are not relevant in this case, because they are standard values (for example the device name), if you want to know more look at [this list](https://gist.github.com/sam016/4abe921b5a9ee27f67b3686910293026#file-allgattcharacteristics-java-L57).
151154

152155
After the UUID, handle and type the permissions are listed in []. We are searching for a characteristic that allows writing = sending to the device, the only candidate in here is `0000ffe1-0000-1000-8000-00805f9b34fb` (spoiler: a HM-11 module again).
153156
Same procedure with the read characteristic, here you have to actually look for `notify` or `indicate`, that is how the receiving side is informed about new data in BLE.
@@ -177,39 +180,48 @@ usage: __main__.py [-h] [-v] -d DEVICE [-t {public,random}] [-i ADAPTER] [-m MTU
177180

178181
Create virtual serial ports from BLE devices.
179182

180-
optional arguments:
183+
options:
181184
-h, --help show this help message and exit
182-
-v, --verbose Increase verbosity to log all data going through (default: False)
185+
-v, --verbose Increase verbosity, can be specified multiple times for connection/DBus debugging (default: 0)
183186
-p PORT, --port PORT Symlink to virtual serial port (default: /tmp/ttyBLE)
184-
-d DEVICE, --dev DEVICE
185-
BLE device address to connect (hex format, can be separated by colons) (default: None)
187+
188+
connection parameters:
186189
-t SEC, --timeout SEC
187190
BLE connect/discover timeout in seconds (default: 5.0)
188-
-a {public,random}, --address-type {public,random}
189-
BLE address type, either public or random (default: public)
190191
-i ADAPTER, --interface ADAPTER
191192
BLE host adapter number to use (default: hci0)
192193
-m MTU, --mtu MTU Max. bluetooth packet data size in bytes used for sending (default: 20)
194+
195+
device parameters:
196+
-d DEVICE, --dev DEVICE
197+
BLE device address to connect (hex format, can be separated by colons) (default: None)
198+
-a {public,random}, --address-type {public,random}
199+
BLE address type, only relevant on Windows, ignored otherwise (default: public)
200+
-s SERVICE_UUID, --service-uuid SERVICE_UUID
201+
The service used for scanning of potential devices (default: None)
193202
-w WRITE_UUID, --write-uuid WRITE_UUID
194-
The GATT characteristic to write the serial data, you might use "ble-scan -d" to find it out
195-
(default: None)
203+
The GATT characteristic to write the serial data, you might use "ble-scan -d" to find it out (default: None)
196204
-r READ_UUID, --read-uuid READ_UUID
197205
The GATT characteristic to subscribe to notifications to read the serial data (default: None)
198-
--permit {ro,rw,wo} Restrict transfer direction on bluetooth: read only (ro), read+write (rw), write only (wo)
199-
(default: rw)
206+
--permit {ro,rw,wo} Restrict transfer direction on bluetooth: read only (ro), read+write (rw), write only (wo) (default: rw)
207+
208+
logging options:
200209
-l FILENAME, --log FILENAME
201210
Enable optional logging of all bluetooth traffic to file (default: None)
202-
-b, --binary Log data as raw binary, disable transformation to hex. Works only in combination with -l
203-
(default: False)
211+
-b, --binary Log data as raw binary, disable transformation to hex. Works only in combination with -l (default: False)
212+
204213
```
205-
Only the device address is always required:
214+
In any case it needs to know which device to connect, the simple and most reliable way to specify this is by device address/id:
206215
```console
207216
$ ble-serial -d 20:91:48:4c:4c:54
208-
18:36:09.255 | INFO | linux_pty.py: Slave created on /tmp/ttyBLE -> /dev/pts/7
209-
18:36:09.255 | INFO | ble_interface.py: Receiver set up
210-
18:36:09.258 | INFO | ble_interface.py: Trying to connect with 20:91:48:4C:4C:54
211-
18:36:12.291 | INFO | ble_interface.py: Device 20:91:48:4C:4C:54 connected
212-
18:36:12.637 | INFO | main.py: Running main loop!
217+
20:38:31.271 | INFO | linux_pty.py: Slave created on /tmp/ttyBLE -> /dev/pts/5
218+
20:38:31.271 | INFO | ble_interface.py: Receiver set up
219+
20:38:31.485 | INFO | ble_interface.py: Trying to connect with 20:91:48:4C:4C:54: UT61E - JK
220+
20:38:32.844 | INFO | ble_interface.py: Device 20:91:48:4C:4C:54 connected
221+
20:38:32.844 | INFO | ble_interface.py: Found write characteristic 0000ffe1-0000-1000-8000-00805f9b34fb (H. 17)
222+
20:38:32.844 | INFO | ble_interface.py: Found notify characteristic 0000ffe1-0000-1000-8000-00805f9b34fb (H. 17)
223+
20:38:33.128 | INFO | main.py: Running main loop!
224+
213225
```
214226
This log shows a successful start on Linux, the virtual serial port was opened on `/dev/pts/8`, the number at the end changes, depending on how many pseudo terminals are already open on the system. It uses the same mechanism on macOS, just the path is slightly different, in the format `/dev/ttys000`.
215227
In addition it automatically creates a symlink to `/tmp/ttyBLE`, so you can easily access it always on the same file, the default can be changed with the `-p`/`--port` option.
@@ -219,7 +231,20 @@ On Windows it uses the port pair created in the setup described above, this does
219231
Now it is possible to use any serial monitor program, just connect to that port, baud rate etc. does not matter, it will work with any value (settings are ignored, because it is only virtual).
220232
The software acts as transparent bridge, everything that is sent to that virtual port gets transmitted to the BLE module and comes out of the TX pin there. Same in the other direction, everything that the BLE module receives on the RX pin gets transmitted to the PC and shows up in the virtual serial port. This makes it also possible to add ble module to create a wireless serial connection with existing hard/software.
221233

222-
As mentioned before, the start might fail because the UUID is not in the list, then you can manually specify the correct write characteristic UUID like this:
234+
Another way to find a device is by service uuid:
235+
```console
236+
$ ble-serial -s 0000ffe0-0000-1000-8000-00805f9b34fb
237+
20:35:41.964 | INFO | linux_pty.py: Slave created on /tmp/ttyBLE -> /dev/pts/5
238+
20:35:41.964 | INFO | ble_interface.py: Receiver set up
239+
20:35:41.964 | WARNING | ble_interface.py: Picking first device with matching service, consider passing a specific device address, especially if there could be multiple devices
240+
20:35:42.308 | INFO | ble_interface.py: Trying to connect with 20:91:48:4C:4C:54: UT61E - JK
241+
20:35:43.020 | INFO | ble_interface.py: Device 20:91:48:4C:4C:54 connected
242+
...
243+
```
244+
This has the advantage that you can replace the device/chip without changing anything in the host script, it will automatically pick the first device that provides the service. It is also possible to add both the service and address, then it will only connect if both match.
245+
On early versions of macOS 12 (Monterey) it is always required to filter with the service uuid, otherwise scanning/connecting won't work.
246+
247+
As mentioned before, the start might fail because the characteristic UUIDs are not in the list, then you can manually specify the correct write UUID like this:
223248
```
224249
$ ble-serial -d 20:91:48:4c:4c:54 -w 0000ffe1-0000-1000-8000-00805f9b34fb
225250
```

0 commit comments

Comments
 (0)