50

I'm building an application in phonegap for the three different platforms: Android, iOS And Blackberry. While doing so, I have to detect the device type so that I can manage the height and width of my app on different platforms and for different devices like Android Phone, Android Tablet, iPhone, iPad and Blackberry...

Ahmad F
  • 26,570
  • 13
  • 76
  • 124
Preet_Android
  • 2,325
  • 5
  • 23
  • 43

9 Answers9

95

New in Phonegap, and simplest, is that you can use the device.platform:

// Depending on the device, a few examples are:
//   - "Android"
//   - "BlackBerry"
//   - "iOS"
//   - "webOS"
//   - "WinCE"
//   - "Tizen"
//   - "browser"
var devicePlatform = device.platform;
Nigel B. Peck
  • 5,804
  • 2
  • 16
  • 38
Sindre
  • 3,780
  • 2
  • 24
  • 39
  • 25
    If you are finding that "device" is undefined, then you are probably using a newer version of Phonegap/Cordova which means you need to install the plugin. This can be found here, https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git and installed by `$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git` or `cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git` – Ian Jamieson Dec 16 '13 at 11:48
  • 7
    Currently, just `cordova plugin add cordova-plugin-device` – Victor Sergienko Jan 27 '16 at 11:58
  • I'm getting "No provider for device!" error. Any ideas? – Marc-André Therrien Jul 05 '16 at 13:55
55

if you want to get device type before onDeviceReady, you can get like this.

var deviceType = (navigator.userAgent.match(/iPad/i))  == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i))  == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";

alert(deviceType);
JDev
  • 2,485
  • 1
  • 16
  • 21
  • Thanks a lot sir.. But problem is that it can't detect the Android tablet/ epad. so can you please help me how i can do this. It shows the epad type as the android. Thanks a lot in advance. – Preet_Android Jun 18 '12 at 07:57
  • you just have to log userAgent string for Android tablet/epad. use console.log(navigator.userAgent); and check what it return and put condition according to string. – JDev Jun 18 '12 at 08:15
  • navigator.userAgent.match(/epad/i)) == "epad" ? "epad" I've put this condition in the code that u provide me... But its not weorking... – Preet_Android Jun 18 '12 at 09:29
  • try to alert(navigator.userAgent) and show me what you got for android epad. – JDev Jun 18 '12 at 10:08
  • Mozila/5.0(Linux; U; Android 4.0.3; en-us; Transformerr TF101 Build/IML74k)AppleWebKit/534.30(KHTML, like Gecko)Version/4.0 Safari/534.30 This Value is shown in the alert. – Preet_Android Jun 18 '12 at 10:34
  • may be it would be like this "Mozilla/5.0 (Linux; U; Android 3.2.1; de-de; EPAD Build/HTK75) AppleWebKit/534.13 (KHTML like Gecko) Version/4.0 Safari/534.13" for android epad so you can use like navigator.userAgent.match(/epad/i)) == "EPAD" – JDev Jun 18 '12 at 10:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12687/discussion-between-preet-android-and-jdev) – Preet_Android Jun 18 '12 at 10:57
  • 1
    Why would you want to set `deviceType` to the string `"null"`? – briangonzalez Oct 16 '13 at 15:36
  • in my case i did perform some operation if deviceType is "null". that is why i have assign null as string. – JDev Oct 17 '13 at 09:25
  • Sindre's solution is a much cleaner one. `$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git` – Ian Jamieson Dec 16 '13 at 11:50
  • for android you can use `navigator.userAgent.toLowerCase().includes("android")` – João Pimentel Ferreira May 08 '18 at 17:54
36
cordova.platformId 
// "android"
RouR
  • 5,300
  • 3
  • 27
  • 23
  • Do you know a list of possible values? I want to apply a certain setting depending on de platform but i don't know if it will be "ios" or "iOS" or "iPhone" or "iSmugface". – Roel Apr 08 '16 at 13:30
  • 2
    @Roel This is just a guess, but since cordova.platformVersion refers to the cordova platform version not the device version, I think cordova.platformId is consistent with the folder name under platforms. So for ios, it would be "ios" – csga5000 Apr 18 '16 at 20:43
  • This is a more reliable solution than JDev's answer. – Design by Adrian May 30 '16 at 15:10
  • This is perfect. No need for a plugin. – Jake Oct 28 '16 at 21:35
  • this should be best answer here. first one is gross and makes me feel uncomfortable. – Zach Smith Sep 12 '18 at 20:05
