| 1 | ''' |
| 2 | Module of MacOS API for plyer.bluetooth. |
| 3 | ''' |
| 4 | |
| 5 | from subprocess import Popen, PIPE |
| 6 | from plyer.facades import Bluetooth |
| 7 | from plyer.utils import whereis_exe |
| 8 | |
| 9 | from os import environ |
| 10 | |
| 11 | |
| 12 | class OSXBluetooth(Bluetooth): |
| 13 | ''' |
| 14 | Implementation of MacOS bluetooth API. |
| 15 | ''' |
| 16 | |
| 17 | def _get_info(self): |
| 18 | old_lang = environ.get('LANG') |
| 19 | environ['LANG'] = 'C' |
| 20 | |
| 21 | sys_profiler_process = Popen( |
| 22 | ["system_profiler", "SPBluetoothDataType"], |
| 23 | stdout=PIPE |
| 24 | ) |
| 25 | |
| 26 | stdout = sys_profiler_process.communicate()[0].decode('utf-8') |
| 27 | output = stdout.splitlines() |
| 28 | |
| 29 | lines = [] |
| 30 | for line in output: |
| 31 | if 'Bluetooth Power' not in line: |
| 32 | continue |
| 33 | lines.append(line) |
| 34 | |
| 35 | if old_lang is None: |
| 36 | environ.pop('LANG') |
| 37 | else: |
| 38 | environ['LANG'] = old_lang |
| 39 | |
| 40 | if output and len(lines) == 1: |
| 41 | return lines[0].split()[2] |
| 42 | else: |
| 43 | return None |
| 44 | |
| 45 | |
| 46 | def instance(): |
| 47 | ''' |
| 48 | Instance for facade proxy. |
| 49 | ''' |
| 50 | import sys |
| 51 | if whereis_exe('system_profiler'): |
| 52 | return OSXBluetooth() |
| 53 | sys.stderr.write("system_profiler not found.") |
| 54 | return Bluetooth() |