Find mobile device identifier (UDID or Android id) - android

I want to find out the list of mobile devices which are all click my ad. Based on the details I want to re-target them
So I need to capture their identifier(UDID in ios and Android id in android phone) in order to identify them.
Can anyone suggest me good method or prefer any better way to achieve the above.

For Android, you should check this thread :
Will TelephonyManger.getDeviceId() return device id for Tablets like Galaxy Tab...?
Jorgesys' answer seems clean.

ANDROID:
I am using this and seems to be working fine.
public static String getDeviceAndroidID(Context context)
{
String android_id = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
if(android_id != null)
return android_id;
else
return "";
}
iOS:
For iOS version less than 7 I am using MAC address of the device. From iOS version 7 apple is providing a unique ID for this purpose.
(NSString *)getMacAddress
{
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0"))
{
NSString *strUID = nil;
if(strUID == nil) strUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
return strUID;
}
int mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = nil;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = #"if_nametoindex failure";
else
{
// Get the size of the data available (store in len)
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = #"sysctl mgmtInfoBase failure";
else
{
// Alloc memory based on above call
if ((msgBuffer = malloc(length)) == NULL)
errorFlag = #"buffer allocation failure";
else
{
// Get system information, store in buffer
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
errorFlag = #"sysctl msgBuffer failure";
}
}
}
// Befor going any further...
if (errorFlag != nil)
{
free(msgBuffer);
if(ENABLE_LOG) DLog(#"Error: %#", errorFlag);
return errorFlag;
}
// Map msgbuffer to interface message structure
interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]];
//if(ENABLE_LOG) DLog(#"Mac Address: %#", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}

Related

How to connect android application to local domun

I am using esp32 to bring up a tcp server. In order not to access this server by ip, I use mDNS on the esp32 side. I can successfully contact the tcp server from the PC at esp32.local. Now I want to do the same from android application. I wrote a little c ++ code which I run on android.
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
hello ="socket creation failed...";
return env->NewStringUTF(hello.c_str());
}
else
hello="Socket successfully created..\n";
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
std::string url = "esp32.local";
servaddr.sin_addr.s_addr = inet_addr(url.c_str());
servaddr.sin_port = htons(1234);
struct hostent *result;
result = gethostbyname(url.c_str());
// destAddr.sin_addr.s_addr = ((in_addr*)hosten->h_addr_list[0])->s_addr;
if (!result)
{
hello ="gethostbyname...";
return env->NewStringUTF(hello.c_str());
}
// puts(result->h_name);
memmove(&(servaddr.sin_addr.s_addr), result->h_addr, result->h_length);
// connect the client socket to server socket
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
hello="connection with the server failed..";
return env->NewStringUTF(hello.c_str());
}
else
printf("connected to the server..\n");
// function for chat
char buff = 0xA;
write(sockfd, &buff, sizeof(buff));
// close the socket
close(sockfd);
When trying to connect from android application, I get an error in function gethostbyname. . What do I need to do to access the .local domain?
std::string url = "esp32.local";
servaddr.sin_addr.s_addr = inet_addr(url.c_str());
inet_addr() does not support parsing hostnames like "sp32.local", only IP addresses. You need to use gethostbyname() or better getaddrinfo() instead.
struct hostent *result;
result = gethostbyname(url.c_str());
...
memmove(&(servaddr.sin_addr.s_addr), result->h_addr, result->h_length);
You are not copying the result of gethostbyname() into the sin_addr.s_addr correctly. Your commented-out code was doing it correctly:
servaddr.sin_addr.s_addr = ((in_addr*)(hosten->h_addr_list[0]))->s_addr;
Which can be simplified to:
servaddr.sin_addr = *(in_addr*)(hosten->h_addr_list[0]);
Or just:
servaddr.sin_addr = *(in_addr*)(hosten->h_addr);
But, getaddrinfo() would be easier to use than populating a sockaddr_in manually. And it supports IPv6, whereas gethostbyname() only supports IPv4:
std::string url = "esp32.local";
int sockfd = -1;
addrinfo hints = {};
addrinfo *result;
hints.ai_family = AF_INET; // AF_UNSPEC
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// assign IP, PORT
int res = getaddrinfo(url.c_str(), "1234", &hints, &result);
if (res != 0) {
return env->NewStringUTF("getaddrinfo failed...");
}
for(addrinfo *addr = result; addr; addr = addr->ai_next) {
// socket create and varification
sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sockfd == -1) {
continue;
}
// connect the client socket to server socket
if (connect(sockfd, addr->ai_addr, addr->ai_addrlen) != 0) {
close(sockfd);
sockfd = -1;
continue;
}
break;
}
if (sockfd == -1) {
freeaddrinfo(result);
return env->NewStringUTF("socket creation/connect failed...");
}
freeaddrinfo(result);
printf("Connected to the server..\n");
// function for chat
char buff = 0xA;
write(sockfd, &buff, sizeof(buff));
// close the socket
close(sockfd);

