
# Places a call and verifies SCO is working.
# 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_(kBAMServiceHFP)

try:
	if not y.pairWithDevice_service_(deviceAddress, y.enabledServices()):
		raise Exception("Failed to pair with HFP.")
	
	print "Calling Harvard Sentences."
	if not y.hfp().call_("8586515050"):
		raise Exception("Failed to call 8586515050.")
	
	print "Leaving the call idle for 5 seconds."
	time.sleep(5)
	
	if not y.hfp().audioConnected():
		raise Exception("SCO is not up after 5 seconds.")
	
	print "Ending the call."
	if not y.hfp().endCall():
		raise Exception("Failed to end the call.")
	
	print "Leaving the phone idle for 5 seconds."
	time.sleep(5)
	
	if y.hfp().audioConnected():
		raise Exception("SCO is still up 5 seconds after ending the call.")
except:
	y.unpairFromDevice_(deviceAddress)
	raise

y.unpairFromDevice_(deviceAddress)
