Trying o get a list of available bluetooth addresses in a spinner

boylesg

Estimable
Aug 16, 2015
1
0
4,510
I have an activity with a spinner, a 'connect' button, a 'disconnect' button and a 'search' button.

Upon clicking the 'search' button the spinner is supposed to fill with a list of available bluetooth devices.

You select one and then connect to it.

I want to make it such that if startDiscovery() failes to find any devices then the 'connect' button is disabled and if it does find devices then the 'connect' button is enabled.

I am trying to do this by examining the number of entries in my ArrayList - a class member of my
MyBroadcastReceiver class.

I originally had an anonymous listener for ACTION_FOUND in my activity class and it was successfully filling the spinner control with bluetooth device names and MAC addresses.

I have since changed it to a non-anonymous listener,b ut I have not yet finished with my MyBroadcastReceiver class so that it does the same.

Unfortunately I have run into the exact same road block that I had with the anonymous listener.

No matter what I do ArrayList.size() return 0. Even if I introduce an integer class member into MyBroadcastReceiver, and increment it each time my listener function is called, it returns as 0.

Even if I introduce a 5 second delay after I call startDiscovery() and then examine the number of bluetooth devices in an AlertDialog.

I simply don't comprehend what is going on with the listener and why it seems there is no possible way I can get a count of the number of bluetooth devices found.

This would be a trivial matter with C++ and Visual Studio.

My bluetooth listener function and the onclick listener that sets off startDiscovery and then pops up the number of devices found (always 0) in an AlertDialog are both highlighted in bold.

Help with understanding what the devil is going wrong would be greatly appreciated.

/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* @see SystemUiHider
*/
public class HexapodControls extends Activity
{
BluetoothAdapter m_BTAdapter;
MyBroadcastReceiver m_BTReceiver;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_hexapod_controls);

/*
final AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("In onCreate(...)");
alertDialog.setMessage("Search button clicked...");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);
*/
setupBTListener();
//doStartDiscovery();
}

public void onSearchBTClick(View arg0)
{
Button button = (Button)arg0;
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("In onSearchBTClick(...)");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);

((Spinner)findViewById(id.spinner_bluetooth)).setEnabled(false);
((Button)findViewById(id.button_connectbt)).setEnabled(false);
((Button)findViewById(id.button_disconnectbt)).setEnabled(false);
button.setEnabled(false);

doStartDiscovery();

boolean bEnable = ((Spinner)findViewById(id.spinner_bluetooth)).getCount() > 0;

//alertDialog.setMessage("bEnable = " + bEnable + "...");
//alertDialog.setMessage("bEnable = " + ((Spinner)findViewById(id.spinner_bluetooth)).getSelectedItem() + "...");
//alertDialog.show();

button.setEnabled(true);
}

public void onDisconnectBTClick(View arg0)
{
Button button = (Button)arg0;
/*
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("In onDisconnectBTClick(...)");
alertDialog.setMessage(button.getText() + " button clicked...");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);
alertDialog.show();
*/
((Spinner)findViewById(id.spinner_bluetooth)).setEnabled(false);
((Button) findViewById(id.button_connectbt)).setEnabled(false);
button.setEnabled(true);
((Button)findViewById(id.button_searchbt)).setEnabled(false);
}

public void onConnectBTClick(View arg0)
{
Button button = (Button)arg0;
/*
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("In onConnectBTClick(...)");
alertDialog.setMessage(button.getText() + " button clicked...");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);
alertDialog.show();
*/
((Spinner) findViewById(id.spinner_bluetooth)).setEnabled(false);
button.setEnabled(true);
((Button) findViewById(id.button_disconnectbt)).setEnabled(false);
((Button) findViewById(id.button_searchbt)).setEnabled(false);
}

void doStartDiscovery()
{
m_BTReceiver.empty();

if (m_BTAdapter.isDiscovering())
m_BTAdapter.cancelDiscovery();
m_BTAdapter.startDiscovery();

long longStart = System.currentTimeMillis(),
longTimer;
boolean bDone = false;

while (/*(m_BTAdapter.isDiscovering()) &&*/ !bDone)
{
longTimer = System.currentTimeMillis();
bDone = (longTimer - longStart) > (long)5000;
}
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("In onConnectBTClick(...)");
alertDialog.setMessage("Number of devices: " + m_BTReceiver.getNumDevices() + "...");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);
alertDialog.show();
}
void setupBTListener()
{
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Bluetooth discovery");
alertDialog.setMessage("Bluetooth device discovered...");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (DialogInterface.OnClickListener) null);
m_BTAdapter = BluetoothAdapter.getDefaultAdapter();

if (m_BTAdapter == null)
{
alertDialog.setTitle("Bluetooth is not available!");
alertDialog.setMessage("Please install this app on a mobile phone with bluetooth...");
alertDialog.show();
}
else if (!m_BTAdapter.isEnabled())
{
alertDialog.setTitle("Bluetooth is not enabled!");
alertDialog.setMessage("Please enable bluetooth...");
alertDialog.show();
}
else
{
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
m_BTReceiver = new MyBroadcastReceiver();
this.registerReceiver(m_BTReceiver, filter);
}
}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
}

}

class MyBroadcastReceiver extends BroadcastReceiver
{
ArrayList<String> m_arrayBTDevices;

MyBroadcastReceiver()
{
m_arrayBTDevices = new ArrayList<>();
}

ArrayAdapter<String> getAdapter()
{
ArrayAdapter<String> BTArrayAdapter = new ArrayAdapter<String>(null, android.R.layout.simple_list_item_1, m_arrayBTDevices);

return BTArrayAdapter;
}

int getNumDevices()
{
return m_arrayBTDevices.size();
}

void empty()
{
m_arrayBTDevices.clear();
}

public void onReceive(Context context, Intent intent)
{
String strAction = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(strAction))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice BTDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

// Add the name and the MAC address of the object to the arrayAdapter
m_arrayBTDevices.add(BTDevice.getName() + ": " + BTDevice.getAddress());
}
}
}


 

itmoba

Estimable
Aug 14, 2015
153
0
4,660
Are you sure your 'AndroidManifest.xml' has the proper 'user-permission android:"blah blah blah"' configuration? One of the problems I noticed is coming from variable scope. You also need to be wrapping 'run()' (with @Override) with the proper "private Handler X;" as needed.