Note that there are some explanatory texts on larger screens.

plurals
  1. POCall a method on a button press held in another class in Android
    text
    copied!<p>I'm brand new to Android and I'm trying to test the new Bluetooth LE functionality Android 4.3 has. I've followed the documentation found <a href="http://developer.android.com/guide/topics/connectivity/bluetooth-le.html" rel="nofollow">here on the Android site</a>. But i'm at a stumbling block of actually starting my code.</p> <p>I was thinking of simply having a button that called my scanLeDevice code, but I can't see anything that can allow me to do this simply. Literally all I have is this class and the sample hello word app they make when you make a new project. </p> <p>Can anyone help me with this? I know it's noobish, but I'm really stumped. </p> <p>For reference, my Bluetooth class is this:</p> <pre><code>public class DeviceScanActivity extends ListActivity { private BluetoothAdapter mBluetoothAdapter; private boolean mScanning; private Handler mHandler; // Stops scanning after 10 seconds. private static final long SCAN_PERIOD = 10000; private ArrayAdapter&lt;Object&gt; list; private Handler handler = new Handler(); protected void OnCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); list = new ArrayAdapter&lt;Object&gt;(getApplicationContext(), android.R.layout.simple_list_item_1); setListAdapter(list); scanLeDevice(true); } @TargetApi(18) public void scanLeDevice(final boolean enable) { list.add("Scanning..."); final BluetoothAdapter adapter = getBluetoothAdapter(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; // mBluetoothAdapter.stopLeScan(mLeScanCallback); adapter.stopLeScan(callback); list.clear(); } }, SCAN_PERIOD); mScanning = true; adapter.startLeScan(callback); } else { mScanning = false; adapter.stopLeScan(callback); } } @TargetApi(18) private BluetoothAdapter getBluetoothAdapter() { BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); return manager.getAdapter(); } private final BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { // TODO Auto-generated method stub { list.add("found: " + device); runOnUiThread(new Runnable(){ @Override public void run() { list.add(device); list.notifyDataSetChanged(); } }); } } }; </code></pre> <p>}</p>
 

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