BluetoothLeAdvertiser可以在Android 5.0的Nexus 5上运行吗?

时间:2014-10-18 16:08:19

标签: android bluetooth-lowenergy ibeacon ibeacon-android

将我的Nexus 5刷新到Android 5.0预览版本hammerhead-lpx13d后,操作系统报告它不再支持蓝牙LE广告。如果你打电话:

((BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE))
    .getAdapter().getBluetoothLeAdvertiser()

始终返回null。另外,新方法:

((BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE))
    .getAdapter().isMultipleAdvertisementSupported()

始终返回false

第一种用于在6月份返回Nexus 5的第一个Android L预览版本上的有效对象的方法。在刷新最新更新后,它不再发生。

有人看到了吗?

编辑:至少有一位人员在此处复制了此问题:https://code.google.com/p/android-developer-preview/issues/detail?id=1570

4 个答案:

答案 0 :(得分:21)

不幸的是,谷歌的官方回答是不,Nexus 5不再支持广告。

  

我们在Android 5.0 Lollipop中引入了BLE外设模式。 Nexus 6和   Nexus 9是支持BLE的前两个生产Nexus设备   外围模式。由于硬件芯片组依赖,旧的Nexus   设备(4/5/7)无法访问Lollipop上的功能。

请参阅danielho在第1570期的评论#52 ... @ google.com:BLE广告模式无效 https://code.google.com/p/android-developer-preview/issues/detail?id=1570

也就是说,我已经确认Nexus 9平板电脑支持广告。有关详细信息,请参阅此处:http://developer.radiusnetworks.com/2014/11/18/beacon-transmission-with-android-5.html

答案 1 :(得分:6)

这不是一个完整的解决方案,而是一个提议的解决方案posted by mattprec on Google Code。它允许您通过调用私有构造函数而不是使用公共API来获取BluetoothLeAdvertiser实例。遗憾的是,有关Nexus 5和Nexus 7 2013版测试的报告称,即使您获得实例,也无法使用该对象来制作广告。此外,请注意,即使您可以使用它,它也可能会破坏Android的任何次要代码版本,因为它使用的是非公共API。

对于记录,这里是从该页面复制的代码段:

private static BluetoothLeAdvertiser getAdvertiserHack(BluetoothAdapter adapter) {
  try {
    Class<? extends BluetoothAdapter> adapterClass = adapter.getClass();
    Field advertiserField = adapterClass.getDeclaredField("sBluetoothLeAdvertiser");
    advertiserField.setAccessible(true);
    Object advertiser = advertiserField.get(adapter);
    if (advertiser == null) {
      Field bluetoothManagerServiceField = adapterClass.getDeclaredField("mManagerService");
      bluetoothManagerServiceField.setAccessible(true);
      Object bluetoothManagerService = bluetoothManagerServiceField.get(adapter);

      Constructor<?> constructor = BluetoothLeAdvertiser.class.getDeclaredConstructor(
          bluetoothManagerServiceField.getType());
      constructor.setAccessible(true);
      advertiser = constructor.newInstance(bluetoothManagerService);

      advertiserField.set(adapter, advertiser);
    }
    return (BluetoothLeAdvertiser) advertiser;
  } catch (Exception e) {
    return null;
  }
}

答案 2 :(得分:1)

更新: 相关问题直到android-beacon-library / BLE Android SDK。是否有可能 - 不调用startAdvertising方法 - 检查是否有在后台运行的广告服务?

更新:

录制到:https://code.google.com/p/android-developer-preview/issues/detail?id=1570#c52

现在只有Nexus 6和Nexus 9支持Android 5.0中的BLE Peripheal模式

更新: 我使用的是Nexus 5 Android 5.0内部版本号LPX13D

根据此https://stackoverflow.com/a/26611779/1906420

在实施您的解决方法后,bluetoothAdvertiser不为空。从bluetoothAdvertiser调用startAdvertising

bluetoothAdvertiser.startAdvertising(settingsBuilder.build(), dataBuilder.build(), advertiseCallback);

其中

private AdvertiseCallback advertiseCallback = new AdvertiseCallback() {

        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffec) {

        }

        @Override
        public void onStartFailure(int result) {
            if (result == ADVERTISE_FAILED_DATA_TOO_LARGE) {
                Log.d(TAG, "Failed to start advertising as the advertise data to be broadcasted is larger than 31 bytes.");
            }
            else if(result == ADVERTISE_FAILED_TOO_MANY_ADVERTISERS){
                Log.d(TAG, "Failed to start advertising because no advertising instance is available.");
            }
            else if(result == ADVERTISE_FAILED_ALREADY_STARTED){
                 Log.d(TAG, "Failed to start advertising as the advertising is already started.");
            }
            else if(result == ADVERTISE_FAILED_INTERNAL_ERROR){
                Log.d(TAG, "Operation failed due to an internal error.");
            }
            else if(result == ADVERTISE_FAILED_FEATURE_UNSUPPORTED){
                Log.d(TAG, "This feature is not supported on this platform.");
            }
            else {
                Log.d(TAG, "There was unknown error.");
            }

        }

    };

始终使用错误代码5(ADVERTISE_FAILED_FEATURE_UNSUPPORTED)给出onStartFailure回调

答案 3 :(得分:1)

  

尽管如此,我已经确认广告是由Nexus支持的   9片。详情请见此处:   http://developer.radiusnetworks.com/2014/11/18/beacon-transmission-with-android-5.html

QuickBeacon应用程序在Nexus 9上正常运行。在app中有一个Beacon Format选项。@ davidgyoung你能为BeaconParser提供精确的字符串,使这个库以iBeacon格式传输吗?