Jasonchenlijian / FastBle

Android Bluetooth Low Energy (BLE) Fast Development Framework. It uses simple ways to filter, scan, connect, read ,write, notify, readRssi, setMTU, and multiConnection.
Apache License 2.0
5.28k stars 1.22k forks source link

How to scan for Bluetooth Devices #540

Open EddieXu123 opened 2 years ago

EddieXu123 commented 2 years ago

Hi there! Thank you so much for this awesome library!

I'm pretty new to Android programming and was hoping to build an app that can detect a local bluetooth device. I use LightBlue to see if the device is available, and it is (I have the device name and can access one of the service UUIDs). I was just wondering how I would write a simple template to scan for this device with my app? I currently have followed the template up to the BleManager.getInstance().scan(new BleScanCallback() { ... } code, and can see that I have imported everything correctly. In my onCreate(Bundle savedInstanceState) starting function, I did the BleManager.getInstance().init(getApplication());. However, I then have the following code, which works up until the BleManager.getInstance().scan(new BleScanCallback() {...} stuff (in comments right now):

Screen Shot 2022-04-14 at 11 07 28 PM

I was just wondering, how should I write this app such that the scan works? I'm assuming it's because I have everything in my onCreate method but I'm pretty new with callbacks and such, so I wasn't sure. Is there an example project in Java that demonstrates a simple scan for bluetooth devices? Thank you so much!