我是蓝牙低功耗广告的新手。 BLE支持API级别28中的动态播发。您可以播发不同的数据包。我正在尝试执行此操作,但是在广告集中获取了状态代码18和Null。 谁能告诉我如何在android中做动态广告。我的设备是带有Android 8.1.0的Nexus 6。
这是我的代码:
//setting parameters
AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder())
.setLegacyMode(false)
.setConnectable(true)
.setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
.setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
.build();
//Adding advertising data
AdvertiseData.Builder mbuilder = new AdvertiseData.Builder();
mbuilder.setIncludeDeviceName(false);
mbuilder.setIncludeTxPowerLevel(false);
mbuilder.addManufacturerData(Integer.parseInt(manuf, 16), manufacturerData1);
AdvertiseData mData = mbuilder.build();
//Adding scan response data
AdvertiseData scanResponse = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.build();
//Advertising set call back and I am receiving null in Advertising set and status code 18
AdvertisingSetCallback callback = new AdvertisingSetCallback() {
@Override
public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
Log.i("", "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
+ status);
if (status==AdvertisingSetCallback.ADVERTISE_FAILED_ALREADY_STARTED)
Toast.makeText(context, "ADVERTISE_FAILED_ALREADY_STARTED", Toast.LENGTH_SHORT).show();
else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED)
Toast.makeText(context, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED", Toast.LENGTH_SHORT).show();
else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_DATA_TOO_LARGE)
Toast.makeText(context, "ADVERTISE_FAILED_DATA_TOO_LARGE", Toast.LENGTH_SHORT).show();
else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_INTERNAL_ERROR)
Toast.makeText(context, "ADVERTISE_FAILED_INTERNAL_ERROR", Toast.LENGTH_SHORT).show();
else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS)
Toast.makeText(context, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS", Toast.LENGTH_SHORT).show();
else if (status==AdvertisingSetCallback.ADVERTISE_SUCCESS)
Toast.makeText(context, "ADVERTISE_SUCCESS", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
Log.i("", "onAdvertisingSetStopped():");
}
};
//starting advertisement
mBluetoothLeAdvertiser.startAdvertisingSet(parameters, mData, scanResponse, null, null, callback);
答案 0 :(得分:0)
您真的需要“ scanResponse”吗?我的设备具有BLE 5.0和Android 9.0。如果我传递“ null”而不是“ scanResponse”,那么当前对象和正确的状态将返回到回调函数。但是,如果我通过“ scanResponse”,则会得到“空”对象和“状态” 18。
如果更改
mBluetoothLeAdvertiser.startAdvertisingSet(parameters, mData, scanResponse, null, null, callback);
在
mBluetoothLeAdvertiser.startAdvertisingSet(parameters, mData, null, null, null, callback);
对我来说一切正常,我可以从其他设备上的智能手机扫描BLE 5信号。