forked from kstep/simpledbus
/
bluetooth4
executable file
·170 lines (138 loc) · 4.27 KB
/
bluetooth4
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
#!/usr/bin/env lua
--[[ SimpleDBus example script
Get info from the bluetoothd daemon about the different bluetooth adapters,
and then make them all discover remote devices.
Tested with bluez 4.29
--]]
local printlist, printproperties
do
local type, ipairs, write, format, sort =
type, ipairs, io.write, string.format, table.sort
printlist = function(list, spacing)
if not spacing then spacing = '' end
for i = 1, #list do
write(spacing, format('%4i: ', i), list[i], '\n')
end
end
printproperties = function(prop, spacing)
if not spacing then spacing = '' end
local keys, i, width = {}, 0, 0
for k, _ in pairs(prop) do
i = i+1
keys[i] = k
if #k > width then width = #k end
end
sort(keys)
for _, k in ipairs(keys) do
write(spacing, k, ': ')
for i = 1, width - #k do
write(' ')
end
local v = prop[k]
if type(v) == 'table' then v = '...' end
write(tostring(v), '\n')
end
end
end
-- Import the simpledbus module
local DBus = require 'simpledbus'
-- Get the system bus
local dbus = assert(DBus.SystemBus())
-- Make a new proxy object for the / object connected at org.bluez
local bluez = dbus:new_proxy('org.bluez', '/')
-- Add methods manually
bluez:add_method('ListAdapters', 'org.bluez.Manager')
bluez:add_method('DefaultAdapter', 'org.bluez.Manager')
-- Get a list of the adapters and print it
local adapters, msg = bluez:ListAdapters()
if not adapters then
print('Couldn\'t get adapters: '..msg)
os.exit(1)
end
if #adapters == 0 then
print 'No adapters found'
os.exit(0)
end
print('Adapters:')
printlist(adapters)
-- Show the default adapter too
print('\nDefault: '..assert(bluez:DefaultAdapter()))
-- Now go through the adapters, create proxies for each of them
-- and dump lots of information
for i, objpath in ipairs(adapters) do
-- This time we use the auto_proxy() method to
-- parse the introspection xml and create the
-- proxy for us
local a = assert(dbus:auto_proxy('org.bluez', objpath))
adapters[i] = a
print('\n'..objpath..':')
local info = assert(a:GetProperties())
printproperties(info, ' ')
local devices = info.Devices
if devices then
for _, objpath in ipairs(devices) do
print('\n '..objpath..':')
-- This time we don't even create a proxy object,
-- but just use call_method() directly
local info = assert(dbus:call_method(
'org.bluez', -- target
objpath, -- object path
'org.bluez.Device', -- interface
'GetProperties')) -- method name
-- '', -- signature (defaults to '')
-- ...)) -- arguments
printproperties(info, ' ')
end
end
end
-- Define signal handlers
local discovering = 0
local function DeviceFoundFunc(id)
return function(address, prop)
print(id..': Found device')
printproperties(prop, ' ')
end
end
local function PropertyChangedFunc(id, adapter)
return function(name, value)
print(id..': Property changed')
print(' '..name..' -> '..tostring(value))
if name == 'Discovering' then
if value then
discovering = discovering + 1
else
adapter:StopDiscovery()
discovering = discovering - 1
if discovering == 0 then DBus.stop() end
end
end
end
end
-- Register signal handlers
for _, a in ipairs(adapters) do
local id = a.object:gsub('.*/(.*)', '%1')
--[[ You can register for signals like this
assert(dbus:register_signal(a.object,
'org.bluez.Adapter',
'DeviceFound',
DeviceFoundFunc(id)))
--]]
--![[ ..but since auto_proxy() already found
-- much of this information we can just do
assert(dbus:register_auto_signal(a.DeviceFound, DeviceFoundFunc(id)))
--]]
assert(dbus:register_auto_signal(a.PropertyChanged, PropertyChangedFunc(id, a)))
end
-- Start discovery on all adapters
print '\nStarting discovery on all devices:'
for _, a in ipairs(adapters) do
a:StartDiscovery()
end
-- Run the main loop
local r, msg = DBus.mainloop(dbus)
if not r then
print(msg)
os.exit(1)
end
print 'Success!'
-- vi: syntax=lua ts=3 sw=3 et: