#!/usr/bin/env python

import dbus
import subprocess
import sys

# Replace this with the bluetooth address of the Bluetooth Network Access Point
dev_bdaddr = '12_34_56_AB_CD_EF'

# dhcp_client = ['/sbin/dhclient', '-v']
dhcp_client = ['/sbin/udhcpc', '-i']

def main():
  bus = dbus.SystemBus()
  bluez_proxy = bus.get_object('org.bluez', '/')
  bluez_manager = dbus.Interface(bluez_proxy, 'org.bluez.Manager')
  adapter = bluez_manager.DefaultAdapter()
  # adapter_proxy = bus.get_object('org.bluez', adapter)
  dev_proxy = bus.get_object('org.bluez', '%s/dev_%s' % (adapter, dev_bdaddr))
  adapter_network = dbus.Interface(dev_proxy, 'org.bluez.Network')
  # adapter_introspect = dbus.Interface(dev_proxy, 'org.freedesktop.DBus.Introspectable')
  # print adapter_introspect.Introspect()
  # print adapter_network.Disconnect()
  net_interface = adapter_network.Connect('NAP') # 'GN' / 'NAP' ?
  print '%s created' % net_interface

  dhcp = subprocess.Popen(dhcp_client + [net_interface], stdout=sys.stdout)

  raw_input('Press enter to close connection\n')

  dhcp.kill()
  dhcp.wait()

if __name__ == '__main__':
  main()

Add script to bring up bluetooth network
Add script to bring up bluetooth network

<rant>
I haven't managed to find any good documentation on this (or pretty much
anything bluez 4.x related) at all (the blueman source implements this,
but it's not the easiest source in the world to follow) - this was
created after a lot of trial and error staring at the bluez dbus
interface in d-feet until I saw the bnep0 interface appear! Only for it
to disappear a moment later :(

Unfortunately, it turns out that using dbus-send is not an option
because bluez removes the bnep0 interface the moment the dbus client
goes away (WHY???). I'm beginning to think that the bluez 4.x developers
are purposefully trying to make the console experience as unusable as
possible... For the love of god would someone please implement a curses
version of blueman... Please?
</rant>

So... Here's the python version that keeps the dbus connection open
until enter is pressed so that the bnep0 interface doesn't suddenly go
away.

This is a very simple script that currently only handles a pretty
specific scenario - where the box this is run on is connecting to
another device that is acting as a bluetooth network access point and is
running a dhcp server.

I'm specifically using it to connect to my Optus MyTab (which is my
primary Internet connection at home) without having to pull up blueman
every time. I also want to use this to allow my N9 (with a voice SIM) to
easily use the Internet from the tablet when I'm out and about without
having to activate the WiFi tethering (which uses more power, is
unreliable unless something is holding a wakelock and requires prior
manual activation from the tablet).

One problem I've found using bluetooth networking is that only one
device appears to be able to connect to my tablet at a time... AFAIK
this is NOT an inherent limitation of bluetooth networking, so it's
possible I'm doing somthing wrong (roles?), or CM7 only allows one
device to connect (or more specifically, to DHCP) at a time... More
investigation required.

The bluetooth address of the device to connect to is currently
hard-coded in the script.

To use this on a Nokia N9 (eventually I want to package this up and
figure out how to add a plugin to the N9's network manager, but for
now):

1. Install python-dbus with:
apt-get install python-dbus

2. Remove 'network' from the 'DisablePlugins' line in:
/etc/bluetooth/main.conf

3. Restart the bluetooth daemon with:
pkill -SIGHUP bluetoothd

4. Pair to the device to connect to via the bluetooth menu in the N9

5. Edit the bluetooth address in the script

6. Run the script from the terminal under develsh:
develsh -c ./blue-tether.py

7. If using an app that does not recognise that an internet connection
is present and insists on bringing up the network manager, create a new
dummy Ad-Hoc wireless network with static IP, etc. all set to 0.0.0.0

Signed-off-by: Ian Munsie <ce389fd2fa887d610219cb85dea6dc9451973e74@gmail.com>
