Reputation: 21
I have combed the forums in order to try and figure out why my bluetooth BroadcastReceiver isn't working and all I can find is people saying to ensure that coarse/fine location permissions are requested. I'm already doing that here (method is called before the discovery method):
Request Permissions
private void requestPermissions()
{
final int CODE = 5; // app defined constant used for onRequestPermissionsResult
boolean allPermissionsGranted = true;
String[] permissionsToRequest =
{
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
for(String permission : permissionsToRequest)
{
allPermissionsGranted = allPermissionsGranted &&
(ContextCompat.checkSelfPermission(this, permission)
== PackageManager.PERMISSION_GRANTED);
}
if(!allPermissionsGranted)
{
ActivityCompat.requestPermissions(this, permissionsToRequest, CODE);
}
}
The problem is that my BroadcastReceiver never finds other discoverable devices, if a device is already paired, it puts it in a ListView as expected, but once the device is unpaired, it cannot identify it again. See screenshots below.
I'm at a loss at this point, it feels like I've tried everything, if anyone has any ideas you'd save my life and I'd be eternally grateful.
Thanks,
Broadcast Receiver
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(btDevice.getBondState() != BluetoothDevice.BOND_BONDED)
{
newDevicesArrayAdapter.add(btDevice.getName() + "\n" + btDevice.getAddress());
}
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
setTitle(R.string.select_device);
if(newDevicesArrayAdapter.getCount() == 0)
{
newDevicesArrayAdapter.clear();
String noDevices = getResources().getText(R.string.none_found).toString();
newDevicesArrayAdapter.add(noDevices);
}
}
}
};
onCreate - Setup ListView elements and IntentFilters
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_machine);
setResult(Activity.RESULT_CANCELED);
pairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
newDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
pairedListView.setAdapter(pairedDevicesArrayAdapter);
pairedListView.setOnItemClickListener(deviceClickListener);
newDevicesListView.setAdapter(pairedDevicesArrayAdapter);
newDevicesListView.setOnItemClickListener(deviceClickListener);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver, filter);
}
doDiscovery() - Called by onStart method, I've verified that it gets called
private void doDiscovery()
{
Log.d(TAG, "doDiscovery()");
requestPermissions();
setTitle(R.string.scanning);
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
if(btAdapter.isDiscovering()) { btAdapter.cancelDiscovery(); }
newDevicesArrayAdapter.clear();
btAdapter.startDiscovery();
}
Upvotes: 0
Views: 732
Reputation: 21
Turns out I was just being stupid and had values updating to both ListViews, so I never saw the list of other devices.
newDevicesListView.setAdapter(pairedDevicesArrayAdapter);
Should have been:
newDevicesListView.setAdapter(newDevicesArrayAdapter);
Upvotes: 1