Android BLE startLeScan 扫描已连接的设备

问题描述 投票:0回答:1

我目前正在使用 BLE 将我的 Android 设备与另一个蓝牙设备连接。

问题是,如何扫描已连接的设备?

在我的第一种方法中,我没有在连接方法之前调用stopLeScan。

这与上述问题没有问题,但它导致 ui 中断(ui 更新间隔太短),有时连接时间非常非常慢。

当我让我的应用程序在连接之前调用 stopLeDevice 后,每个问题都已解决,但又出现了一个新问题。新问题是我无法再在 scanResult 上看到连接的设备。它仅显示断开连接的设备。我仍然想监控我连接的设备。我怎样才能实现这个目标?

android bluetooth bluetooth-lowenergy
1个回答
0
投票

使用此类自动启动新的 BLE 设备。

BLE 扫描仪服务

public class BLEScanner extends Service {
    private BluetoothAdapter mBluetoothAdapter;
    private ArrayList<BluetoothDevice> mLeDevices;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mLeDevices = new ArrayList<BluetoothDevice>();
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            mBluetoothAdapter = bluetoothManager.getAdapter();
        }
        startLeScan();
        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void startLeScan() {
        scanLeDevice(true);
    }

    private void stopLeScan() {
        scanLeDevice(false);
    }

    private void scanLeDevice(boolean enable) {
        if (enable) {
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            if (!mLeDevices.contains(device)) {
                mLeDevices.add(device);
                connectToDevice(device);
            }
        }
    };

    private void connectToDevice(final BluetoothDevice device) {
        if (device != null) {
            Log.i("Tag", "Name: " + device.getAddress() + " Connecting");
            if (device.getName() != null)
                device.connectGatt(this.getApplicationContext(), false, new BluetoothCallBack(this.getApplicationContext(), BLEScanner.this));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopLeScan();
        mLeDevices.clear();
    }

    public void removeDevice(BluetoothDevice mDevice) {
        try {
            if (mLeDevices != null && mLeDevices.size() > 0)
                mLeDevices.remove(mDevice);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

现在回调类来检查 BLE 设备是否连接

public class BluetoothCallBack extends BluetoothGattCallback {
    private BLEScanner mServiceObject;

    public BluetoothCallBack(Context mContext, BLEScanner mServiceObject) {
        this.mServiceObject = mServiceObject;
    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i("Tag", "CONNECTED DEVICE: " + gatt.getDevice().getAddress());
            gatt.discoverServices();
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.e("Tag", "DISCONNECTED DEVICE: " + gatt.getDevice().getAddress());
            gatt.disconnect();
            gatt.close();
            mServiceObject.removeDevice(gatt.getDevice());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.