I debug my BLE scan application.
First I initialize BLE stuff.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the adapter used to populate the list of beacons and attach it to the widget
scanAdapter = new ScanResultArrayAdapter(this);
final ListView scanResults = (ListView) findViewById(R.id.listResults);
scanResults.setAdapter(scanAdapter);
// set up a handler for taps on the start/stop scanning button
btn_scan = (Button) findViewById(R.id.buttonScan);
btn_scan.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
ToggleScan();
}
});
// retrieve the BluetoothManager instance and check if Bluetooth is enabled. If not the
// user will be prompted to enable it and the response will be checked in onActivityResult
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bleDev = bluetoothManager.getAdapter();
if (bleDev == null || !bleDev.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
When I press a button I get to the start function ( I omitted ToggleScan it leads to StartScan) . And all goes OK. I see - Starting BLE scan...
private void StartScan()
{
if(scanner == null)
{
scanner = bleDev.getBluetoothLeScanner();
if(scanner == null)
{
// probably tried to start a scan without granting Bluetooth permission
Toast.makeText(this, "Failed to start scan (BT permission granted?)", Toast.LENGTH_LONG).show();
Log.w(TAG, "Failed to get BLE scanner instance");
return;
}
}
Toast.makeText(this, "Starting BLE scan...", Toast.LENGTH_SHORT).show();
// clear old scan results
scanAdapter.clear();
List<ScanFilter> filters = new ArrayList<>();
ScanSettings settings = new ScanSettings.Builder().setScanMode(scanMode).build();
scanner.startScan(filters, settings, bleScanCallback);
isScanning = true;
btn_scan.setText("Stop scanning");
}
But I never get a callback.
// class implementing BleScanner callbacks
private ScanCallback bleScanCallback = new ScanCallback()
{
@Override
public void onScanResult(int callbackType, ScanResult result)
{
super.onScanResult(callbackType, result);
final BluetoothDevice dev = result.getDevice();
final int rssi = result.getRssi();
if(dev != null && isScanning)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
// retrieve device info and add to or update existing set of beacon data
String name = dev.getName();
String address = dev.getAddress();
scanAdapter.update(dev, address, name == null ? "Unnamed device" : name, rssi);
}
});
}
}
//and so on .....
Also I have a device sending beacons. I can see it with a beacons sniffer.
What can be a problem?