I am new to android and i am making an application with bluetooth functionalities. I am able to set the bluetooth adaptor, an fetch my own device information, but i could not use startdiscovery to discover bluetooth devices. When i start a scan it does nothing.
i am using an onclicklistner to start a scan:
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!(bluetooth.isEnabled())) {
status = "Bluetooth is not Enabled.";
Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
else
{
scand();
}
}
Here is the onActivityResult function which i have put just after the "public void onCreate" function:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println(resultCode);
if (resultCode == RESULT_CANCELED) {
status="Error Enabling bluetooth";
Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show();
} else {
scand();
}
}
This is my scand function, in which i am callng startdiscovery:
private void scand()
{
bluetooth.startDiscovery();
Log.d("itmes", ""+items.size());
item1 = new String[items.size()];
item1 = items.toArray(item1);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose a device");
builder.setItems(item1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), item1[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
This is the broadcastReceiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
Log.e("br", "--- device found ---");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
items.add(device.getName());
}
}
};
In the Above code for broadcastreceiver, i am trying to put the found device name in a string ArrayList "items".
I am registering broadcastreceiver inside the oncreate functon like this:
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
i have set bluetooth permissions in the androidmanifest file. In the scand function above, it is suppose to show the list of discovered devices, but it is displaying an empty dialog with just the title. Please tell me how to use startdiscovery and broadcastreceiver properly to display the result in the alertdialog.
Check you have
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
in your manifest.
See https://developer.android.com/reference/android/bluetooth/BluetoothDevice#ACTION_FOUND
The startDiscovery() is asynchronous you will not receive the result right after the call. Move the code to show the dialog to a function let say public void showScanResult() and call that in your onReceive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With