即使调用停止广告方法,Android BLE 广告器也会传输残留数据

问题描述

如果应用程序在前台,我可以停止广告并调用 advertiser.stopAdvertising() 方法。当我的屏幕关闭时出现问题。

我有两台设备,一台使用 BLE 广告器发送数据的 Android 设备和一台正在监听的设备。我的目标是 API 21,以更改广告商发送的数据,我停止广告商,然后使用新数据重新启动它。为了安全起见,处理程序会在一段时间后自行停止广告客户。

public class BluetoothManager extends Application
{
    private static BluetoothManager Instance;

    private BluetoothAdapter adapter;
    private android.bluetooth.BluetoothManager manager;

    private AdvertiseCallback AdvertiseCallbackCalibration = new AdvertiseCallback(){};
    private Handler handlerCalibration= new Handler();
    private BluetoothLeAdvertiser BLEAdvertiserCalibration;

    public static BluetoothManager getApplication()
    {
        return Instance;
    }

    public void onCreate()
    {
        super.onCreate();

        EnableBluetooth();
        Instance = this;
    }

    void CreateBLEAdvertiser()
    {
        if (adapter == null)
        {
            manager = (android.bluetooth.BluetoothManager) getSystemService(Context.BLUetoOTH_SERVICE);
            adapter = manager.getAdapter();
        }

        if (adapter.isMultipleAdvertisementSupported() && adapter.isEnabled())
        {
            if(BLEAdvertiserCalibration == null)
            {
                BLEAdvertiserCalibration = adapter.getBluetoothLeAdvertiser();
            }
        }
        else
        {
            EnableBluetooth();
        }
    }

    void SendData(byte[] data)
    {
        if (adapter.isEnabled())
        {
            AdvertiseSettings settings = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY).setConnectable(false).setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH).build();
            AdvertiseData data = new AdvertiseData.Builder().addManufacturerData(2200,data).build();

            if(BLEAdvertiserCalibration != null)
            {
                BLEAdvertiserCalibration.stopAdvertising(AdvertiseCallbackCalibration);
                handlerCalibration.removeCallbacksAndMessages(null);
            }

            BLEAdvertiserCalibration.startAdvertising(settings,data,AdvertiseCallbackCalibration);
            handlerCalibration.postDelayed(() -> BLEAdvertiserCalibration.stopAdvertising(AdvertiseCallbackCalibration),3000);
        }
        else
        {
            EnableBluetooth();
        }
    }

    private void EnableBluetooth()
    {
        Intent intentBluetooth = new Intent();
        intentBluetooth.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        this.startActivity(intentBluetooth);
    }
}

这个类是一个Singleton,它扩展了Application,因为多个Activity可以发送数据。这允许确保无论上下文如何调用和停止的广告商和回调都是相同的。

它按预期工作。在输入期间,Android 发送数据,第二个设备接收数据。传输自行或手动停止,Android不再传输数据,第二台设备不再接收数据。

问题:在多次使用后,如果手机屏幕自行关闭(如果用户手动关闭屏幕不会发生这种情况),数据会被第二个设备接收,而 Android 没有调用 SendData()。当然,屏幕关闭时不会有意发送任何数据。 接收到的数据对应于先前传输的数据,但顺序有些随机

有什么想法吗?就像蓝牙芯片清除残留数据...

编辑

Android 发送的数据示例。我确定第二个设备接收到的数据来自Android,因为接收到的字节数组是匹配的。

static byte[] Calibration(byte statut)
{
    byte[] packet = new byte[3];
    try
    {
        byte[] buffer;
        int i,size,limit;
        i = 0;

        // Type bluetooth packet
        size = 2;
        limit = i + size;// char
        buffer = ByteBuffer.allocate(size).putChar('C').array();
        for (int j = size - 1; i < limit; i++,j--)
        {
            packet[i] = buffer[j];
        }

        // State
        size = 1;
        limit = i + size;// bool
        buffer = ByteBuffer.allocate(size).put(statut).array();
        for (int j = size - 1; i < limit; i++,j--)
        {
            packet[i] = buffer[j];
        }
    }
    catch (NumberFormatException e)
    {
        packet = new byte[3];
    }
    return packet;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)