Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two things I see wrong:</p> <ol> <li>You are calling the method in charge of updating the TextView (<code>setIp()</code>) only once, since <code>onCreate()</code> is only called once per Activity creation. Since turning the <a href="http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable%28%29" rel="nofollow">adapter on/off, is done outside the UI Thread</a>, you will have to make a <code>BroadcastReceiver</code> and listen for <code>BluetoothAdapter.ACTION_STATE_CHANGED</code> then update accordingly. </li> <li>You are comparing CharSequences with <code>==</code>. You should instead use Strings and then use <code>.equals()</code> since that will ensure a deep comparison. Only thing you need to change besised the type declarations are to add <code>.toString()</code> after <code>getText()</code>. For example <code>button.getText().toString()</code></li> </ol> <p>Edit: A simple (but messy) solution can be making the receiver dynamically and registering it. Now, keep in mind that you only have to call <code>setUp()</code> once, like you did before. Something like the below code might work. </p> <pre><code>public class MainActivity extends Activity { BluetoothAdapter btAdapter; Button button; TextView update; String enabled = "Bluetooth Enabled"; String disabled = "Bluetooth disabled"; String connect = "Connect"; String disconnect = "Disconnect"; BroadcastReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btAdapter = BluetoothAdapter.getDefaultAdapter(); setUp(); action(); } @Override public void onPause() { try{ unregisterReceiver (receiver); } catch (NullPointerException e) { e.printStackTrace(); } catch (IllegalStateException ie) { ie.printStackTrace(); } super.onPause(); } private void action() { button.setOnClickListener(new View.OnClickListener (){ @Override public void onClick(View v) { if (btAdapter.isEnabled()) btAdapter.disable(); else btAdapter.enable(); } }); } private void setButtonText () { if (btAdapter.isEnabled()) { update.setText("Bluetooth is enabled"); button.setText(disconnect); } else { update.setText("Bluetooth is disabled"); button.setText(connect); } } private void setUp() { button = (Button) findViewById(R.id.button1); update = (TextView) findViewById(R.id.textView1); setButtonText(); receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { setButtonText(); } }; IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver (receiver, filter); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload