Is there any way to customize the device name in the advertising packet? I don't see any way to do that in the docs for AdvertiseData or for AdvertiseData.Builder推荐答案您必须从 data AdvertiseData中删除"setIncludeDeviceName" 对象并定义扫描响应AdvertiseData对象You have to remove the "setIncludeDeviceName" from your data AdvertiseData object and define a scan response AdvertiseData objectAdvertiseData scanResponse = new AdvertiseData.Builder() .setIncludeDeviceName(true) .build();然后也使用scanResponse开始广告Then start advertising using the scanResponse as wellbluetoothAdapter.getBluetoothLeAdvertiser() .startAdvertising(advSettings, data, scanResponse, advCallback);完整示例:AdvertiseSettings advSettings = new AdvertiseSettings.Builder() .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED) .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM) .setConnectable(true) .build();AdvertiseData advData = new AdvertiseData.Builder() .setIncludeTxPowerLevel(true) .addServiceUuid(mCurrentServiceFragment.getServiceUUID()) .build();AdvertiseData advScanResponse = new AdvertiseData.Builder() .setIncludeDeviceName(true) .build();AdvertiseCallback advCallback = new AdvertiseCallback() { @Override public void onStartFailure(int errorCode) { super.onStartFailure(errorCode); Log.e(TAG, "Not broadcasting: " + errorCode); int statusText; switch (errorCode) { case ADVERTISE_FAILED_ALREADY_STARTED: Log.w(TAG, "ADVERTISE_FAILED_ALREADY_STARTED"); break; case ADVERTISE_FAILED_DATA_TOO_LARGE: Log.w(TAG, "ADVERTISE_FAILED_DATA_TOO_LARGE"); break; case ADVERTISE_FAILED_FEATURE_UNSUPPORTED: Log.w(TAG, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED"); break; case ADVERTISE_FAILED_INTERNAL_ERROR: Log.w(TAG, "ADVERTISE_FAILED_INTERNAL_ERROR"); break; case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS: Log.w(TAG, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS"); break; default: Log.wtf(TAG, "Unhandled error: " + errorCode); } } @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); Log.v(TAG, "Advertising started"); }};bluetoothAdapter.getBluetoothLeAdvertiser() .startAdvertising(advSettings, advData, advScanResponse, advCallback); 这篇关于如何设置BLE广告包的设备名称字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-13 00:14