android - 如何重启BluetoothLeScanner?

标签 android bluetooth bluetooth-lowenergy android-bluetooth

我正在使用 Android 端的 BluetoothLeScanner 将 Android 设备连接到 iOS。这一切都是第一次完美运行。

问题是,在我连接到其中一个已发现的 iOS 设备后,我停止了扫描 (stopScan(scanCallback)) 以保存能量。

如果之前连接的设备由于某种原因断开连接,我会再次开始扫描...但似乎在“重新启动”扫描后不再工作 - 它不会发现更多设备...不知道为什么但我'被迫重启应用程序。

有什么办法解决这个问题吗?

我还使用 ScanFilter 按 UUID 过滤设备,并使用 ScanSettings.SCAN_MODE_BALANCED 作为扫描模式。

代码如下:

private void startScanner() {
    BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(activity.BLUETOOTH_SERVICE);
    BluetoothAdapter adapter = bluetoothManager.getAdapter();
    scanner = adapter.getBluetoothLeScanner();
    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();
    scanner.startScan(scanFilters(), settings, scanCallback);
    System.out.println("Scanner started");
}

private List<ScanFilter> scanFilters() {
    ScanFilter filter = new ScanFilter.Builder().setServiceUuid(THE_BT_UUID).build();
    List<ScanFilter> list = new ArrayList<ScanFilter>(1);
    list.add(filter);
    return list;
}



private final ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        BluetoothDevice device = result.getDevice();
        if (device != null) {
            callback.foundRemoteDevice(BluetoothCentral.this, device);
        }
    }
}


public void connectToDevice(BluetoothDevice device) {
    if (gatt == null) {
        gatt = device.connectGatt(activity, false, gattCallback);
        scanner.stopScan(scanCallback);
    }
}


private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        System.out.println("onConnectionStateChange; status: " + status + ", state: " + newState);

        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                System.out.println("STATE_CONNECTED");
                if (!startedServiceDiscovery) {
                    gatt.discoverServices();
                    startedServiceDiscovery = true;
                }
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                callback.didDisconnectFromDevice(BluetoothCentral.this, gatt.getDevice());
                gatt = null;
                startScanner();
                break;
            default:
                System.out.println("STATE_OTHER");
        }

    }
}

请注意,我在调用 connectGatt 后立即停止扫描器,并在收到 BluetoothProfile.STATE_DISCONNECTED 回调时再次启动它。

最佳答案

作为我的评论,帮助您解决了问题,我在这里添加我的评论作为答案。

Code seems ok to me, did you try adding logs in onScanResult method. to check if device is getting detected but something else is failing. One more thing, I see if I restart scanning it takes time to get the device detected in comparison of first scanning.

关于android - 如何重启BluetoothLeScanner?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898861/

相关文章:

bluetooth - 为什么 BluetoothLEAdvertisementWatcher 停止触发 `Received` 事件?

java - ant jar 错误 : Execute failed: java. io.IOException : Cannot run program. ..${aapt}": error=2, 没有这样的文件或目录

android - JSON异常 : Value -- array hasn't got a label

android - 试图建立我自己的通知,但没有任何效果

ios - 通过蓝牙将音频从麦克风流式传输到另一部 iPhone

android - 如何从自定义 BLE 服务(例如智能 watch )读取数据

bluetooth-lowenergy - 如何修改 BLE Gatt 服务器配置文件

android - 从 Android 应用程序读取 HTML 源代码

python - 在 SWIG 中获取从 C++ 到 Python 的 char* 输出

bluetooth-lowenergy - 禁用 LE Set Scan Enable 命令的 "Filter duplicates"设置