简体   繁体   English

从javascript获取(移动)设备名称

[英]Getting (mobile) device name from javascript

Is there a way to get the name of a mobile device (eg "John's iPhone" ) using javascript?有没有办法使用 javascript 获取移动设备的名称(例如“John's iPhone”)?


Maybe I wasn't very clear... what I meant is not whether it's an iPhone, iPad etc. but the "device name" - for example it can be "John's iPhone".也许我不是很清楚……我的意思不是它是 iPhone、iPad 等,而是“设备名称”——例如它可以是“约翰的 iPhone”。

You can't do this through javascript for a web app running in a native browser - javascript generally doesn't have access to this personal identifying data.对于在本机浏览器中运行的 Web 应用程序,您无法通过 javascript 执行此操作 - javascript 通常无权访问此个人识别数据。

One possible way is to use a framework like PhoneGap which may have an API to access the device name.一种可能的方法是使用像PhoneGap这样的框架,它可能有一个 API 来访问设备名称。 But then, you can only deploy your web site via an app store, so this could be very limiting based on your use case.但是,您只能通过应用商店部署您的网站,因此根据您的用例,这可能会非常有限。

Your best bet is to use the user agent:最好的办法是使用用户代理:

eg例如

const ua = navigator.userAgent
const device = {
  iPad: /iPad/.test(ua),
  iPhone: /iPhone/.test(ua),
  Android4: /Android 4/.test(ua)
}

The object will allow you to write nice conditional logic such as if(device.iPad) { /* do stuff */ }该对象将允许您编写漂亮的条件逻辑,例如if(device.iPad) { /* do stuff */ }

I'm working with mobile device with embedded scanner.我正在使用带有嵌入式扫描仪的移动设备。 To be able to use some the JavaScript library of different devices and to avoid conflict with those libraries of different manufacturer (Zebra, Honeywell, Datalogic, iOs ect...) I need to come up with a way to identify each devices so I can load the proper library and this is what I came up with.为了能够使用不同设备的一些 JavaScript 库并避免与不同制造商(Zebra、Honeywell、Datalogic、iOs 等)的库发生冲突,我需要想出一种方法来识别每个设备,以便我可以加载正确的库,这就是我想出的。 Enjoy享受

getDeviceName: function () {
    var deviceName = '';

    var isMobile = {
        Android: function() {
            return navigator.userAgent.match(/Android/i);
        },
        Datalogic: function() {
            return navigator.userAgent.match(/DL-AXIS/i);
        },
        Bluebird: function() {
            return navigator.userAgent.match(/EF500/i);
        },
        Honeywell: function() {
            return navigator.userAgent.match(/CT50/i);
        },
        Zebra: function() {
            return navigator.userAgent.match(/TC70|TC55/i);
        },
        BlackBerry: function() {
            return navigator.userAgent.match(/BlackBerry/i);
        },
        iOS: function() {
            return navigator.userAgent.match(/iPhone|iPad|iPod/i);
        },
        Windows: function() {
            return navigator.userAgent.match(/IEMobile/i);
        },
        any: function() {
            return (isMobile.Datalogic() || isMobile.Bluebird() || isMobile.Honeywell() || isMobile.Zebra() || isMobile.BlackBerry() || isMobile.Android() || isMobile.iOS() || isMobile.Windows());
        }
    };

    if (isMobile.Datalogic())
        deviceName = 'Datalogic';
    else if (isMobile.Bluebird())
        deviceName = 'Bluebird';
    else if (isMobile.Honeywell())
        deviceName = 'Honeywell';
    else if (isMobile.Zebra())
        deviceName = 'Zebra';
    else if (isMobile.BlackBerry())
        deviceName = 'BlackBerry';
    else if (isMobile.iOS())
        deviceName = 'iOS';
    else if ((deviceName == '') && (isMobile.Android()))
        deviceName = 'Android';
    else if ((deviceName == '') && (isMobile.Windows()))
        deviceName = 'Windows';

    if (deviceName != '') {
        consoleLog('Devices information deviceName = ' + deviceName);
        consoleLog('Devices information any = ' + isMobile.any());
        consoleLog('navigator.userAgent = ' + navigator.userAgent);
    }

    return deviceName;
},

and this is an example of how it can be used:这是如何使用它的示例:

initializeHandheldScanners: function () {
    if (DeviceCtrl.getDeviceName() == 'Zebra')
        DeviceCtrl.initializeSymbolScanner();

    if (DeviceCtrl.getDeviceName() == 'Honeywell')
        DeviceCtrl.initializeHoneywellScanner();

    if (DeviceCtrl.getDeviceName() == 'Datalogic')
        DeviceCtrl.initializeDatalogicScanner();
},

You can say thanks to Cory LaViska.您可以感谢 Cory LaViska。 I did this based on his work.我是根据他的工作做的。 Here is the link if you want to know more如果你想了解更多,这里是链接

https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript

You can use this snippet:您可以使用此代码段:

const getUA = () => {
    let device = "Unknown";
    const ua = {
        "Generic Linux": /Linux/i,
        "Android": /Android/i,
        "BlackBerry": /BlackBerry/i,
        "Bluebird": /EF500/i,
        "Chrome OS": /CrOS/i,
        "Datalogic": /DL-AXIS/i,
        "Honeywell": /CT50/i,
        "iPad": /iPad/i,
        "iPhone": /iPhone/i,
        "iPod": /iPod/i,
        "macOS": /Macintosh/i,
        "Windows": /IEMobile|Windows/i,
        "Zebra": /TC70|TC55/i,
    }
    Object.keys(ua).map(v => navigator.userAgent.match(ua[v]) && (device = v));
    return device;
}

console.log(getUA());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM