I'm writing an Android-App, which is supposed to discover devices via Bluetooth.
I don't get any exceptions, but the devices are just not found, even though my windows pc finds them (and can be found itself).
I'm certain they are BLE, but I tried both ways. And of course I tried them separately.
Here is my ListActivity, which searches for the devices:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
setContentView(R.layout.bluetooth_list_view);
listView = getListView();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
Log.d(TAG, "on Create start5");
if (mBluetoothAdapter == null) {
Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();
finish();
return;
}
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
setListAdapter(mLeDeviceListAdapter);
listView.setAdapter(mLeDeviceListAdapter);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
}
private void scanLeDevice(final boolean enable) {
Log.e(TAG, "scanLeDevice: " + enable);
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.e(TAG, "after Scan mLeDeviceListAdapter: " + mLeDeviceListAdapter.getCount());
Log.e(TAG, "after Scan isEmpty(): " + mLeDeviceListAdapter.isEmpty());
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothLeScanner.startScan(mScanCallback);
} else {
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
}
invalidateOptionsMenu();
}
The logs say after Scan mLeDeviceListAdapter: 0 and after Scan isEmpty(): true.
ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.e(TAG, "onScanResult: " + result);
super.onScanResult(callbackType, result);
mLeDeviceListAdapter.addDevice(result.getDevice());
mLeDeviceListAdapter.notifyDataSetChanged();
}
};
That log (onScanResult) is never called.
And for not BLE: private final BroadcastReceiver mReceiver = new BroadcastReceiver() { //TODO not ble example public void onReceive(Context context, Intent intent) { String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.e(TAG, "Not BLE discovry starts");
//discovery starts, we can show progress dialog or perform other tasks
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.e(TAG, "Not BLE discovry finishes");
//discovery finishes, dismis progress dialog
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//bluetooth device found
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(TAG, "Found device " + device.getName());
}
}
};
Not BLE discovry finishes is printed and so is Not BLE discovry starts. Found device is never printed. But I'm positiv that the devices are BLE.
I have the necessary permissions in my manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
What am I doing wrong?
Edit: i really need help. Thinks I tried/checked so far:
- Enabled Bluetooth adapter.
- Bluetooth usage permissions granted.
- Location permission (ACCESS_COARSE and ACCESS_FINE) granted.
- Made sure gps provider is enabled (Smartphone GPS settings).