-1

I have a basic Login Screen that the tablet user will log in to, I want to display that particular device's MAC address onscreen when the app starts.

Later as I learn more about Android development, and I'm learning slowly, I want to insert the users name, the time they logged in, and the MAC address to a database via a RESTful webservice. But for now I'm just trying to display the MAC address.

I found some code for obtaining the MAC address from the WIFI Manager as you'll see in my code below and put it in a method. It seems when I call this method in the onCreate method the app bombs. What is the correct work flow? Should I call showMACAddress() from within the onCreate() method? Should it be in a try / catch??

BTW i'm testing this on my 7 inch Kenton tablet with Android 4.0 as I work, and I have specified in the Android Manifest file that the app is Landscape orientation only. Thank you in advance.

here is my java class

package za.co.crcode.Inspector;

import android.net.wifi.WifiInfo;  
import android.net.wifi.WifiManager;  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Context;  
import android.view.Menu;  
import android.widget.TextView;  

public class DeviceLogin extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_login);
        showMACAddress();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_device_login, menu);
        return true;        
    }

    public void showMACAddress(){
        WifiManager wifiMan = (WifiManager) this.getSystemService(
            Context.WIFI_SERVICE);
        WifiInfo wifiInf = wifiMan.getConnectionInfo();
        String macAddr = wifiInf.getMacAddress();

        TextView loginDeviceMACAddress = (TextView)findViewById(R.id.LoginDeviceMACAddress);
        CharSequence macAddy = (String) macAddr.toString();     

        loginDeviceMACAddress.setText(macAddy);
    }

}

here is my Android GUI XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceLogin" >

    <LinearLayout
        android:id="@+id/LoginFieldsLayoutLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="50dp"
        android:gravity="left"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/LoginScreenHeading"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            android:text="@string/loginScreenHeading"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/LoginFirstName"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/loginFirstNameHint"
            android:inputType="textPersonName"
            android:text="@string/loginFirstName" />

        <EditText
            android:id="@+id/LoginLastName"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/loginLastNameHint"
            android:inputType="textPersonName"
            android:text="@string/loginLastName" />

        <EditText
            android:id="@+id/LoginEmployeeID"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/loginEmployeeIDHint"
            android:inputType="number"
            android:text="@string/loginEmployeeID" />

        <EditText
            android:id="@+id/loginPassword"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/loginPasswordHint"
            android:inputType="textPassword" >


        </EditText>

        <TextView
            android:id="@+id/LoginMACAddressLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:text="@string/LoginMACAddressLabel"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <TextView
            android:id="@+id/LoginDeviceMACAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:text="@string/LoginMACAddress"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LoginFieldsLayoutRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"        
        android:layout_marginTop="75dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/LoginIdPic"
            android:contentDescription="@string/EmployeeImage"          
            android:layout_width ="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxWidth="200dp"
            android:maxHeight="200dp"
            android:scaleType="fitCenter"           
            android:src="@drawable/idpic" >         
        </ImageView>

        <Button
            android:id="@+id/LoginScreenButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loginButtonText"
            android:layout_gravity="center" />                      

    </LinearLayout>  

</RelativeLayout>

here is my Strings.XML

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Inspector</string>
    <string name="hello_world">Hello World!</string>
    <string name="menu_settings">Settings</string>
    <string name="loginScreenHeading">Device Login</string>    
    <string name="loginFirstName">First Name</string>
    <string name="loginFirstNameHint">Enter your first name</string>
    <string name="loginLastName">Last Name</string>
    <string name="loginLastNameHint">Enter your last name</string>
    <string name="loginEmployeeID">Employee ID</string>
    <string name="loginEmployeeIDHint">Enter your employee ID</string>
    <string name="loginPasswordHint">Password</string>
    <string name="LoginMACAddressLabel">Device ID</string>
    <string name="LoginMACAddress">00:00:00:00</string>
    <string name="loginButtonText">Login</string>
    <string name="EmployeeImage">Employee_image</string>
</resources>
Solone
  • 11
  • 3

1 Answers1

2

The setText() should work and you don't have to cast macAddr from String to CharSequence(CharSequence is an Interface and String implements it). I believe why your app crashes is because you *don't add the permission * to the AndroidManifest.xml file.

Put <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> in the AndroidManifest.xml and your problem should be solved.

Huang
  • 4,812
  • 3
  • 21
  • 20