Does BluetoothLeAdvertiser work on a Nexus 5 with Android 5.0?

前端 未结 4 851
一向
一向 2020-12-05 00:57

After flashing my Nexus 5 to the Android 5.0 preview release hammerhead-lpx13d, the OS reports that it no longer supports Bluetooth LE advertising. If you call:

<         


        
4条回答
  •  無奈伤痛
    2020-12-05 01:17

    This is not full a solution, but a proposed work-around posted by mattprec on Google Code. It allows you to get a BluetoothLeAdvertiser instance by calling the private constructor rather than using the public API. Unfortunately, reports of testing on a Nexus 5 and a Nexus 7 2013 edition say that even after you get an instance you can't use the object to make advertisements come out. Also, be warned that even if you can get it to work, it might break on any minor code release of Android because it is using a non-public API.

    For the record, here's the code snippet copied from that page:

    private static BluetoothLeAdvertiser getAdvertiserHack(BluetoothAdapter adapter) {
      try {
        Class 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;
      }
    }
    

提交回复
热议问题