2016-03-28 120 views
0

我試圖用BLE框架來測試廣告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

回答

0

EDIT1:

你不能用一個回調啓動servel廣告:

/** 
* 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 
} 
+0

所以唯一的辦法就是設置一個標誌...確定... – user3814793

+0

@ user3814793哪裏是你advertiseCallback?我認爲這是key.Just用一個成員來存儲它,你將只能啓動一個廣告。 –

+0

什麼是「mLeAdvertisers」? – user3814793