Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
哪个Android API发出扫描请求和响应_Android_Android Ble - Fatal编程技术网

哪个Android API发出扫描请求和响应

哪个Android API发出扫描请求和响应,android,android-ble,Android,Android Ble,是Android gatt connect还是Android ScangeBluetoothLeScanner导致扫描请求和响应? 如果我们知道BLE设备地址,我们可以直接连接到它而不发现服务吗?在Android BLE扫描中,扫描请求的结果将以您想要的方式返回,例如 List<ScanFilter> filters = new ArrayList<ScanFilter>(); ScanFilter filter = new ScanFilter.Builder()

是Android gatt connect还是Android ScangeBluetoothLeScanner导致扫描请求和响应?
如果我们知道BLE设备地址,我们可以直接连接到它而不发现服务吗?

在Android BLE扫描中,扫描请求的结果将以您想要的方式返回,例如

List<ScanFilter> filters = new ArrayList<ScanFilter>();

ScanFilter filter = new ScanFilter.Builder()
        .setServiceUuid(uuid)
        .setDeviceAddress(address)
        .setDeviceName(name)
        .build();
filters.add(filter);

希望这能有所帮助。

第二个问题的答案更高级一些。由于API中缺少地址类型参数,只有在上次蓝牙重置后扫描了设备地址,或者该地址已绑定,才能可靠地连接到设备地址。@Emil,在这种情况下,我可以使用另一个回调来扫描并连接重定向到设备,而不会发现服务。BluetoothAdapter.LeScanCallback mLeScanCallback=新建BluetoothAdapter.LeScanCallback。这个回调将允许我扫描并添加它找到的任何设备。@DuyKyou如果我没有错的话,LeScanCallback将一直使用到Android J,将来将使用getBluetoothScanner。startScan@Emil是的,它已经连接,现在尝试再次连接,但在不同的服务上,但在此之前断开连接后,我调用了刷新缓存。@Raulp是的,我用的是mBluetoothLeScanner。startScan。。。;,因为从android API 21来看,LeScanCallback是不推荐的。
ScanCallBack mCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            if (result != null){
                BluetoothDevice device = result.getDevice();
                mDeviceList.add(device);
                removeDuplicateWithOrder(mDeviceList);
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            Log.e("TAG", "Scan failed " + errorCode);
        }
    };
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    return true;
}