Skip to content

Commit

Permalink
Fixes hbldh#78
Browse files Browse the repository at this point in the history
Modified RSSI property for BLEDevice. Fixed for BlueZ.
Updated documentation some.
  • Loading branch information
hbldh committed Jun 30, 2019
1 parent 1aa78aa commit cbaacf5
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 79 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ Tips

To run a subset of tests::

$ py.test tests.test_bleak
$ py.test tests.test_bleak

11 changes: 11 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
History
=======

0.4.2 (2019-06-30)
------------------

* Fix for #76
* Fix for #69
* Fix for #74
* Fix for #68
* Fix for #70
* Fix for
* Merged #66


0.4.2 (2019-05-17)
------------------
Expand Down
4 changes: 1 addition & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
bleak
=====

.. image:: https://www.dropbox.com/s/fm0670e9yrmwr5t/Bleak_logo.png?raw=1
.. image:: https://raw.githubusercontent.com/hbldh/bleak/master/Bleak_logo.png
:target: https://github.com/hbldh/bleak
:alt: Bleak Logo
:scale: 50%

|
.. image:: https://dev.azure.com/hbldh/github/_apis/build/status/hbldh.bleak?branchName=master
:target: https://dev.azure.com/hbldh/github/_build/latest?definitionId=4&branchName=master

Expand Down
10 changes: 10 additions & 0 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ async def write_gatt_char(
"""
characteristic = self.services.get_characteristic(str(_uuid))

if ("write" not in characteristic.properties and "write-without-response" not in characteristic.properties):
raise BleakError("Characteristic %s does not support write operations!" % str(_uuid))
if not response and "write-without-response" not in characteristic.properties:
response = True
# Force response here, since the device only supports that.
if response and "write" not in characteristic.properties and "write-without-response" in characteristic.properties:
response = False
logger.warning("Characteristic %s does not support Write with response. Trying without..." % str(_uuid))

if response or (self._bluez_version[0] == 5 and self._bluez_version[1] > 50):
# TODO: Add OnValueUpdated handler for response=True?
await self._bus.callRemote(
Expand Down
33 changes: 22 additions & 11 deletions bleak/backends/bluezdbus/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ def _filter_on_device(objs):


def _device_info(path, props):
name = props.get("Name", props.get("Alias", path.split("/")[-1]))
address = props.get("Address", None)
if address is None:
try:
address = path[-17:].replace("_", ":")
if not validate_mac_address(address):
try:
name = props.get("Name", props.get("Alias", path.split("/")[-1]))
address = props.get("Address", None)
if address is None:
try:
address = path[-17:].replace("_", ":")
if not validate_mac_address(address):
address = None
except Exception:
address = None
except Exception:
address = None
rssi = props.get("RSSI", "?")
return name, address, rssi, path
rssi = props.get("RSSI", "?")
return name, address, rssi, path
except Exception as e:
logger.exception(e, exc_info=True)
return None, None, None, None


async def discover(timeout=5.0, loop=None, **kwargs):
Expand Down Expand Up @@ -96,6 +100,13 @@ def parse_msg(message):
devices[msg_path] = (
{**devices[msg_path], **changed} if msg_path in devices else changed
)
elif message.member == "InterfacesRemoved" and message.body[1][0] == defs.BATTERY_INTERFACE:
logger.info(
"{0}, {1} ({2}): {3}".format(
message.member, message.interface, message.path, message.body
)
)
return
else:
msg_path = message.path
logger.info(
Expand Down Expand Up @@ -186,6 +197,6 @@ def parse_msg(message):
name, address, _, path = _device_info(path, props)
uuids = props.get("UUIDs", [])
manufacturer_data = props.get('ManufacturerData', {})
discovered_devices.append(BLEDevice(address, name, path, uuids=uuids,
discovered_devices.append(BLEDevice(address, name, {"path": path, "props": props}, uuids=uuids,
manufacturer_data=manufacturer_data))
return discovered_devices
14 changes: 9 additions & 5 deletions bleak/backends/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class BLEDevice(object):
- When using Windows backend, `details` attribute is a
`Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisement` object.
- When using Linux backend, `details` attribute is a
string path to the DBus device object.
dict with keys `path` which has the string path to the DBus device object and `props`
which houses the properties dictionary of the D-Bus Device.
- When using macOS backend, `details` attribute will be
something else.
Expand All @@ -31,10 +32,13 @@ def __init__(self, address, name, details=None, **kwargs):
@property
def rssi(self):
"""Get the signal strength in dBm"""
try:
return int(self.details.RawSignalStrengthInDBm)
except Exception as e:
return None
if isinstance(self.details, dict) and "props" in self.details:
rssi = self.details["props"].get("RSSI", 0) # Should not be set to 0...
elif hasattr(self.details, "RawSignalStrengthInDBm"):
rssi = self.details.RawSignalStrengthInDBm
else:
rssi = None
return int(rssi) if rssi is not None else None

def __str__(self):
if self.name == "Unknown":
Expand Down
4 changes: 1 addition & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
bleak
=====

.. image:: https://www.dropbox.com/s/fm0670e9yrmwr5t/Bleak_logo.png?raw=1
.. image:: https://raw.githubusercontent.com/hbldh/bleak/master/Bleak_logo.png
:target: https://github.com/hbldh/bleak
:alt: Bleak Logo
:width: 50%

|
.. image:: https://dev.azure.com/hbldh/github/_apis/build/status/hbldh.bleak?branchName=master
:target: https://dev.azure.com/hbldh/github/_build/latest?definitionId=4&branchName=master

Expand Down
56 changes: 0 additions & 56 deletions examples/szymon_2.py

This file was deleted.

0 comments on commit cbaacf5

Please sign in to comment.