[英]BLE - how can i check advertise
我試圖使用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:
您無法通過一個回調開始服務式廣告:
/**
* 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.