hid_get_feature_report analog in Android USB Library

What would be analog to hid_get_feature_report in Android USB Library?
I need to get relays state from usb relays device on Android.
I found example on C (for Linux/Windows): https://github.com/darrylb123/usbrelay/blob/master/libusbrelay.c
static int get_board_features(relay_board *board, hid_device *handle)
{
unsigned char buf[9];
//Get the features of the device
buf[0] = 0x01;
int ret = hid_get_feature_report(handle, buf, sizeof(buf));
if (ret == -1)
{
perror("hid_get_feature_report\n");
}
//Set the serial number (0x0 for termination)
memset(board->serial, 0x0, sizeof(board->serial));
memcpy(board->serial, buf, Serial_Length);
//Byte 7 in the response contains the target_state of the relays
board->state = buf[7];
return ret;
}
On Android it returns only one end point Log.i(TAG, "endpointCount ${usbInterface.endpointCount}

use char as an operator?

I am making an android app + arduino that will receive ir code from arduino and sending the results.value (ir decode) to android through bluetooth. on the android side I have receive the code as a String that results into (for example) 92c0 then made a test button that will send it back to arduino and trigger it to send ir code to a device by irsend.sendNEC(0x92c0, 32) problem is when receiving the codes back from the android app is I have to receive it by char data, how do I use data which is a char and use it as a substitute for 0x92c0 in irsend.sendN My sketch down below:
#include <IRremote.h>
#include <SoftwareSerial.h>
SoftwareSerial bluetoothPort(4,5);
const int RECV_PIN = 12;
char data = "0";
const int SEND_PIN = 13;
IRsend irsend;
IRrecv irrecv(RECV_PIN);
decode_results results;
int BTval;
int IRval;
void setup()
{
bluetoothPort.begin(9600);
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if(bluetoothPort.available() > 0)
{
data = bluetoothPort.read();
Serial.print(data);
irsend.sendNEC(operator[data],32);
irrecv.resume();
}
if(irrecv.decode(&results))
{
Serial.println(results.value);
int set = results.value;
bluetoothPort.println(results.value, HEX);
irrecv.resume();
}
}
The problem here is that you CAN'T transfer 0x92c0 as a char (or byte). Just because it is not a byte, but two.
Now, I'm not much into android, so I need to see the android code to make a real solution You can handle this in three ways:
Binary transfer the data (2 bytes)
Transfer the data as a string (5 bytes)
Index the possible replies in an array and transfer the index (only applicable for a small quantity of codes).
I'm showing you the last 2, because the 1st is the most effective but I don't think you have the proper knowledge to send and receive data in binary format from android (and surely I don't have it).
So, if the data is transferred in string format (the same way you are uploading it) you will receive more bytes:
1 to 4 hexadecimal digits
1 carriage return
The code just stores the bytes you receive in a variable and then sends it when you receive a CR or LF:
// Outside the loop function
uint16_t receivedData;
// Inside the loop function
if(bluetoothPort.available() > 0)
{
data = bluetoothPort.read();
if ((data >= '0') && (data <= '9'))
{ // If it is a digit between 0 and 9 (in ascii)
receivedData = (receivedData << 4) | (data - '0');
}
else if ((data >= 'A') && (data <= 'F'))
{ // If it is a digit between A and F (in ascii)
receivedData = (receivedData << 4) | (data - 'A' + 10);
}
else if ((data >= 'a') && (data <= 'f'))
{ // Lowercase case
receivedData = (receivedData << 4) | (data - 'a' + 10);
}
else if (((data == '\r') || (data == '\n')) && (receivedData > 0))
{ // I tend to consider both CR and LF, because windows always screws this
Serial.print(receivedData, HEX);
irsend.sendNEC(receivedData,32); // Not sure about the 32 here...
irrecv.resume();
receivedData = 0;
}
else
receivedData = 0; // Something went wrong, just reset the variable
}
If you just have to send a few codes, you can store them and then transfer only the proper index. For instance:
// Outside the loop function
uint16_t possibleCodes[] = { 0x92c0, 0x8238, 0x5555 };
// Inside the loop function
if(bluetoothPort.available() > 0)
{
data = bluetoothPort.read();
// If you are using string transmission, use the following
// line to get the correct value
// data = bluetoothPort.read() - '0';
if (data < sizeof(possibleCodes) / sizeof(possibleCodes[0]))
{
Serial.print(possibleCodes[data], HEX);
irsend.sendNEC(possibleCodes[data],32); // Not sure about the 32 here...
irrecv.resume();
}
}
For instance in this case to send 0x92c0 you will have to send from android the value 0.

Arduino - Converting byte array to string/char array

I'm currently working on communication between devices using HID over a usb cable. I am sending a string that is UTF-8 encoded from an Android device, and would like to receive and read it on my Arduino Leonardo.
My problem is that I am unable to get the received message into any other type. I need to do a human readable string comparison as I'm sending a variety of commands to the Arduino. The IDE either has a type mismatch problem regardless of how I try to convert the received message. I've tried many different things but I will post one as an example. I'm sure there is something I missing that's keeping me from getting this!
int n;
n = RawHID.recv(buffer, 0); // 0 timeout = do not wait
if (msUntilNextSend > 2000) {
msUntilNextSend = msUntilNextSend - 2000;
// String mystr = "";
// byte charbuff[10];
//
// for (int i = 0; i < 64; i++)
// {
// mystr.concat((char) buffer[i]);
// }
//
// mystr.toCharArray(charbuff, 10);
char readin[64] = { ' ' };
readin = (char *)buffer;
String myString = String((char *)buffer);
if (strcmp(readin, "test") == 0)
{
String resp = "response";
resp.getBytes(buffer, 64);
n = RawHID.send(buffer, 100);
}
I've included some comments with bits of a different approach but as I mentioned, I have been unsuccessful in my attempts. Any insight is appreciated!

How to convert openssl_pkey_get_public and openssl_verify to C

Now I'm looking for a way to verify the signature from 'Google inapp billing' system.
I've found 'openssl_pkey_get_public' and 'openssl_verify' functions in php(it is very easy and simple!!), but no example or document for C or C++;;;(I spend last two days for searching it..OTL...)
now I have :
- public key
- signature
- purchase data from google
I want to implement verifying code using C or C++
Is there someone who knows how I can get it?
I've searched belows..
- http://www.nlnetlabs.nl/downloads/publications/hsm/hsm_node21.html
It deals with 'openssl EVP'..but it tells about HSM(hardware security module)
thanks!
below is the answer about what I've asked..
1 means success, 0 is fail..
thanks..
int Verify_GoogleInappBilling_Signature(const char* data, const char* signature, const char* pub_key_id)
{
std::shared_ptr<EVP_MD_CTX> mdctx = std::shared_ptr<EVP_MD_CTX>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
const EVP_MD* md = EVP_get_digestbyname("SHA1");
if(NULL == md)
{
return -1;
}
if(0 == EVP_VerifyInit_ex(mdctx.get(), md, NULL))
{
return -1;
}
if(0 == EVP_VerifyUpdate(mdctx.get(), (void*)data, strlen(data)))
{
return -1;
}
std::shared_ptr<BIO> b64 = std::shared_ptr<BIO>(BIO_new(BIO_f_base64()), BIO_free);
BIO_set_flags(b64.get(),BIO_FLAGS_BASE64_NO_NL);
std::shared_ptr<BIO> bPubKey = std::shared_ptr<BIO>(BIO_new(BIO_s_mem()), BIO_free);
BIO_puts(bPubKey.get(),pub_key_id);
BIO_push(b64.get(), bPubKey.get());
std::shared_ptr<EVP_PKEY> pubkey = std::shared_ptr<EVP_PKEY>(d2i_PUBKEY_bio(b64.get(), NULL), EVP_PKEY_free);
std::string decoded_signature = Base64Decode(std::string(signature));
return EVP_VerifyFinal(mdctx.get(), (unsigned char*)decoded_signature.c_str(), decoded_signature.length(), pubkey.get());
}

Categories

Resources