I'm a beginner in android app development. I've tried reading the documentation but am getting nowhere (functions in Android's tutorial such as StartLeScan()
have been deprecated, etc...)
Is there a simple function that returns a list of bluetooth devices ?
something like getDevices()
-> (list of devices) ?
Thank you
Make sure you have the appropriate Bluetooth permissions and set up your app for Bluetooth before attempting to find Bluetooth devices. Device discovery is a scanning procedure that searches the local area for Bluetooth-enabled devices and requests some information about each one.
Using Bluetooth LE scanner tools will help you determine which nearby devices use Bluetooth in their communications. How can we investigate them? You can gather valuable information from nearby devices you do not know about.
Those can be found within domestic environments, such as smart toys, locks or appliances. We carry others everywhere, such as smart watches, cloth tags, electric cars, bicycles and those present in the workplace. Bluetooth LE is widely used by IOT devices due to its low consumption and compatibility with many other devices.
Device discovery is a scanning procedure that searches the local area for Bluetooth-enabled devices and requests some information about each one. This process is sometimes referred to as discovering, inquiring, or scanning.
basically it depends on which android version you are targeting. since the api has changed a bit in lollipop (21).
in your activity, get the bluetooth adapter
BluetoothManager bm = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE)
BluetoothAdapter mBluetoothAdapter = bm.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
then you should check which android version you are targeting
int apiVersion = android.os.Build.VERSION.SDK_INT;
if (apiVersion > android.os.Build.VERSION_CODES.KITKAT){
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
// scan for devices
scanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
// get the discovered device as you wish
// this will trigger each time a new device is found
BluetoothDevice device = result.getDevice();
}
});
} else {
// targetting kitkat or bellow
mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// get the discovered device as you wish
}
});
// rest of your code that will run **before** callback is triggered since it's asynchronous
dont forget to add permissions in your manifest
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With