
# Sends some keyboard input to the phone.
# BluetoothAutomation

from bluetoothautomation import *
import time
import code

deviceAddress = "<phone_bdaddress>"

# Use this for USB dongles.
# x = iOSUSBProvider.alloc().init()

x = iOSSerialProvider.alloc().initWithAddress_(bytearray((0x00, 0x11, 0x22, 0x33, 0x44, 0x55)))
paths = BAMDevice.allDevicePathsWithTransport_(x)
if len(paths) <= 0:
	raise Exception("Atleast one Bluetooth serial board must be plugged in.")

y = BAMDevice.deviceWithTransport_atPath_(x, paths[0])
y.setAllowConnect_(True)
y.setAllowDiscover_(True)

y.setEnabledServices_(kBAMServiceHID)

try:
    # Initial incoming connect will fail due to <rdar://problem/7831105> [Apex8A239 + BT326 + N90] Incoming HID pairing doesn't result in a HID connection.
	if not y.pairWithDevice_service_(deviceAddress, y.enabledServices()):
		raise Exception("Failed to pair with HID.")
	
	print "Sending http://www.apple.com as keyboard input."
	if not y.hid().sendString_("http://www.apple.com<keyboard_return/>"):
		raise Exception("Failed to send keyboard input to the device.")

except:
	y.unpairFromDevice_(deviceAddress)
	raise

y.unpairFromDevice_(deviceAddress)
