Reputation: 135
I want to get list of all nearby discoverble bluetooth devices in my app but i am not getting any list. My deviceDiscovery() method is executing fine but not getting any response in onReceive() in BroadcastReceiver in onCreate(). Can anyone tell me why so? Here is my code:
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
BluetoothAdapter ba;
ToggleButton tb;
ArrayAdapter aa;
ListView l;
private static final int ENABLE_REQUEST=1;
private static final int DISCOVERABLE_REQUEST=2;
private static final int DISCOVERABLE_DURATION=10;
BroadcastReceiver br=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice bd= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
aa.add(bd.getName()+" "+bd.getAddress());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb= (ToggleButton) findViewById(R.id.toggle);
aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1);
l= (ListView) findViewById(R.id.list);
l.setAdapter(aa);
ba= BluetoothAdapter.getDefaultAdapter();
tb.setOnClickListener(this);
}
@Override
public void onClick(View v) {
aa.clear();
ToggleButton tb= (ToggleButton) v;
if(ba==null) {
Toast.makeText(this, "This feature is not supported", Toast.LENGTH_SHORT).show();
tb.setChecked(false);
}
else
{
if(tb.isChecked())
{
if(!ba.isEnabled())
{
Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i,ENABLE_REQUEST);
}
else
{
Toast.makeText(this, "Your device is already enabled", Toast.LENGTH_SHORT).show();
deviceDiscovery();
makeDiscoverable();
}
}
else
{
aa.clear();
ba.disable();
Toast.makeText(this, "Your device is now disabled", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case ENABLE_REQUEST:if(resultCode==RESULT_OK)
{
Toast.makeText(this, "Bluetooth Enabled", Toast.LENGTH_SHORT).show();
deviceDiscovery();
makeDiscoverable();
}
else
{
Toast.makeText(this, "Bluetooth Enabling Rejected", Toast.LENGTH_SHORT).show();
tb.setChecked(false);
}
break;
case DISCOVERABLE_REQUEST:if(resultCode==DISCOVERABLE_DURATION)
{
Toast.makeText(this, "Your device is discoverable by other devices", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Your device is not discoverable", Toast.LENGTH_SHORT).show();
}
break;
}
}
private void makeDiscoverable() {
Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,DISCOVERABLE_DURATION);
startActivityForResult(i,DISCOVERABLE_REQUEST);
}
private void deviceDiscovery()
{
if(ba.startDiscovery())
{
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(br,filter);
Toast.makeText(this, "Discovering other bluetooth devices", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Discovery failed to start", Toast.LENGTH_SHORT).show();
}
}
}
MainActivity.xml
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off"
android:id="@+id/toggle"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_below="@+id/toggle"/>
AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Upvotes: 1
Views: 2929
Reputation: 176
Add filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
and run project. You can read more Getting the address and names of all available bluetooth devices in android
Upvotes: 1