Reputation: 1034
in my application we use device imei
and return some result base on that. i cant find any solution to validate phone imei, user can be use some emulator such as Genymotion, my app must be detect that and if user is using emulators i must be don't post data to server, how can i validate imei?
this solution on PHP
work fine, but i can't find equivalent like with that on java for android
function is_luhn($n) {
$str = '';
foreach (str_split(strrev((string) $n)) as $i => $d) {
$str .= $i %2 !== 0 ? $d * 2 : $d;
}
return array_sum(str_split($str)) % 10 === 0;
}
function is_imei($n){
return is_luhn($n) && strlen($n) == 15;
}
Upvotes: 3
Views: 2368
Reputation: 4032
Try This:
public boolean isValidImei(String s){
long n = Long.parseLong(s);
int l = s.length();
if(l!=15) // If length is not 15 then IMEI is Invalid
return false;
else
{
int d = 0, sum = 0;
for(int i=15; i>=1; i--)
{
d = (int)(n%10);
if(i%2 == 0)
{
d = 2*d; // Doubling every alternate digit
}
sum = sum + sumDig(d); // Finding sum of the digits
n = n/10;
}
System.out.println("Output : Sum = "+sum);
if(sum%10==0 && sum!=0)
return true;
else
return false;
}
}
sumDig Function
int sumDig(int n) // Function for finding and returning sum of digits of a number
{
int a = 0;
while(n>0)
{
a = a + n%10;
n = n/10;
}
return a;
}
Upvotes: 6