【问题标题】:Reacting to BLE directed advertising (ADV_DIRECT_IND) in Android对 Android 中的 BLE 定向广告 (ADV_DIRECT_IND) 做出反应
【发布时间】:2015-08-07 14:34:11
【问题描述】:

如何应对 Android 中的定向广告 (ADV_DIRECT_IND == 0001)?

有一个 BLE 小工具向 Android 手机发送定向广告(目前使用手机的硬编码 MAC 地址),在我的 Android 应用程序中,我想做出反应并启动与小工具的连接并阅读 @来自小工具的 987654321@ 值:

请通过Android 5 API告知是否可行。

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy android-bluetooth android-ble


    【解决方案1】:

    直接广告确实有效 - 至少在 HTC M8 手机和 Android 5.0(API 级别 21 及更高版本)上有效。

    解决方案是将设备地址添加到ScanFilter

    如果您将过滤器留空,则不会调用扫描回调。

    这是我的工作代码:

    public static final String TAG = "My_BLE_app";
    
    public static final String DEVICE_1 = "D4:BE:84:72:5B:8E";
    public static final String DEVICE_2 = "C4:39:07:19:60:E2";
    
    private Context mContext;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothLeScanner mScanner;
    private ScanSettings mSettings;
    private List<ScanFilter> mFilters = new ArrayList<ScanFilter>();
    

    首先是初始化BLE的方法:

    public boolean init() {
        BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if (mBluetoothAdapter == null)
            return false;
    
        mScanner = mBluetoothAdapter.getBluetoothLeScanner();
        mSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
    
        mFilters.add(new ScanFilter.Builder().setDeviceAddress(DEVICE_1).build());
        mFilters.add(new ScanFilter.Builder().setDeviceAddress(DEVICE_2).build());
    
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
    }
    

    然后是扫描回调:

    private ScanCallback mDirectedScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            processResult(result);
        }
    
        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            for (ScanResult result: results) {
                processResult(result);
            }
        }
    
        private void processResult(ScanResult result) {
            BluetoothDevice device = result.getDevice();
            if (device == null)
                return;
    
            String address = device.getAddress();
            if (!BluetoothAdapter.checkBluetoothAddress(address))
                return;
    
            int rssi = result.getRssi();
    
            Log.d(TAG, "address=" + address + ", rssi=" + rssi);
    
            // TODO connect to the device in under 1 second
        }
    };
    

    最后是开始扫描的代码:

    mScanner.startScan(mFilters, mSettings, mDirectedScanCallback);
    

    一个问题仍然悬而未决:

    我不知道如何检测 Android 端的扫描类型 - 即扫描是否被定向

    【讨论】:

      【解决方案2】:

      部分回答未决问题:
      我注意到ScanRecord.getBytes() 是空的 - 全部为 '\0' - 用于定向扫描 (ADV_DIRECT_IND) - 但否则将包含广告数据 (ADV_IND)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 2012-01-18
        相关资源
        最近更新 更多