-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Jakeler/bleak-backend
Switch to Bleak backend
- Loading branch information
Showing
22 changed files
with
646 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "bluepy-repo"] | ||
path = bluepy-repo | ||
url = https://github.com/edwios/bluepy.git | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import platform | ||
|
||
if platform.system() == 'Linux': | ||
from ble_serial.ports.linux_pty import UART as platform_uart | ||
elif platform.system() == 'Windows': | ||
from ble_serial.ports.windows_com0com import COM as platform_uart | ||
else: | ||
raise Exception('Platform not supported!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,4 @@ | ||
import logging, sys, argparse, time | ||
from ble_serial.virtual_serial import UART | ||
from ble_serial.interface import BLE_interface | ||
from ble_serial.fs_log import FS_log, Direction | ||
from bluepy.btle import BTLEDisconnectError | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
description='Create virtual serial ports from BLE devices.') | ||
|
||
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', | ||
help='Increase verbosity to log all data going through') | ||
parser.add_argument('-d', '--dev', dest='device', required=True, | ||
help='BLE device address to connect (hex format, can be seperated by colons)') | ||
parser.add_argument('-t', '--address-type', dest='addr_type', required=False, choices=['public', 'random'], default='public', | ||
help='BLE address type, either public or random') | ||
parser.add_argument('-i', '--interface', dest='adapter', required=False, default='0', | ||
help='BLE host adapter number to use') | ||
parser.add_argument('-w', '--write-uuid', dest='write_uuid', required=False, | ||
help='The GATT chracteristic to write the serial data, you might use "scan.py -d" to find it out') | ||
parser.add_argument('-l', '--log', dest='filename', required=False, | ||
help='Enable optional logging of all bluetooth traffic to file') | ||
parser.add_argument('-b', '--binary', dest='binlog', required=False, action='store_true', | ||
help='Log data as raw binary, disable transformation to hex. Works only in combination with -l') | ||
parser.add_argument('-p', '--port', dest='port', required=False, default='/tmp/ttyBLE', | ||
help='Symlink to virtual serial port') | ||
parser.add_argument('-r', '--read-uuid', dest='read_uuid', required=False, | ||
help='The GATT characteristic to subscribe to notifications to read the serial data') | ||
args = parser.parse_args() | ||
|
||
logging.basicConfig( | ||
format='%(asctime)s.%(msecs)03d | %(levelname)s | %(filename)s: %(message)s', | ||
datefmt='%H:%M:%S', | ||
level=logging.DEBUG if args.verbose else logging.INFO | ||
) | ||
|
||
try: | ||
uart = UART(args.port) | ||
bt = BLE_interface(args.device, args.addr_type, args.adapter, args.write_uuid, args.read_uuid) | ||
if args.filename: | ||
log = FS_log(args.filename, args.binlog) | ||
bt.set_receiver(log.middleware(Direction.BLE_IN, uart.write_sync)) | ||
uart.set_receiver(log.middleware(Direction.BLE_OUT, bt.send)) | ||
else: | ||
bt.set_receiver(uart.write_sync) | ||
uart.set_receiver(bt.send) | ||
logging.info('Running main loop!') | ||
uart.start() | ||
while True: | ||
bt.receive_loop() | ||
except BTLEDisconnectError as e: | ||
logging.warning(f'Bluetooth connection failed') | ||
except KeyboardInterrupt: | ||
logging.info('Keyboard interrupt received') | ||
except Exception as e: | ||
logging.error(f'Unexpected Error: {e}') | ||
finally: | ||
logging.warning('Shutdown initiated') | ||
if 'uart' in locals(): | ||
uart.stop() | ||
if 'bt' in locals(): | ||
bt.shutdown() | ||
if 'log' in locals(): | ||
log.finish() | ||
logging.info('Shutdown complete.') | ||
exit(0) | ||
from ble_serial.main import launch | ||
|
||
if __name__ == '__main__': | ||
main() | ||
launch() |
Oops, something went wrong.