如何设置BLE广告包的设备名称字段 [英] How to set BLE advertisement packet's device name field

查看:418
本文介绍了如何设置BLE广告包的设备名称字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用API​​来构建广告包.我将true传递给setIncludeDeviceName

I use the API to the build an advertisement packet. I pass true to setIncludeDeviceName

    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .setIncludeTxPowerLevel(false)
            .addServiceUuid(new ParcelUuid(TimeProfile.TIME_SERVICE))
            .build();

API对广告包中的设备型号进行编码.对于我的应用程序,广告数据包的设备名称应来自应用程序中硬编码的字符串

The API encodes the device model number in the advertising packet. For my app however, the device name for the advertisement packet should be come from a string hard coded into the app

private static final String DEVICE_NAME = "My_Device_Name";

有什么方法可以自定义广告包中的设备名称?我在 AdvertiseData AdvertiseData.Builder

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 object

AdvertiseData scanResponse = new AdvertiseData.Builder()
        .setIncludeDeviceName(true)
        .build();

然后也使用scanResponse开始广告

Then start advertising using the scanResponse as well

bluetoothAdapter.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广告包的设备名称字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