Last active
November 26, 2017 07:53
-
-
Save WayneKeenan/8ce4d841158909ad441dc7312637a064 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# python3 -m venv my-venv | |
# source my-venv/bin/activate | |
# pip3 install --upgrade pip setuptools wheel | |
# python3 dispatch_test.py | |
# deactivate | |
import ctypes | |
import objc | |
from PyObjCTools import AppHelper | |
import CoreBluetooth | |
NULL_PTR = ctypes.POINTER(ctypes.c_int)() | |
lib = ctypes.cdll.LoadLibrary("/usr/lib/system/libdispatch.dylib") | |
_dispatch_queue_create = lib.dispatch_queue_create | |
_dispatch_queue_create.argtypes = [ctypes.c_char_p, ctypes.c_void_p] # 2nd param is stuct, but we don't use it. | |
_dispatch_queue_create.restype = ctypes.c_void_p | |
def dispatch_queue_create(name): | |
b_name = name.encode('utf-8') | |
c_name = ctypes.c_char_p(b_name) | |
return _dispatch_queue_create(c_name, NULL_PTR) | |
class DispatchTest(): | |
def __init__(self): | |
try: | |
with objc.autorelease_pool(): | |
dispatch_queue = dispatch_queue_create('blesonq') | |
queue_ptr = objc.objc_object(c_void_p=dispatch_queue) | |
manager = CoreBluetooth.CBCentralManager.alloc() | |
manager.initWithDelegate_queue_options_(self, queue_ptr, None) | |
manager.scanForPeripheralsWithServices_options_(None, None) | |
AppHelper.runConsoleEventLoop(installInterrupt=True) | |
except Exception as e: | |
print(e) | |
# CoreBluetooth CentralManager Protocol | |
def centralManagerDidUpdateState_(self, manager): | |
print(manager.state()) | |
def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(self, manager, peripheral, data, rssi): | |
print(peripheral) | |
DispatchTest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment