How to connect an android device with ADB server using Python? - python

I am able to connect an Android device with ADB using a USB cable but I want to connect a remote device without a USB cable, and not rooted also. I am running the ADB command through Python. I have been googling for 2 days and most of the resources say we can't do this. If it's not possible, please suggest other options.
Is this possible with a Python script?

Related

Get connected iPhone device info with Python?

I am trying to get my iPhone's device info using Python on Windows,
unfortunately I could not find any package or way to do so.
Does anybody know how to get device info from a connected iPhone with USB using Python?

adb shell input touchscreen tap is not working

I am trying to simulate tap and swipe gesture from adb to my android device. I've tried to run the following command
adb shell input tap 500 500 and adb shell input touchscreen tap 500 500 from windows command prompt. It is giving me the following error,
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.os.Parcel.createException(Parcel.java:2074)
at android.os.Parcel.readException(Parcel.java:2042)
at android.os.Parcel.readException(Parcel.java:1990)
at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:925)
at android.hardware.input.InputManager.injectInputEvent(InputManager.java:886)
at com.android.commands.input.Input.injectMotionEvent(Input.java:428)
at com.android.commands.input.Input.access$200(Input.java:41)
at com.android.commands.input.Input$InputTap.sendTap(Input.java:224)
at com.android.commands.input.Input$InputTap.run(Input.java:218)
at com.android.commands.input.Input.onRun(Input.java:108)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:56)
at com.android.commands.input.Input.main(Input.java:71)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:380)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.input.InputManagerService.injectInputEventInternal(InputManagerService.java:732)
at com.android.server.input.InputManagerService.injectInputEvent(InputManagerService.java:706)
at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:422)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
I've also tried to execute the same command from python script with the pure-python-adb package. The following is script I've exucted.
from ppadb.client import Client
adb = Client()
devices = adb.devices()
if len(devices) == 0:
print("No devices attached")
quit()
device = devices[0]
print(device)
device.shell('input touchscreen tap 700 1453')
I got the following output with no tap noticed on device and no error from the script.
<ppadb.device.Device object at 0x02B30E50>
Am I missing anything (Might be in developer's option setting for android)?
I am on windows 10 machine with adb version 1.0.41. And using an android device with Android 10 (Android Q) over a usb.
Look for a "USB debugging (Security Settings)" or something similar in your developer options in the debugging section -- This is where you grant ADB special permissions and allow input.
Best of luck; I hope you find it.
I tried this to tap a xy coordinate using adb command. Enabling the "USB debugging (Security Settings)" is worked for me. But the phone should connect to the internet inorder to enable the settings.
Thanks #Meer

Automate android using python

I am trying to automate android mobile using python's pure-python-adb package but when I run the following code:
from ppadb.client import Client as AdbClient
# Default is "127.0.0.1" and 5037
client = AdbClient(host="127.0.0.1", port=5037)
print(client.version())
I get error in Pycharm as:
RuntimeError: ERROR: connecting to 127.0.0.1:5037 [WinError 10061] No connection could be made because the target machine actively refused it.
Is adb running on your computer?
Please can anyone help me with it
Switch on developer mode in your phone and connect it to your laptop using a physical data transfer (or changing) cable.
REMEMBER: Switch on 'USB debugging' on your phone.
Then go to cmd terminal and make sure you have adb requirements from android studio installed. Once that's there, go to terminal and type.
adb devices
if your phone shows up, you should be connected. You can then type
adb shell
or:
adb connect tcpip 5555
to connect over the internet

USB Device/PyUSB on Windows and LInux behaving differently

I have a device with USB interface which I can connect to both my Ubuntu 18.04 machine and my Windows 10 machine. On Windows 10 I have to install the CP210x driver and manually attach it to the device (otherwise Windows tries to find the device manufacturer's driver - it's a CP210x serial chip), and in Linux write the vendorID and productID to the cp210x driver to allow it to attach to ttyUSB0. This works fine.
The Windows driver is from SiliconLabs - the manufacturer of the UART-USB chip in the device.
So on Windows it is attached to COM5 and Linux to ttyUSB0 (Ubuntu, Raspbian)
Using Wireshark I can snoop the usb bus successfully on both operating systems.
The USB device sends data regularly over the USB bus and on Windows using Wireshark I can see this communication as "URB_INTERRUPT in" messages with the final few bytes actually containing the data I require.
On Linux it seems that the device connects but using Wireshark this time I can only see URB_BULK packets. Examining the endpoints using pyusb I see that there is no URB_Interrupt endpoint only the URB_Bulk.
Using the pyusb libraries on Linux it appears that the only endpoints available are URB_BULK.
Question mainly is how do I tell Linux to get the device to send via the Interrupt transfer mechanism as Windows seems to do. I don't see a method in pyusb's set_configuration to do this (as no Interrupt transfer endpoints appear) and haven't found anything in the manufacturer's specification.
Failing that, of course, I could snoop the configuration messages on Windows, but there has to be something I'm missing here?
Disregard this, the answer was simple in the end: Windows was reassigning the device address on the bus to a different device.

Bluetooth - listening to a pairing even in a Linux device in Python

I'm pretty new to Bluetooth so this might be trivial, but I'll still ask:
I would like to connect 2 devices via Bluetooth - a mobile device with a Linux device (like Raspberry Pi, but another one...).
Side 1 - the mobile: It has an app that should pair with the Linux device, and send some data to it (a msg with "Hello" in this point).
Side 2 - the linux device: It should have a kind of listener to the fact that a device was connected to it via bluetooth, and then expect the data, receive it, and process it.
Side 1 is all fine and clear to me.
As for side 2, for now I only use some command line commands to turn Bluetooth on, set some name to the device, and wait for scan. I do it with "hciconfig", by running the following commands in Python script, one after the other:
hciconfig hci0 up
hciconfig hci0 name MyDevice
hciconfig hci0 sspmode 1
hciconfig hci0 piscan
At this point, my device is discover-able to my mobile and it pairs with it successfully. Now, I'm stuck with the listening part. I would like the linux device to run a certain function (prefer in Python) when the device is paired, and expect to receive data from it. I've read some links over the net, using RFCOMM and Bluez, but none succeeded...
Can someone please assist?
Thanks
Good morning, there is a library written in Python that handle Bluetooth connection for you already PyBluez
to install use sudo pip install pybluez
here is an example on how to use sockets to communicate with bluetooth devices
import bluetooth
bd_addr = "01:23:45:67:89:AB"
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("hello!!")
sock.close()
the complete guide is at Bluetooth Programming with PyBluez
`

Categories