Reputation: 11829
I am trying to scan for bluetooth devices, but the register is never called:
public class MainActivity extends Activity {
ArrayList<String> arr_devices = new ArrayList<String>();
ArrayList<String> arr_in_range = new ArrayList<String>();
Button btn_devices;
BluetoothAdapter bluetoothAdapter;
IntentFilter filter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btn_devices = (Button)findViewById(R.id.search);
btn_devices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
if (bluetooth != null)
{
if (bluetooth.isEnabled())
{
//this is called
bluetoothAdapter.startDiscovery();
Log.i("State", bluetoothAdapter.getState() + ""); //12 (STATE_ON)
Log.i("Discovery", bluetoothAdapter.isDiscovering() + ""); //FALSE
filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myreceiver, filter);
//this is where the program stops. No more actions are performed
}
else
{
Toast.makeText(getApplicationContext(), "Bluetooth is not enabled!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myreceiver);
}
final BroadcastReceiver myreceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//I placed a Log statement here but it doesn't appear in the logcat
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
}
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("device", device.getName() + "\n" + device.getAddress());
}
}};
}
Bluetooth is enabled. What am I missing?
Upvotes: 4
Views: 1993
Reputation: 8292
Try to put your broadcast receiver before
bluetoothAdapter.startDiscovery();
Cause you startDiscovery and after you listen broadcastListener.
Upvotes: 2
Reputation: 39698
Why do you have two bluetooth adapters set up? The system might be getting confused with both of them, you should only need one. I recommend you remove the line listed below, and change all references from bluetooth to bluetoothAdapter.
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
I suspect what is happening is that this is returning null, as you already have a copy of the receiver, or something similar to that.
Also, make sure you have added the permissions to the manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
All of that failing, you might try rebooting your phone. At least on my phone, bluetooth gets mucked up sometimes.
Upvotes: 1