Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect bluetooth Le devices in Android

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

like image 865
kmn Avatar asked Feb 06 '15 11:02

kmn


People also ask

How do I find Bluetooth devices in my App?

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.

What can I do with Bluetooth LE scanner tools?

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.

What is Bluetooth LE and where can it be used?

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.

What is Bluetooth device discovery?

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.


1 Answers

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"/>
like image 70
DanM Avatar answered Oct 24 '22 01:10

DanM