]>
code.delx.au - monosys/blob - hacks/bt-dun-connect
13 1. Pair your bluetooth device, use bluetoothctl
14 2. Use 'sdptool search DUN' to find the bluetooth channel
15 3. Save your configuration to ~/.config/bt-dun-connect.json
19 Example configuration:
22 "bluetooth_addr": "DE:AD:BE:EE:EE:EF",
23 "bluetooth_channel": "22"
29 class DiallerException(Exception):
32 class BluetoothDialler(object):
33 def __init__(self
, rfcomm_id
, bt_addr
, bt_channel
, apn
):
34 self
.rfcomm_id
= rfcomm_id
35 self
.bt_addr
= bt_addr
36 self
.bt_channel
= bt_channel
41 self
.wvdial_conf_name
= None
42 self
.dbus_system
= None
47 self
.wvdial
.terminate()
49 except Exception as e
:
54 self
.rfcomm
.terminate()
56 except Exception as e
:
59 if self
.wvdial_conf_name
:
61 os
.unlink(self
.wvdial_conf_name
)
62 except Exception as e
:
66 reset_rfcomm(self
.rfcomm_id
)
67 except Exception as e
:
72 self
.disconnect_bluetooth()
73 except Exception as e
:
78 self
.dbus_system
= dbus
.SystemBus()
80 def enable_bluetooth(self
):
81 bluez
= self
.dbus_system
.get_object("org.bluez", "/org/bluez/hci0")
82 iprops
= dbus
.Interface(bluez
, "org.freedesktop.DBus.Properties")
83 iprops
.Set("org.bluez.Adapter1", "Powered", True)
85 def disconnect_bluetooth(self
):
86 path
= self
.bt_addr
.upper().replace(":", "_")
87 bluez_dev
= self
.dbus_system
.get_object("org.bluez", "/org/bluez/hci0/dev_" + path
)
88 idev
= dbus
.Interface(bluez_dev
, "org.bluez.Device1")
91 def connect_rfcomm(self
):
92 self
.rfcomm
= subprocess
.Popen([
100 # poll until connected
102 while time
.time() - start
< 10:
103 if self
.is_rfcomm_connected():
106 raise DiallerException("Timeout connecting rfcomm")
108 def is_rfcomm_connected(self
):
109 output
= subprocess
.check_output(["rfcomm", "-a"])
110 for line
in output
.decode("ascii").split("\n"):
111 if not line
.startswith("rfcomm%s:" % self
.rfcomm_id
):
113 if line
.find(" connected ") >= 0:
117 def write_wvdial_conf(self
):
118 fd
, self
.wvdial_conf_name
= tempfile
.mkstemp()
119 f
= os
.fdopen(fd
, "w")
125 Init2 = AT+CGDCONT=1,"IP","%s"
132 def connect_wvdial(self
):
133 self
.wvdial
= subprocess
.Popen([
134 "wvdial", "-C", self
.wvdial_conf_name
143 print("Enabling bluetooth...")
144 self
.enable_bluetooth()
146 print("Connecting rfcomm...")
147 self
.connect_rfcomm()
148 self
.write_wvdial_conf()
151 self
.connect_wvdial()
153 except KeyboardInterrupt as e
:
155 except DiallerException
as e
:
161 def get_next_rfcomm_id():
162 # for now always use rfcomm0
166 def reset_rfcomm(rfcomm_id
):
167 subprocess
.call(["rfcomm", "release", rfcomm_id
], stderr
=open("/dev/null"))
169 def read_config(filename
):
171 config
= open(os
.path
.expanduser(filename
))
173 print("Failed to open config file: %s" % e
)
177 return json
.load(config
)
178 except ValueError as e
:
179 print("Failed to parse config file %s: %s" % (filename
, e
))
184 rfcomm_id
= get_next_rfcomm_id()
185 config
= read_config("~/.config/bt-dun-connect.json")
186 dialler
= BluetoothDialler(
188 bt_addr
=config
["bluetooth_addr"],
189 bt_channel
=config
["bluetooth_channel"],
195 if __name__
== "__main__":