BLE - 我该怎么检查广告

时间:2016-03-28 14:34:03

标签: android

我尝试使用BLE框架来测试广告

// Get LE advertise Object
        BluetoothLeAdvertiser bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();

        // Setting LE advertise
        AdvertiseSettings advertiseSettings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
                .setTimeout(0)
                .setConnectable(true)
                .build();

        // UUIDs
        ParcelUuid serviceUUID = new ParcelUuid(UUID.fromString(SERVICE_UUID));

        // Setting LE advertise data
        AdvertiseData advertiseData = new AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .addServiceUuid(serviceUUID)
                .build();

        // Start advertising
        bluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertiseData, advertiseCallback);

上面的代码工作正常,但我发现如果我再次调用此方法,中央设备将显示2个相同的名称设备,当我再次调用该方法时,它将成为中央的3个同名设备,所以我尝试在Android上搜索BLE API,但是找不到任何方法可以检测到广告已经存在BluetoothLeAdvertiser,有没有办法检测广告已经存在?

编辑:

/**
 * Advertise callback
 */
private AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
    @Override
    public void onStartFailure(int errorCode) {
        super.onStartFailure(errorCode);
        GlobalVariable.logInfo(BLEUtility.class.getName(), String.format("Advertising start failed (%d)", errorCode));
    }

    @Override
    public void onStartSuccess(AdvertiseSettings settingsInEffect) {
        super.onStartSuccess(settingsInEffect);
        GlobalVariable.logInfo(BLEUtility.class.getName(), "Advertising start succeed");
    }
};

不幸的是Android没有广告检查方法,所以在onStartSuccess时只需将标志设置为true

1 个答案:

答案 0 :(得分:0)

<强> EDIT1:

您无法通过一次回调启动服务广告:

/**
 * Start Bluetooth LE Advertising. The {@code advertiseData} will be broadcasted if the
 * operation succeeds. The {@code scanResponse} is returned when a scanning device sends an
 * active scan request. This method returns immediately, the operation status is delivered
 * through {@code callback}.
 * <p>
 * Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
 *
 * @param settings Settings for Bluetooth LE advertising.
 * @param advertiseData Advertisement data to be advertised in advertisement packet.
 * @param scanResponse Scan response associated with the advertisement data.
 * @param callback Callback for advertising status.
 */
public void startAdvertising(AdvertiseSettings settings,
        AdvertiseData advertiseData, AdvertiseData scanResponse,
        final AdvertiseCallback callback) {
    synchronized (mLeAdvertisers) {
        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        if (callback == null) {
            throw new IllegalArgumentException("callback cannot be null");
        }
        if (!mBluetoothAdapter.isMultipleAdvertisementSupported() &&
                !mBluetoothAdapter.isPeripheralModeSupported()) {
            postStartFailure(callback,
                    AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED);
            return;
        }
        boolean isConnectable = settings.isConnectable();
        if (totalBytes(advertiseData, isConnectable) > MAX_ADVERTISING_DATA_BYTES ||
                totalBytes(scanResponse, false) > MAX_ADVERTISING_DATA_BYTES) {
            postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE);
            return;
        }

//look here

        if (mLeAdvertisers.containsKey(callback)) {
            postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED);
            return;
        }

        IBluetoothGatt gatt;
        try {
            gatt = mBluetoothManager.getBluetoothGatt();
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
            postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
            return;
        }
        AdvertiseCallbackWrapper wrapper = new AdvertiseCallbackWrapper(callback, advertiseData,
                scanResponse, settings, gatt);
        wrapper.startRegisteration();
    }
}

我认为没有系统方法来判断它。

private boolean started = false;
public void startadvertise(){
    if(started)return;
    started = true;
    //your code to startAdvertising
}

public void stopadvertise(){
    started = false;
    //your code to stopAdvertising
}