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
所以唯一的辦法就是設置一個標誌...確定... – user3814793
@ user3814793哪裏是你advertiseCallback?我認爲這是key.Just用一個成員來存儲它,你將只能啓動一個廣告。 –
什麼是「mLeAdvertisers」? – user3814793