找不到BLE广告

时间:2019-01-11 23:26:01

标签: android xamarin.android bluetooth-lowenergy

在我的Xamarin.Forms应用程序中,我正在编写本机Android代码来发布UUID和一些测试数据,以便另一台设备进行扫描和查找。我正在平板电脑SM-T580上运行该应用程序。这是我的代码:

var parameters = (new AdvertiseSettings.Builder())
                         .SetAdvertiseMode(AdvertiseMode.Balanced)
                         .SetConnectable(false)
                         .SetTimeout(0)
                         .SetTxPowerLevel(AdvertiseTx.PowerMedium).Build();

AdvertiseData data = (new AdvertiseData.Builder()).AddServiceData(_applicationParcelUuid,
                                                                          System.Text.Encoding.ASCII.GetBytes("helloTest123"))
                                                  .Build();

if (BluetoothAdapter.DefaultAdapter.BluetoothLeAdvertiser != null)
{
 BluetoothAdapter.DefaultAdapter.BluetoothLeAdvertiser.StartAdvertising(parameters, data, _customAvertiseCallback);
}

在我的_customAvertiseCallback中,调用了OnStartSuccess覆盖,因此我认为它应该可以工作。我尝试在Nexus 5设备上使用nRF Connect应用程序对其进行测试,但我从未在其上看到设备的广告。

编辑

我尝试添加扫描BLE广告的代码,以查看是否可以自己捡起它。它会从其他设备找到其他BLE广告,但从我的应用程序找不到。这是我的扫描代码,它在同一应用程序上运行:

public void StartBluetoothScanning()
{
    if(_bleScanningInitialized)
    {
        return;
    }

    /**
        * Initialize BluetoothAdapter
        * Check the device has the hardware feature BLE
        * Then enable the hardware,
    */
    BluetoothManager bluetoothManager = (BluetoothManager)GetSystemService(Java.Lang.Class.FromType(typeof(BluetoothManager)));
    var bluetoothAdapter = bluetoothManager.Adapter;
    bool isBleSupported = bluetoothAdapter != null && this.BaseContext.PackageManager.HasSystemFeature(PackageManager.FeatureBluetoothLe);

    if(!isBleSupported)
    {
        return;
    }

    /**
        * Register GATT update receiver
    */

    this.RegisterReceiver(_gattUpdateReceiver, new IntentFilter(BluetoothAdapter.ActionStateChanged));

    /**
    * Initialize Bluetooth service.
    */

    Intent gattServiceIntent = new Intent(BaseContext, typeof(BluetoothGattService));
    var componentName = BaseContext.StartService(gattServiceIntent);
    bool isBind = BaseContext.BindService(gattServiceIntent, _serviceConnection, Bind.AutoCreate);

    /**
    * Prepare for scanning
    */

    List<ScanFilter> scanFilters = new List<ScanFilter>();
    ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
    //scanFilterBuilder.SetServiceUuid(_applicationParcelUuid);
        scanFilters.Add(scanFilterBuilder.Build());

    ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
        scanSettingsBuilder.SetScanMode(Android.Bluetooth.LE.ScanMode.Balanced);

    if (!bluetoothAdapter.IsEnabled)
    {
        bluetoothAdapter.Enable();
    }

    if (BluetoothAdapter.DefaultAdapter.BluetoothLeScanner != null)
    {
        BluetoothAdapter.DefaultAdapter.BluetoothLeScanner.StartScan(scanFilters, scanSettingsBuilder.Build(), _customScanCallback);

        _bleScanningInitialized = true;
    }
    else
    {
        if (ContextCompat.CheckSelfPermission(ApplicationContext,
            Android.Manifest.Permission.AccessFineLocation) != Permission.Granted)
        {

        }
    }
}

public class CustomScanCallback : ScanCallback
{
    public override void OnScanResult([GeneratedEnum] ScanCallbackType callbackType, ScanResult result)
    {
        base.OnScanResult(callbackType, result);

        if (result.ScanRecord.ServiceUuids != null)
        {
            foreach (var x in result.ScanRecord.ServiceUuids)
            {
                var id = x.Uuid.ToString();

                // never true
                if(id == _applicationGuid)
                {
                    // Found my advertisement,
                }
            }
        }
    }

    public override void OnScanFailed([GeneratedEnum] ScanFailure errorCode)
    {
        // never gets called
        base.OnScanFailed(errorCode);
    }
}

1 个答案:

答案 0 :(得分:4)

最后发现了问题:AddServiceData应该是AddServiceUuid,所以我可以在扫描回调的result.ScanRecord.ServiceUuids中找到它。此外,对于任何其他不知道的人,蓝牙5.0需要Android 8.0,蓝牙5.0的广告数据容量有所增加:

https://source.android.com/devices/bluetooth/ble_advertising

您的设备还需要支持蓝牙5.0。我的SM-T580没有。

使用较旧版本的蓝牙,您播发的UUID不能超过1个,或者用户最大有效载荷为29个字节。