
# Plays music on the phone and displays the A2DP metrics.
# 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_(kBAMServiceA2DP | kBAMServiceAVRCP)

try:
	if not y.pairWithDevice_service_(deviceAddress, y.enabledServices()):
		raise Exception("Failed to pair with A2DP and AVRCP.")
	
	print "Playing music for 5 seconds."
	if not y.avrcp().play_(deviceAddress):
		raise Exception("Failed to play via AVRCP.")
	
	time.sleep(5)

	print "A2DP statistics after 5 sec of music: streaming = %d, rx = %d b/s" % (y.a2dp().streaming(), y.a2dp().throughput())

	print "Pausing music for 5 seconds."
	if not y.avrcp().pause_(deviceAddress):
		raise Exception("Failed to pause via AVRCP.")
	
	time.sleep(5)

	print "A2DP statistics after 5 sec of no music: streaming = %d, rx = %d b/s" %  (y.a2dp().streaming(), y.a2dp().throughput())
except:
	y.unpairFromDevice_(deviceAddress)
	raise

y.unpairFromDevice_(deviceAddress)
