This tutorial illustrates how to return a match for a Device Id. It shows how to first get the Device Id from a match, then get a match for that Device Id and return the value of the IsMobile property. This can be useful to look at devices that have already been matched at a previous date.
Code and Explanation
Example shows how to use an existing 51Degrees deviceId to obtain a Match
object with relevant properties and values.
Example illustrates:
Loading a Provider from a Disk-based (Stream) Pattern Dataset
provider = new Provider(StreamFactory.create
(Shared.getLitePatternV32(), false));
Matching a User-Agent string header value
provider.match(mobileUserAgent);
Using the match object to retrieve and store deviceId
Match matchFromUA;
as a string:
matchFromUA.getDeviceId();
as byte array:
matchFromUA.getDeviceIdAsByteArray();
as list of profile IDs:
for (Profile profile : matchFromUA.getProfiles()) {
Creating a match object from deviceId
device.matchForDeviceIdArray(deviceIdByteArray);
device.matchForDeviceIdString(deviceIdString);
device.matchForDeviceIdList(deviceIdList);
At the end a short console message will be printed containing a hash code of
the object, deviceId and the value for the IsMobile property. Notice that
all three objects are different as demonstrated by hash value but have the
same deviceId and value for the IsMobile property.
Storing deviceId as opposed to the individual properties is a much more
efficient way of retaining device information for future use. Byte array is
the most efficient of the three options demonstrated as it only requires
enough space to store the number of integers corresponding to the number of
profiles (one profile per component).
Summary
In this tutorial you have seen how to use the detector to retrieve the
IsMobile
property for a pre-defined Device ID string. The example can easily be modified to retrieve the value of any other property. Premium and Enterprise data files provide considerably more properties such as
IsCrawler
,
PriceBand
,
HardwareVendor
and
ScreenInchesWidth
. For a full list of properties and the data files they exist in please see the
Property Dictionary
.