Skip to content

Commit

Permalink
Add BLE support with 'bleak'
Browse files Browse the repository at this point in the history
  • Loading branch information
yhql committed Mar 3, 2023
1 parent 48e6e7b commit 3ba98a8
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ $ ledgerctl -v run Bitcoin
=> e0d8000007426974636f696e
<= 9000
```

### Using BLE

BLE scanning is disabled by default. It can be activated by setting an environment variable named `LEDGER_USE_BLE`.
Expand All @@ -159,4 +160,3 @@ And executed with:
```console
pre-commit run --all-files
```

4 changes: 3 additions & 1 deletion ledgerwallet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class NoLedgerDeviceException(Exception):

class LedgerClient(object):
def __init__(self, device=None, cla=0xE0, private_key=None):
self.device = None
if device is None:
devices = enumerate_devices()
if len(devices) == 0:
Expand All @@ -184,7 +185,8 @@ def __del__(self):
self.close()

def close(self):
self.device.close()
if self.device is not None:
self.device.close()

def raw_exchange(self, data: bytes) -> bytes:
LOG.debug("=> " + data.hex())
Expand Down
2 changes: 1 addition & 1 deletion ledgerwallet/transport/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from contextlib import contextmanager

from .device import Device
from .ble import BleDevice
from .device import Device
from .hid import HidDevice
from .tcp import TcpDevice

Expand Down
5 changes: 3 additions & 2 deletions ledgerwallet/transport/ble.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio
import os
from typing import List

from bleak import BleakClient, BleakScanner
from bleak.exc import BleakError
from typing import List

HANDLE_CHAR_ENABLE_NOTIF = 13
HANDLE_CHAR_WRITE = 16
Expand All @@ -13,7 +14,7 @@


async def ble_discover():
devices = await BleakScanner.discover(timeout=1.)
devices = await BleakScanner.discover(timeout=1.0)
return devices


Expand Down

0 comments on commit 3ba98a8

Please sign in to comment.