I have looked at many examples and everything seems to be correct. I have both permissions, check to see if they are activated. I have looked at countless posts on Stackoverflow. I know it is something super tiny that I am missing, like always. I make sure that all the devices I search are visible. I have even double checked with the system bluetooth and it finds it just find. What am I doing wrong. Here is my code.
public class Bluetooth_ListView extends ListActivity
{
private BluetoothAdapter mBluetoothAdapter;
private ArrayList<Bluetooth> arrayOfFoundBTDevices;
private BroadcastReceiver mReceiver;
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Quick permission check
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
if (permissionCheck != 0) {
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
}
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
displayListOfFoundDevices();
}
private void displayListOfFoundDevices()
{
arrayOfFoundBTDevices = new ArrayList<Bluetooth>();
// Create a BroadcastReceiver for ACTION_FOUND
mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.v("discover_me", "Bluetooth receiving");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the bluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
// Create the device object and add it to the arrayList of devices
Bluetooth bluetoothObject = new Bluetooth();
bluetoothObject.setBluetooth_name(device.getName());
bluetoothObject.setBluetooth_address(device.getAddress());
bluetoothObject.setBluetooth_state(device.getBondState());
bluetoothObject.setBluetooth_type(device.getType());
bluetoothObject.setBluetooth_uuids(device.getUuids());
bluetoothObject.setBluetooth_rssi(rssi);
arrayOfFoundBTDevices.add(bluetoothObject);
Bluetooth_Adapter adapter = new Bluetooth_Adapter(getApplicationContext(), arrayOfFoundBTDevices);
setListAdapter(adapter);
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
@Override
protected void onPause()
{
super.onPause();
mBluetoothAdapter.cancelDiscovery();
}
@Override
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(mReceiver);
mBluetoothAdapter.cancelDiscovery();
}
}
Any advice would be awesome. The array is always blank no matter what. Also, the Log.v inside the onReceive is never called.