-1

I'm new to android studio and was wondering if anyone knew how to obtain the users Mac address and display it in a text view with the tag lbl_Mac?

This is my text view xml

    <TextView
    android:id="@+id/lbl_Mac"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="MAC Address: Not Found"
    android:textColor="#FFF"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.989"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.983" />

and i want to use this code to obtain the MAC address:

public String getMacAddress(Context context) {
WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String macAddress = wimanager.getConnectionInfo().getMacAddress();
if (macAddress == null) {
    macAddress = "Device don't have mac address or wi-fi is disabled";
}
return macAddress;
}

But I want to set the mac address to the text view.

Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
David Ngo
  • 1
  • 5

1 Answers1

0

Just needed to add the public string name into the setText brackets.

lbl_Mac.setText(***getMacAddress***(this));

For Example

    public String ***getMacAddress***(Context context) {
    WifiManager wimanager = (WifiManager) 
    context.getSystemService(Context.WIFI_SERVICE);
    String macAddress = wimanager.getConnectionInfo().getMacAddress();
    if (macAddress == null) {
        macAddress = "Device don't have mac address or wi-fi is disabled";
    }
    return macAddress;
David Ngo
  • 1
  • 5