unable to start device in Accessory mode
Hi, Iam trying to send and receive messages from my python script to android application by connecting my phone via USB, iam trying to achieve it through Accessory mode.
I have a python script to acheive the accessory mode but when i run the code the mobile gets disconnected after the set_accessory_mode () function.(FYI i get this problem only on windows, this code works fine in linux and iam able to achieve accessory mode)
Here is my python script that i have used to start my device in accessory mode, The problem iam facing with this code is the the mobile gets disconnected after the set_accessory_mode () function , when i try to query the connected usb devices after the set_accessory_mode () function it return an empty list. Can any of you help me with this issue, Or provide me another way to start accessory mode through python script.
(FYI i get this problem only on windows, the same code works fine in linux and mac OS and it says that the device already configured, should be ok and iam able to proceed)
import usb.core
import usb.util
import sys
import time
import random
VID_GALAXY_NEXUS_DEBUG = 0x04e8
PID_GALAXY_NEXUS_DEBUG = 0x6860
VID_ANDROID_ACCESSORY = 0x18d1
PID_ANDROID_ACCESSORY = 0x2d01
def get_accessory():
print('Looking for Android Accessory')
print('VID: 0x%0.4x - PID: 0x%0.4x'
% (VID_ANDROID_ACCESSORY, PID_ANDROID_ACCESSORY))
dev = usb.core.find(idVendor=VID_ANDROID_ACCESSORY,
idProduct=PID_ANDROID_ACCESSORY)
return dev
def get_android_device():
print('Looking for Galaxy Nexus')
print('VID: 0x%0.4x - PID: 0x%0.4x'
% (VID_GALAXY_NEXUS_DEBUG, PID_GALAXY_NEXUS_DEBUG))
android_dev = usb.core.find(idVendor=VID_GALAXY_NEXUS_DEBUG,
idProduct=PID_GALAXY_NEXUS_DEBUG)
if android_dev:
print('Samsung Galaxy Nexus (debug) found')
else:
sys.exit('No Android device found')
return android_dev
def set_protocol(ldev):
try:
ldev.set_configuration()
except usb.core.USBError as e:
if e.errno == 16:
print('Device already configured, should be OK')
else:
sys.exit('Configuration failed')
ret = ldev.ctrl_transfer(0xC0, 51, 0, 0, 2)
protocol = ret[0]
print('Protocol version: %i' % protocol)
if protocol < 2:
sys.exit('Android Open Accessory protocol v2 not supported')
return
def set_strings(ldev):
send_string(ldev, 0, 'Samsung Electronics Co., Ltd')
send_string(ldev, 1, 'Galaxy series')
send_string(ldev, 2, 'A Python based Android accessory')
send_string(ldev, 3, 'RFCN20933LZ')
send_string(ldev, 4,
'https://github.com/Arn-O/py-android-accessory/')
send_string(ldev, 5, 'RFCN20933LZ')
return
def set_accessory_mode(ldev):
ret = ldev.ctrl_transfer(0x40, 53, 0, 0, '', 0)
if ret:
sys.exit('Start-up failed')
time.sleep(1)
return
def send_string(ldev, str_id, str_val):
ret = ldev.ctrl_transfer(0x40, 52, 0, str_id, str_val, 0)
if ret != len(str_val):
sys.exit('Failed to send string %i' % str_id)
return
def start_accessory_mode():
dev = get_accessory()
if not dev:
print('Android accessory not found')
print('Try to start accessory mode')
dev = get_android_device()
set_protocol(dev)
set_strings(dev)
set_accessory_mode(dev)
dev = get_accessory()
if not dev:
sys.exit('Unable to start accessory mode')
print('Accessory mode started')
return dev
def main():
dev = start_accessory_mode()
if __name__ == '__main__':
main()
Output:
Looking for Android Accessory
VID: 0x18d1 - PID: 0x2d01
Android accessory not found
Try to start accessory mode
Looking for Galaxy Nexus
VID: 0x04e8 - PID: 0x6860
Samsung Galaxy Nexus (debug) found
Protocol version: 2
Looking for Android Accessory
VID: 0x18d1 - PID: 0x2d01
SystemExit: Unable to start accessory mode
Do help me by pointing out where i have gone wrong, or provide me another way by which i can start the device in accessory mode through python. Thankyou.