Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Are you using a library, or is BTDiscoverable() a function you made?</p> <p>In Android, the discoverability is set using an intent </p> <pre><code>Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); </code></pre> <p>(see <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability" rel="nofollow noreferrer">http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability</a>)</p> <p>So if I understand you correctly, what you could do is: launch this intent (or use your BTDiscoverable if it is equivalent), then return. The button remains enabled. If the user accepted the discoverability, next time he/she will push the button, the code will execute the else loop and start the server, otherwise it will ask the permission again</p> <pre><code>public void onClick(View v) { Button bttn = (Button) v; if(bttn.getId() == R.id.bt_server_start) { /* * If BluetoothAdapter name does not start with BT_ prefix, add it to the name, then proceed */ if (!mBluetoothAdapter.getName().startsWith(PREFIX)) mBluetoothAdapter.setName(PREFIX + mBluetoothAdapter.getName()); /* * Check the connectability/discoverability, compare to the statemanet made by user that it is enabled. * If they are not equal, run BTDiscoverable method - set device to be discoverable by other devices */ if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { (If your BTDiscoverable is this equivalent if this, you can use it) Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } else { /* * As the bluetooth is now on and the device can be connected/discovered, USER MUST START THE SERVER SITE ON DEVICE BY * CREATING NEW THREAD */ DeviceServer = new AcceptThread(); /* * Start the thread */ DeviceServer.start(); /* * Set the availability of the button */ bttn.setEnabled(false); ((Button) this.findViewById(R.id.bt_server_stop)).setEnabled(true); } } else if (bttn.getId() == R.id.bt_server_stop) { DeviceServer.cancel(); bttn.setEnabled(false); ((Button) this.findViewById(R.id.bt_server_start)).setEnabled(true); BTDeviceName(); } } </code></pre> <p>If you really want the button to be disabled immediately when the user chooses "yes", then you should try to launch this intent using startActivityForResult(disoverableIntent). Then the onActivityResult function (you can override it in your activity) will be called and you should be able to see here if the user accepted the discoverability or not. So you would get something like:</p> <pre><code>@override protected void onActivityResult(int code, int result, Intent intent) { if(result == 1) // Here i'm not sure how to check that the user accepted the discoverability /* * As the bluetooth is now on and the device can be connected/discovered, USER MUST START THE SERVER SITE ON DEVICE BY * CREATING NEW THREAD */ DeviceServer = new AcceptThread(); /* * Start the thread */ DeviceServer.start(); /* * Set the availability of the button */ bttn.setEnabled(false); ((Button) this.findViewById(R.id.bt_server_stop)).setEnabled(true); } </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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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