How to detect a pc from android

57 views Asked by At

I im needing to detect a pc or/and get bytes from USB, i was testing and nor my own application in kotlin or play store apps can detect my pc, there is a way to make this work?

The connection i meant is PC:Host - Cellphone:Client

(PC sends data to an Android phone, and the phone retrieves it)

USB: Type-C 2.0 (not OTG)

Android code:

val DeviceMan = getSystemService(Context.USB_SERVICE) as UsbManager 
val Accesoryls = DeviceMan.accessoryList //Gives null 
val Devicels = DeviceMan.deviceList //Gives size == 0

The manifest XML already has the feature and permissions

The cellphone is in USB depuration mode

The PC detects the cellphone (But when sending raw data to the USB, the access is denied)

PC code (Minimal):

std::cout << "Opening device..." << std::endl;
CelHandle = libusb_open_device_with_vid_pid(contexto, CelularDesc.idVendor, CelularDesc.idProduct);
std::cout << "Detaching device..." << std::endl;
libusb_set_auto_detach_kernel_driver(CelHandle, 0);
std::cout << "Claiming device..." << std::endl;
libusb_claim_interface(CelHandle, 0);
int endsize;
std::cout << "Transfering device..." << std::endl;
std::vector<uchar> buf = CaptureScreen();

libusb_config_descriptor* Cdescriptor;
for(int i = 0; i < CelularDesc.bNumConfigurations; i++){
    libusb_get_config_descriptor(Celular, i,&Cdescriptor);
    if(Cdescriptor->bNumInterfaces == 1){
        break;
    }
}

int ENDPOINT;
std::cout << Cdescriptor->bNumInterfaces << std::endl;
for (int i = 0; i < Cdescriptor->bNumInterfaces; i++){
    libusb_interface interface = Cdescriptor->interface[i];
    for (int j = 0; j < interface.num_altsetting; j++){
        libusb_interface_descriptor LIdescriptor = interface.altsetting[j];
        for (int k = 0; k < LIdescriptor.bNumEndpoints; k++){
            libusb_endpoint_descriptor Edescriptor = LIdescriptor.endpoint[k];
            if (Edescriptor.bEndpointAddress == 0x81){
                std::cout << Edescriptor.bEndpointAddress << std::endl;
                ENDPOINT = Edescriptor.bEndpointAddress;
                std::cout << "Encontro endpoint address" << Edescriptor.bDescriptorType << std::endl;
                break;
            }
            else{
                ENDPOINT = Edescriptor.bEndpointAddress;
            }
        }

    }
}

for (int i = 0; i < buf.size(); i++){
    int r = libusb_bulk_transfer(CelHandle, ENDPOINT, &buf[i], 1, &endsize, 0);
    std::cout << int(buf[i]) << std::endl;
    if (r < 0){
        std::cout << "Transfer error " << r << std::endl;
        system("PAUSE");
    }
}
std::cout << "BYTES: " << endsize << std::endl;

Always is transfer error (if having timeout, the error is a timeout error, else, it gets stuck)

PC Operative system: Windows 11

Physical cellphone Operative system: Android 9

Deprecated function mentioned in a comment:

val accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY) as UsbAccessory

In the intent file:

@Deprecated
    public @Nullable <T extends Parcelable> T getParcelableExtra(String name) {
        return mExtras == null ? null : mExtras.<T>getParcelable(name);
    }

    /**
     * Retrieve extended data from the intent.
     *
     * @param name The name of the desired item.
     * @param clazz The type of the object expected.
     *
     * @return the value of an item previously added with putExtra(),
     * or null if no Parcelable value was found.
     *
     * @see #putExtra(String, Parcelable)
     */
    public @Nullable <T> T getParcelableExtra(@Nullable String name, @NonNull Class<T> clazz) {
        return mExtras == null ? null : mExtras.getParcelable(name, clazz);
    }

Also tried changing the minimum android version to the API 12, like one post said, but didn't work

0

There are 0 answers