-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmac_bt.py
187 lines (141 loc) · 6.08 KB
/
mac_bt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from pprint import pprint
import ctypes
import ctypes.util
import objc
import AppKit
iokit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('IOKit'))
cf = ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreFoundation'))
kIOMasterPortDefault = ctypes.c_void_p.in_dll(iokit, "kIOMasterPortDefault")
kCFAllocatorDefault = ctypes.c_void_p.in_dll(cf, "kCFAllocatorDefault")
kCFStringEncodingMacRoman = 0
iokit.IORegistryEntryCreateCFProperty.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint32]
iokit.IORegistryEntryCreateCFProperty.restype = ctypes.c_void_p
cf.CFStringCreateWithCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int32]
cf.CFStringCreateWithCString.restype = ctypes.c_void_p
cf.CFDataGetLength.argtypes = [ctypes.c_void_p]
cf.CFDataGetLength.restype = ctypes.c_int
try:
# mac os 10.5 loads frameworks using bridgesupport metadata
'''
__bundle__ = objc.initFrameworkWrapper("IOBluetoothUI",
frameworkIdentifier="com.apple.IOBluetoothUI",
frameworkPath=objc.pathForFramework(
"/System/Library/Frameworks/IOBluetoothUI.framework"),
globals=globals())
'''
IOBluetoothUI = objc.ObjCLazyModule("IOBluetoothUI",
frameworkIdentifier="com.apple.IOBluetoothUI",
frameworkPath=objc.pathForFramework(
"/System/Library/Frameworks/IOBluetoothUI.framework"),
metadict=globals())
IOBluetooth = objc.ObjCLazyModule("IOBluetooth",
frameworkIdentifier="com.apple.IOBluetooth",
frameworkPath=objc.pathForFramework(
"/System/Library/Frameworks/IOBluetooth.framework"
),
metadict=globals())
IOKit = objc.ObjCLazyModule("IOKit",
frameworkIdentifier="com.apple.IOKit",
frameworkPath=objc.pathForFramework(
"/System/Library/Frameworks/IOKit.framework"
),
metadict=globals())
except (AttributeError, ValueError) as x:
pprint(x)
del objc
def formatdevaddr(addr):
"""
Returns address of a device in usual form e.g. "00:00:00:00:00:00"
- addr: address as returned by device.getAddressString() on an
IOBluetoothDevice
"""
# make uppercase cos PyS60 & Linux seem to always return uppercase
# addresses
# can safely encode to ascii cos BT addresses are only in hex (pyobjc
# returns all strings in unicode)
return addr.replace("-", ":").encode('ascii').upper()
def _getdevicetuple(iobtdevice):
"""
Returns an (addr, name, COD) device tuple from a IOBluetoothDevice object.
"""
addr = formatdevaddr(iobtdevice.getAddressString())
name = iobtdevice.getName()
cod = iobtdevice.getClassOfDevice()
return (addr, name, cod)
def selectdevice():
gui = IOBluetoothUI.IOBluetoothDeviceSelectorController.deviceSelector()
SERIAL_SERVICE = 0x1101
# try to bring GUI to foreground by setting it as floating panel
# (if this is called from pyobjc app, it would automatically be in foreground)
try:
gui.window().setFloatingPanel_(True)
except:
pass
# only allow devices with Serial service
uuid = IOBluetooth.IOBluetoothSDPUUID.uuid16_(SERIAL_SERVICE)
gui.addAllowedUUID_(uuid)
# show the window and wait for user's selection
response = gui.runModal() # problems here if transferring a lot of data??
if response == AppKit.NSRunStoppedResponse:
results = gui.getResults()
if len(results) > 0: # should always be > 0, but check anyway
devinfo = _getdevicetuple(results[0])
# sometimes the baseband connection stays open which causes
# problems with connections w so close it here, see if this fixes
# it
dev = results[0]
if dev is not None:
name = dev.getName()
disp_name = dev.getDisplayName()
dev_class = [dev.getDeviceClassMajor(), dev.getDeviceClassMinor()]
print('Name:', name)
print('DispName:', disp_name)
print('DeviceClass', 'Major:', dev_class[0], 'Minor:', dev_class[1])
pprint(devinfo)
dev = IOBluetooth.IOBluetoothDevice.withAddressString_(devinfo[0])
if dev is not None and dev.isConnected():
dev.closeConnection()
return devinfo
# user cancelled selection
return None
class CFRange(ctypes.Structure):
_fields_ = [("location", ctypes.c_int), ("length", ctypes.c_int)]
def get_bytes_property(device_type, property):
"""
Search the given device for the specified string property
@param device_type Type of Device
@param property String to search for
@return Python string containing the value, or None if not found.
"""
key = cf.CFStringCreateWithCString(
kCFAllocatorDefault,
property.encode("mac_roman"),
kCFStringEncodingMacRoman)
CFContainer = iokit.IORegistryEntryCreateCFProperty(
device_type,
key,
kCFAllocatorDefault,
0)
output = None
print(type(CFContainer))
if CFContainer:
cf_range = CFRange()
cf_range.location = 0
print(cf_range.length)
cf_range.length = cf.CFDataGetLength(CFContainer)
cf.CFDataGetBytePtr.argtypes = [ctypes.c_void_p]
cf.CFDataGetBytePtr.restype = ctypes.POINTER(ctypes.c_uint8)
output = list(range(cf_range.length))
out_ptr = cf.CFDataGetBytePtr(CFContainer)
# 0, cf.CFDataGetLength(CFContainer))
for i in range(0, cf_range.length):
output[i] = out_ptr[i]
print(out_ptr)
print(cf_range.length)
print(output)
#cf.CFDataGetBytes(CFContainer, cf_range, out_ptr)
#output = cf.CFStringGetCStringPtr(CFContainer, 0)
#if output is not None:
# output = output.decode('mac_roman')
#cf.CFRelease(CFContainer)
return output