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

dav*_*ung 20 android bluetooth-lowenergy ibeacon ibeacon-android

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

((BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE))
    .getAdapter().getBluetoothLeAdvertiser()
Run Code Online (Sandbox Code Playgroud)

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

((BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE))
    .getAdapter().isMultipleAdvertisementSupported()
Run Code Online (Sandbox Code Playgroud)

总是返回false

第一种用于在6月份返回Nexus 5的第一个Android L预览版本上的有效对象的方法.闪烁最新更新后,它不再这样做了.

有人看到了吗?

编辑:至少有一个人在此处复制了此问题,他在此处向Google提出了一个问题:https://code.google.com/p/android-developer-preview/issues/detail?id = 1570

dav*_*ung 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


dav*_*ung 6

这不是一个完整的解决方案,而是由mattprec在Google Code上发布的拟议解决方案.它允许您BluetoothLeAdvertiser通过调用私有构造函数而不是使用公共API 来获取实例.遗憾的是,有关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;
  }
}
Run Code Online (Sandbox Code Playgroud)