代码之家  ›  专栏  ›  技术社区  ›  Akaanksha

使用NPM/Node检查Android手机上的USB调试

  •  0
  • Akaanksha  · 技术社区  · 7 年前

    我试图检查USB调试是否通过 。一旦安卓手机连接到我的系统并且USB调试关闭,那么我需要向用户显示一个提示,在他的手机上启用USB调试。

    唯一GUID 这有助于我区分连接了哪个设备。此外,我无法获取usb调试详细信息。

    到目前为止,我编写的代码是基于iSerialNumber的,但我想根据总线类型GUID来区分它。

    var usb = require('usb');
    usb.on('attach', function(device) {
    var devices = usb.getDeviceList();
    var check = devices[0].deviceDescriptor;
    if(check.iSerialNumber == '3')
    {
        console.log("Please enable USB Debugging");
    }
    else
    {
        console.log("Connect an Android device");
    }
    

    });

    I'm facing these issues.

    2 回复  |  直到 7 年前
        1
  •  1
  •   Kamil Mahmood    7 年前
    if(check.iSerialNumber == '3')
    

    iSeries编号 link . USB设备不使用,但如前所述,操作系统本身可能会使用 here here 因此,不建议使用序列号将设备标记为Android。我找到的最好的方法是检查设备供应商id和产品id,它们存储在设备描述符中,作为 ID供应商 分别地我找到了一个供应商ID列表,但没有找到每个供应商的任何产品ID列表。

    接口类255 接口子类66 .我找到了这些数字 here

    程序首先测试新连接的设备是否为Android,如果是Android,则检查USB调试。我不是普通的节点。所以我的代码不是很好。

    main.js

    var usb = require('usb');
    usb.on('attach', function(device) {
        getDeviceInformation(device, function onInfromation(error, information)
        {
            if (error)
            {
                console.log("Unable to get Device information");
                return;
            }
    
            if (isAndroidDevice(information))
            {
                if (!isDebuggingEnabled(device))
                {
                    console.log("Please enable USB Debugging from Developer Options in Phone Settings");
                    return;
                }
    
                //Do your thing here
                console.log("Device connected with Debugging enabled");
                console.log("Device Information");
                console.log(information);
                console.log();
            }
        });
    });
    
    function getDeviceInformation(device, callback)
    {
        var deviceDescriptor = device.deviceDescriptor;
        var productStringIndex = deviceDescriptor.iProduct;
        var manufacturerStringIndex = deviceDescriptor.iManufacturer;
        var serialNumberIndex = deviceDescriptor.iSerialNumber;
    
        var callbacks = 3;
        var resultError = false;
        var productString = null;
        var manufacturerString = null;
        var serialNumberString = null;
    
        device.open();
        device.getStringDescriptor(productStringIndex, function callback(error, data)
        {
            if (error)resultError = true;
            else productString = data;
    
            if (--callbacks == 0)onFinish();
        });
    
        device.getStringDescriptor(manufacturerStringIndex, function callback(error, data)
        {
            if (error)resultError = true;
            else manufacturerString = data;
    
            if (--callbacks == 0)onFinish();
        });
    
        device.getStringDescriptor(serialNumberIndex, function callback(error, data)
        {
            if (error)resultError = true;
            else serialNumberString = data;
    
            if (--callbacks == 0)onFinish();
        });
    
        function onFinish()
        {
            device.close();
    
            var result = null;
            if (!resultError)
            {
                result = {
                    idVendor: deviceDescriptor.idVendor,
                    idProduct: deviceDescriptor.idProduct,
                    Product: productString,
                    Manufacturer: manufacturerString,
                    Serial: serialNumberString
                };
            }
    
            callback(resultError, result);
        }
    }
    
    /**
     *Currently this procedure only check vendor id
     *from limited set of available vendor ids
     *for complete functionality it should also
     *check for Product Id, Product Name or Serial Number
     */
    function isAndroidDevice(information)
    {
        var vendorId = information.idVendor;
        var index = require('./usb_vendor_ids').builtInVendorIds.indexOf(vendorId);
        return index == -1? false : true;
    }
    
    /**
     *Currently this procedure only check the
     *interfaces of default activated configuration 
     *for complete functionality it should check 
     *all interfaces available on each configuration 
     */
    function isDebuggingEnabled(device)
    {
        const ADB_CLASS = 255;
        const ADB_SUBCLASS = 66;
        const ADB_PROTOCOL = 1;
    
        /*opened device is necessary to set new configuration and 
         *to get available interfaces on that configuration
         */
        device.open();
    
        var result = false;
        const interfaces = device.interfaces;
        for (var i = 0, len = interfaces.length; i < len; ++i)
        {
            const bClass = interfaces[i].descriptor.bInterfaceClass;
            const bSubClass = interfaces[i].descriptor.bInterfaceSubClass;
            const bProtocol = interfaces[i].descriptor.bInterfaceProtocol;
            if (bClass == ADB_CLASS && bSubClass == ADB_SUBCLASS && bProtocol == ADB_PROTOCOL)
            {
                result = true;
                break;
            }
        }
    
        device.close();
        return result;
    }
    

    usb\u vendor\u ids.js

    //copied from https://github.com/karfield/adb/blob/master/src/usb_vendor_ids.c
    module.exports.builtInVendorIds = [
        0x18d1, /* Google */
        0x0bb4, /* HTC */
        0x04e8, /* Samsung */
        0x22b8, /* Motorola */
        0x1004, /* LG */
        0x12D1, /* Huawei */
        0x0502, /* Acer */
        0x0FCE, /* Sony Ericsson */
        0x0489, /* Foxconn */
        0x413c, /* Dell */
        0x0955, /* Nvidia */
        0x091E, /* Garmin-Asus */
        0x04dd, /* Sharp */
        0x19D2, /* ZTE */
        0x0482, /* Kyocera */
        0x10A9, /* Pantech */
        0x05c6, /* Qualcomm */
        0x2257, /* On-The-Go-Video */
        0x0409, /* NEC */
        0x04DA, /* Panasonic Mobile Communication */
        0x0930, /* Toshiba */
        0x1F53, /* SK Telesys */
        0x2116, /* KT Tech */
        0x0b05, /* Asus */
        0x0471, /* Philips */
        0x0451, /* Texas Instruments */
        0x0F1C, /* Funai */
        0x0414, /* Gigabyte */
        0x2420, /* IRiver */
        0x1219, /* Compal */
        0x1BBB, /* T & A Mobile Phones */
        0x2006, /* LenovoMobile */
        0x17EF, /* Lenovo */
        0xE040, /* Vizio */
        0x24E3, /* K-Touch */
        0x1D4D, /* Pegatron */
        0x0E79, /* Archos */
        0x1662, /* Positivo */
        0x15eb, /* VIA-Telecom */
        0x04c5, /* Fujitsu */
        0x091e, /* GarminAsus */
        0x109b, /* Hisense */
        0x24e3, /* KTouch */
        0x17ef, /* Lenovo */
        0x2080, /* Nook */
        0x10a9, /* Pantech */
        0x1d4d, /* Pegatron */
        0x04da, /* PMCSierra */
        0x1f53, /* SKTelesys */
        0x054c, /* Sony */
        0x0fce, /* SonyEricsson */
        0x2340, /* Teleepoch */
        0x19d2, /* ZTE */
        0x201e, /* Haier */
        /* TODO: APPEND YOUR ID HERE! */
    ];
    
        2
  •  0
  •   Nikhil Chaudhary    7 年前

    var usbDetect = require('usb-detection');
    var Promise = require('bluebird');
    var adb = require('adbkit');
    var client = adb.createClient();
    
    usbDetect.on('add', function(dev) {
        setTimeout(checkAndyDevice, 1500);
     }); 
    
    var checkAndyDevice = function(){
     client.listDevices()
          .then(function(devices) {
            return Promise.map(devices, function() {
              return devices;
            })
          })
          .then(function(id) {
            if((JSON.stringify(id)) == '[]')
            {
                console.log("Please enable Usb Debugging option from your Android device");
            }
          })
          .catch(function(err) {
            console.error('Something went wrong:', err.stack)
          });
     };