Я новичок в Android и BLE. В настоящее время я пытаюсь рекламировать пакет данных, который периодически изменяется в Android через BLE. Я использовал следующий код, который был доступен в https://source.android.com/devices/bluetooth/ble_advertising.
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder())
.setLegacyMode(true) // True by default, but set here as a reminder.
.setConnectable(false)
.setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
.setTxPowerLevel(AdvertisingSetParameters.TX_POWER_HIGH)
.build();
AdvertiseData data = (new AdvertiseData.Builder()).setIncludeDeviceName(true).build();
final AdvertisingSet[] currentAdvertisingSet = new AdvertisingSet[1];
//final AdvertisingSet[] currentAdvertisingSet = {null};
AdvertisingSetCallback callback = new AdvertisingSetCallback() {
@Override
public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
+ status);
currentAdvertisingSet[0] = advertisingSet;
}
@Override
public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
Log.i(LOG_TAG, "onAdvertisingDataSet() :status:" + status);
}
@Override
public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {
Log.i(LOG_TAG, "onScanResponseDataSet(): status:" + status);
}
@Override
public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
Log.i(LOG_TAG, "onAdvertisingSetStopped():");
}
};
//start advertising
advertiser.startAdvertisingSet(parameters, data, null, null, null, callback);
//change the advertising packet
currentAdvertisingSet[0].setAdvertisingData(new AdvertiseData.Builder().setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build());
Но когда я пытаюсь назначить новые рекламные данные последней строкой, я получаю
Attempt to invoke virtual method 'void android.bluetooth.le.AdvertisingSet.setAdvertisingData(android.bluetooth.le.AdvertiseData)' on a null object reference
ошибка и приложение закрыто с обоими значениями setLegacyMode true и false. Но я уже назначил AdvertisingSet в функции public void onAdvertisingSetStarted. Что мне здесь нужно делать?