How to Get Device ID

palmzac

Active Member
Licensed User
Longtime User
Hi,

I am newbie on Basic4Android. I don't understand the document of Phone Library. I want to get DeviceID. Would you give me a sample code ?

Thanks !

palmzac
 

susu

Well-Known Member
Licensed User
Longtime User
Try this:

Sub Globals
Dim PhoneId As PhoneId
End Sub

Sub Activity_Create(FirstTime As Boolean)
Msgbox(PhoneId.GetDeviceId, "Your device ID")
End Sub
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
or this one :

B4X:
Dim DeviceID1 As PhoneId
Dim DevID As String 
DevID = DeviceID1.GetDeviceId
If DevID = "" Then
   Dim Device As Phone
   DevID = Device.GetSettings("android_id")
End If
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
And... don't forget to to put a checkmark in the correct phone library in the Libs tab of the B4A editor. I can't tell you exactly which one, I'm not at my dev pc right now. If there is a lib called PhoneId, use it, otherwise use the standard Phone lib. You'll know you got the right one when you start typing 'As PhoneId' and PhoneId is in the popup list if available objects.

Cheers
 
Upvote 0

devjet

Member
Licensed User
Longtime User
I guess one has also to add the permission in the manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
I guess one has also to add the permission in the manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

No, B4A does that for you. The library's author is responsible for instructing B4A which permissions each of their objects use, and its all handled behind the scenes at compile time. Note that a library can have multiple objects, and unless I am mistaken, only the Dimmed objects will cause permissions to be reported.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…