Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - stopLeScan() & startLeScan() deprecated in API Level 22 - How do I go about replacing this with stopScan() and startScan()?

Tags:

I see that stopLeScan() & startLeScan() are deprecated in Android 5.1.1. I am having issues replacing my stopLeScan() & startLeScan() methods. Here is my following code:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null) return;
    final Intent intent = new Intent(this, MainActivity.class);
    //intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
    intent.putExtra(MainActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    if (mScanning) {
        mBluetoothAdapter.stopScan(mLeScanCallback);
        mBluetoothAdapter.getBluetoothLeScanner();
        mScanning = false;
    }
    startActivity(intent);
}

private void scanLeDevice(final boolean enable) {
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                invalidateOptionsMenu();
            }
        }, SCAN_PERIOD);

        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
    invalidateOptionsMenu();
}

// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;
    private LayoutInflater mInflator;

    public LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<BluetoothDevice>();
        mInflator = BTDeviceList.this.getLayoutInflater();
    }

    public void addDevice(BluetoothDevice device) {
        if(!mLeDevices.contains(device)) {
            mLeDevices.add(device);
        }
    }

    public BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    public void clear() {
        mLeDevices.clear();
    }

    @Override
    public int getCount() {
        return mLeDevices.size();
    }

    @Override
    public Object getItem(int i) {
        return mLeDevices.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        final String deviceName = device.getName();
        if (deviceName != null && deviceName.length() > 0)
            viewHolder.deviceName.setText(deviceName);
        else
            viewHolder.deviceName.setText(R.string.unknown_device);
        viewHolder.deviceAddress.setText(device.getAddress());

        return view;
    }
}

// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String btDeviceName = device.getName();
                        if (btDeviceName.startsWith("Nonin3230")) {
                            mLeDeviceListAdapter.addDevice(device);
                        }
                        mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                });
            }
        };

These are the lines I'm having issues with:

mBluetoothAdapter.stopScan(mLeScanCallback); //In onListItemClick() method
mBluetoothAdapter.stopLeScan(mLeScanCallback); //In run()method in scanLeDevice() method
mBluetoothAdapter.startLeScan(mLeScanCallback); //In scanLeDevice() method
mBluetoothAdapter.stopLeScan(mLeScanCallback); //In scanLeDevice() method

How do I go about replace these stopLeScan() & startLeScan() methods with stopScan() & startScan() methods? Any suggestion would be great! Thanks.

like image 967
user268397 Avatar asked Apr 01 '16 18:04

user268397


People also ask

How do you check Bluetooth is on or off in Android programmatically?

Call isEnabled() to check whether Bluetooth is currently enabled. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult() , passing in an ACTION_REQUEST_ENABLE intent action.

How do I scan my Android phone with Bluetooth?

To start discovering devices, call startDiscovery() . The process is asynchronous and returns a boolean value indicating whether discovery has successfully started. The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each device found to retrieve its Bluetooth name.

How do I get a list of Bluetooth devices on my Android phone?

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.

What is BluetoothAdapter Android?

The BluetoothAdapter lets you perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices.


1 Answers

Supporting Different Platform Versions

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ...
        startScan()
    } else {
        ...
        startLeScan()
    }

about the source:https://github.com/captain-miao/bleYan

like image 142
qinmiao Avatar answered Oct 11 '22 07:10

qinmiao