This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to Find Nordic Beacon Using BLE onLeScan() for Android

I'm an Android developer and I bought the NRF51822-Beacon kit. All I want to do is be able to "see" the beacon using Android's onLeScan() bluetooth call. However, the beacon doesn't register at all. I can see other BLE devices, but not the Nordic beacon. I have the battery installed and the purple light is flashing which should mean it's beaconing. I don't want to use the Nordic app to "see" the beacon---the whole point is to be able to do it in a generic Android app. Do you know why I can't see the beacon? Is it even possible?

Parents
  • Hi RaQ,

    The startLeScan() method from BluetoothAdapter should work for any BLE device, including nordic beacons. If you see the beacon in nRF Beacon app, nRF Master Control Panel or any other app that scans for Bluetooth Smart devices - they use the same API. I would also recommend you to use our compat library for scanning: github.com/.../Android-Scanner-Compat-Library. With t you'll have some more settings and filtering options. The scanning API on Android has changed in 5.0 and this library gives the new functionality to older platforms. but even if you use the bluetoothAdapter.startLeScan(callback) method it should work. Make sure you update your GUI in the UI thread, not in the callback thread.

    Could you post you code here?

  • Here is the whole code. It should show the device name, rssi, and bluetooth address. Even if the name is null, the latter two elements should still show. I don't have access to Lollipop, only KitKat. Yes, my app finds other BLE devices but not my beacon.

    public class MainActivity extends Activity {

    // VARIABLES - FOR FINDING BLE DEVICE
    
    BluetoothAdapter btAdapter;
    final int REQUEST_ENABLE_BT = 1;	// A value > 0 which ensures the corresponding activity is
                                        // launched as a sub-activity
    
    Button btn_name, btn_address, btn_rssi;
    
    
    /* TURNS ON BLUETOOTH */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // GENERAL SETUP
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
        setContentView(R.layout.activity_main);
    
        Log.e("Main: onCreate()", "Starting");
    
        // GET STATUS
    
        btn_name = (Button) findViewById(R.id.name);
        btn_address = (Button) findViewById(R.id.address);
        btn_rssi = (Button) findViewById(R.id.rssi);
    
        // GET BLUETOOTH ADAPTER --- NECESSARY FOR DEVICE DISCOVERY
        //
        // Also check that the Android device has Bluetooth enabled. Will request the user enable
        // Bluetooth if it is currently disabled
    
        final BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        btAdapter = btManager.getAdapter();	// The phone's Bluetooth radio
    
        if (btAdapter != null && !btAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            enableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);	// Enables Bluetooth if disabled
        }
        else
            btAdapter.startLeScan(mLeScanCallback);
    }
    
    
    /* QUIT IF THEY DIDN'T ENABLE BLUETOOTH */
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // User didn't enable Bluetooth
        if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == Activity.RESULT_CANCELED)) {
            finish();
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    
        btAdapter.startLeScan(mLeScanCallback);
    }
    
    
    /* DEVICE DISCOVERY */
    
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
    
        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
    
                    Log.e("Main: LeScanCallback()", "rssi " + rssi + " device " + device.getName() +
                            "  " + device.getAddress());
    
                    btn_name.setText(device.getName());
                    btn_address.setText(device.getAddress());
                    btn_rssi.setText(""+rssi);
                }
            });
        }
    };
    

    }

Reply
  • Here is the whole code. It should show the device name, rssi, and bluetooth address. Even if the name is null, the latter two elements should still show. I don't have access to Lollipop, only KitKat. Yes, my app finds other BLE devices but not my beacon.

    public class MainActivity extends Activity {

    // VARIABLES - FOR FINDING BLE DEVICE
    
    BluetoothAdapter btAdapter;
    final int REQUEST_ENABLE_BT = 1;	// A value > 0 which ensures the corresponding activity is
                                        // launched as a sub-activity
    
    Button btn_name, btn_address, btn_rssi;
    
    
    /* TURNS ON BLUETOOTH */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // GENERAL SETUP
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
        setContentView(R.layout.activity_main);
    
        Log.e("Main: onCreate()", "Starting");
    
        // GET STATUS
    
        btn_name = (Button) findViewById(R.id.name);
        btn_address = (Button) findViewById(R.id.address);
        btn_rssi = (Button) findViewById(R.id.rssi);
    
        // GET BLUETOOTH ADAPTER --- NECESSARY FOR DEVICE DISCOVERY
        //
        // Also check that the Android device has Bluetooth enabled. Will request the user enable
        // Bluetooth if it is currently disabled
    
        final BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        btAdapter = btManager.getAdapter();	// The phone's Bluetooth radio
    
        if (btAdapter != null && !btAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            enableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);	// Enables Bluetooth if disabled
        }
        else
            btAdapter.startLeScan(mLeScanCallback);
    }
    
    
    /* QUIT IF THEY DIDN'T ENABLE BLUETOOTH */
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // User didn't enable Bluetooth
        if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == Activity.RESULT_CANCELED)) {
            finish();
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    
        btAdapter.startLeScan(mLeScanCallback);
    }
    
    
    /* DEVICE DISCOVERY */
    
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
    
        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
    
                    Log.e("Main: LeScanCallback()", "rssi " + rssi + " device " + device.getName() +
                            "  " + device.getAddress());
    
                    btn_name.setText(device.getName());
                    btn_address.setText(device.getAddress());
                    btn_rssi.setText(""+rssi);
                }
            });
        }
    };
    

    }

Children
No Data
Related