其他分享
首页 > 其他分享> > 在API级别22中不推荐使用android – stopLeScan()和startLeScan() – 如何使用stopScan()和startScan()替换它?

在API级别22中不推荐使用android – stopLeScan()和startLeScan() – 如何使用stopScan()和startScan()替换它?

作者:互联网

我看到stopLeScan()&在Android 5.1.1中不推荐使用startLeScan().我有问题替换我的stopLeScan()& startLeScan()方法.这是我的以下代码:

@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();
                    }
                });
            }
        };

这些是我遇到的问题:

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

我如何更换这些stopLeScan()&使用stopScan()& start的startLeScan()方法startScan()方法?任何建议都会很棒!谢谢.

解决方法:

支持不同的平台版本

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

关于来源:https://github.com/captain-miao/bleYan

标签:android,bluetooth,bluetooth-lowenergy,android-bluetooth,android-ble
来源: https://codeday.me/bug/20190623/1269596.html