5

you can use device.name property to get the detailed info about the device.

    function onDeviceReady() {
    var name=device.name ;
       }

or to get only the platform you can use device.platform property.

    function onDeviceReady() {
    var name=device.name ;
    var plat=device.platform;
       }
Ice Box
  • 1,514
  • 1
  • 14
  • 25
  • 1
    Thanks a lot... But here i want to device type. Either it is an android phone oo an android pad. And same in tha cas of iPhone. Means its an iPhone or iPad. because my application is for the three platform. 1.Android 2. Blackberry and 3. iPhone/iPad. – Preet_Android Jun 18 '12 at 06:56
3

use this code as ur html file

Device Properties Example

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {
    var element = document.getElementById('deviceProperties');

    element.innerHTML = 'Device Name: '     + device.name     + '<br />' + 
                        'Device PhoneGap: ' + device.phonegap + '<br />' + 
                        'Device Platform: ' + device.platform + '<br />' + 
                        'Device UUID: '     + device.uuid     + '<br />' + 
                        'Device Version: '  + device.version  + '<br />';
}

</script>

Loading device properties...

i suggest u to check this in device Best Of Luck Aamirkhan I.

Aamirkhan
  • 6,853
  • 11
  • 45
  • 75
1

You can have a look at the following link: http://docs.phonegap.com/en/1.0.0/phonegap_device_device.md.html

There are several methods to get device infos from phonegap.

An other option is to have a look at the browser type. (IE = WP7, Safari = iOS,...)

I'm not sure if you will be able to detect if you are at an Android phone or tablet... Anyway your html apps should be able to handle any screen resolution. You can have different resolutions at the phones also!

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Dominik Kirschenhofer
  • 1,185
  • 1
  • 12
  • 26
0
function onDeviceReady()
{
// ***IMPORTANT: access device object only AFTER "deviceready" event    
document.getElementById("name").innerHTML = device.name;
document.getElementById("pgversion").innerHTML = device.cordova ? device.cordov:device.phonegap;
document.getElementById("platform").innerHTML = device.platform;
document.getElementById("uuid").innerHTML = device.uuid;
document.getElementById("version").innerHTML = device.version;

}

See Here for device.platform

vikky
  • 4,746
  • 4
  • 40
  • 63
  • Thanks a lot... But here i want to device type. Either it is an android phone oo an android pad. And same in tha cas of iPhone. Means its an iPhone or iPad. because my application is for the three platform. 1.Android 2. Blackberry and 3. iPhone/iPad. – Preet_Android Jun 18 '12 at 06:52
  • device.platform return "Android" on android emulator.Actually I don't check on real device but I know this will give you fruitful result. – vikky Jun 18 '12 at 06:59
0

If you need this information because you're trying to format the screen correctly - you should be using CSS @media queries to determine the appropriate screen layout and apply CSS accordingly.

Use Google to find info on "responsive design with CSS"

mr.subtle
  • 65
  • 1
  • 7
-2

i would certainly NOT use the device type when laying out a page, the layout should be based on screen size only

you can use the javascript properties:

width = window.innerWidth
height = window.innerHeight

or use CSS:

.element{   
    height: 50vh;  //(height = 50 percent of screen)
}

see vmin,vmax, vh, vw

  • huh ? yes it does !, he wants to detect the device, then have a table with hard coded sizes for each model !, that is just wrong !, his real question is not how to detect the model, but how to detect the size of the screen, so my answer is correct, and anyone actually agreeing with the idea of using a device model to determine the size of the screen isn't. – Martijn Scheffer Apr 19 '17 at 23:46
  • The question is how to detect a device type. You didn't answer the question. – Pier Aug 18 '17 at 02:20
  • the question is how to detect the device type to INFER the screen size, that is just wrong !, read it again ! anyone downvoting this answer encourages someone to write code that will not scale well and will break. "I have to detect the device type so that I can manage the height and width of my app" ! isn't that clear ? – Martijn Scheffer Aug 21 '17 at 12:51
  • the question is NOT that he just wants to know the device type ! – Martijn Scheffer Aug 21 '17 at 12:54
  • yes i did, the question is to detect the device TO KNOW THE SIZE OF THE SCREEN !!, it's totaly wrong. "have to detect the device type so that I can manage the height and width of my app on different platforms" i should not be voted down, read his question again, what he wants to do is wrong. – Martijn Scheffer Nov 02 '17 at 22:16