0

I have some problems with a check if user has turned on bluetooth, and gps.

If the GPS is turned off the "OK" icon allways display status as the gps is activated.

If the bluetooth is turned on the "OK" icon will show. But not the "notOK" icon when it's turned off. Can someone please check the code.

Thanks in advance!

JAVA class:

    LocationManager locationManager;
Button buttongps;
Button buttonblue;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_konkurranse);

    buttongps = (Button) findViewById(R.id.gps);
    buttongps.setOnClickListener(onButtongpsClick);

    buttonblue = (Button) findViewById(R.id.bluetooth);
    buttonblue.setOnClickListener(onButtonblueClick);


    );


    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        ImageView img = (ImageView) findViewById(R.id.bluestatus);
        img.setImageResource(R.drawable.notok);

    } else {
        if (mBluetoothAdapter.isEnabled()) {
            ImageView img = (ImageView) findViewById(R.id.bluestatus);
            img.setImageResource(R.drawable.ok);
        }
    }


    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider != null){
        ImageView img = (ImageView) findViewById(R.id.gpsstatus);
        img.setImageResource(R.drawable.ok);
    }else{
        ImageView img = (ImageView) findViewById(R.id.gpsstatus);
        img.setImageResource(R.drawable.notok);
    }

}

public View.OnClickListener onButtongpsClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(gpsOptionsIntent);
    }
};


public View.OnClickListener onButtonblueClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) mBluetoothAdapter.enable();
        ImageView img = (ImageView) findViewById(R.id.bluestatus);
        img.setImageResource(R.drawable.ok);
    }
    };
 // More code below not related, and then removed..

XML code.

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="0dp"
    android:orientation="vertical"
    android:weightSum="1"
    android:gravity="top|bottom|center_vertical|fill_vertical|center_horizontal">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="50dp"
        android:layout_width="wrap_content"
        android:background="@drawable/actionbar"
        android:minHeight="?attr/actionBarSize"
        android:layout_gravity="center">
    </android.support.v7.widget.Toolbar>


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="0.1dp"
        android:id="@+id/imageView"
        android:src="@drawable/nttop"
        android:layout_marginRight="0.94dp"
        android:layout_marginLeft="0.06dp"
        android:layout_marginBottom="0.05dp"
        android:contentDescription="fblogin"
        android:scaleType="fitXY" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="SOME test here woop woop"
        android:id="@+id/intro"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="0.1dp"
        android:layout_marginLeft="0.06dp"
        android:layout_marginRight="0.94dp"
        android:layout_marginBottom="20dp"
        android:background="#ffffffff"
        android:textSize="20dp"
        android:layout_weight="0" />

<LinearLayout
    android:layout_width="307dp"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tel"
        android:background="@android:color/transparent"
        android:src="@drawable/tel"
        android:contentDescription="tel"
        android:layout_gravity="left|center" />


    <EditText
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/mobilnummer"
        android:hint="Mobilnummer"
        android:textColor="#000000"

        android:background="#ffffffff"
        android:autoText="false"
        android:phoneNumber="false"
        android:layout_weight="0.80"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center_horizontal" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tlfstatus"
        android:layout_gravity="right"
        android:contentDescription="notok" />
</LinearLayout>


    <LinearLayout
        android:layout_width="307dp"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/gpsicon"
            android:src="@drawable/gps"
            android:background="@android:color/transparent"
            android:contentDescription="gps"
            android:layout_gravity="left|center" />

        <Button
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Click here for GPS"
            android:id="@+id/gps"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.76"
            android:layout_marginLeft="20dp" />


        <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gpsstatus"
        android:layout_gravity="right"
        android:background="@android:color/transparent"
        android:contentDescription="notok" />
        </LinearLayout>


<LinearLayout
    android:layout_width="307dp"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blueicon"
        android:src="@drawable/bluetooth"
        android:background="@android:color/transparent"
        android:contentDescription="blue"
        android:layout_gravity="left|center" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Bluetooth"
        android:id="@+id/bluetooth"
        android:layout_weight="0.76"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center_horizontal" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bluestatus"
        android:background="@android:color/transparent"
        android:layout_gravity="right"
        android:contentDescription="notok" />
</LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/info"
        android:layout_gravity="bottom|center_horizontal"
        android:text="Woop thank you text /fix later " />


</LinearLayout>
<ListView
    android:id="@+id/drawer"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@drawable/menu"
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false">
</ListView>
</android.support.v4.widget.DrawerLayout>
datasmurfen
  • 324
  • 8
  • 25

3 Answers3

2

I think that the checks are wrong.

Try the following check for the Bluetooth

final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final ImageView btImg = (ImageView) findViewById(R.id.bluestatus);

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    btImg.setImageResource(R.drawable.notok);
} else {
    btImg.setImageResource(R.drawable.ok);
}

And also try the following for the GPS

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
final ImageView gpsImg = (ImageView) findViewById(R.id.gpsstatus);
if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
    gpsImg.setImageResource(R.drawable.ok);
} else {
    gpsImg.setImageResource(R.drawable.notok);
}
  • Thanks that works. Is there a way to listen for changes. I now have to go to another activity to get it to work. – datasmurfen Jun 08 '15 at 15:27
  • Yes, it is possible - could you check this post for the GPS - https://stackoverflow.com/questions/8146511/android-listen-for-change-in-gps-status and this one for the Bluetooth https://stackoverflow.com/questions/9693755/detecting-state-changes-made-to-the-bluetoothadapter – Yanislav Mihaylov Jun 08 '15 at 21:01
0

The problem is your app is only setting the disabled resource when getDefaultAdapter() is null (which is probably not the case here), so your code never reaches the part where you set the image. Try this instead:

ImageView img = (ImageView) findViewById(R.id.bluestatus);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) img.setImageResource(R.drawable.notok);
else img.setImageResource(mBluetoothAdapter.isEnabled() ? R.drawable.ok : R.drawable.notok);

Now it will set the disabled button in the event the adapter is null or it is not enabled.

Cruceo
  • 6,763
  • 2
  • 31
  • 52
0
    // BLUETOOTH
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            ImageView img = (ImageView) findViewById(R.id.bluestatus);
            img.setImageResource(R.drawable.notok);

        } else {
            if (mBluetoothAdapter.isEnabled()) {
                ImageView img = (ImageView) findViewById(R.id.bluestatus);
                img.setImageResource(R.drawable.ok);
            }
            else{
                ImageView img = (ImageView) findViewById(R.id.bluestatus);
                img.setImageResource(R.drawable.notok);
            }
        }

// GPS
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) { 
    ImageView img = (ImageView) findViewById(R.id.gpsstatus);
    img.setImageResource(R.drawable.notok);
} else {
   ImageView img = (ImageView) findViewById(R.id.gpsstatus);
   img.setImageResource(R.drawable.ok);
}
Carnal
  • 21,744
  • 6
  • 60
  • 75