java.lang.NoSuchMethodError: android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser

java.lang.NoSuchMethodError: android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser

我想通过 Android 4.4 发送 Beacon。 (银河枢纽)

这里有帮助: enter link description here

在我的 android 应用中:

fun onClickTransmittingBeacon(view: View) {
        val result = BeaconTransmitter.checkTransmissionSupported(this)
        Log.d(TAG, "onClickTransmittingBeacon, result = $result")
        val beacon = Beacon.Builder()
                .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
                .setId2("1")
                .setId3("2")
                .setManufacturer(0x0118)
                .setTxPower(-59)
                .setDataFields(Arrays.asList(*arrayOf(0L)))
                .build()
        val beaconParser = BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")
        val beaconTransmitter = BeaconTransmitter(applicationContext, beaconParser)
        beaconTransmitter.startAdvertising(beacon)
}

在logcat中:

onClickTransmittingBeacon, result = 1

所以我的设备支持 BLE。不错

但为什么在 logcat 中出现运行时异常:

10-22 18:49:00.472 D/com.myproject.android.activity.RangingActivity(25202): onClickTransmittingBeacon, result = 1
10-22 18:49:00.472 D/BeaconParser(25202): Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
10-22 18:49:00.479 D/BluetoothManagerService(  378): Message: 20
10-22 18:49:00.479 D/BluetoothManagerService(  378): Added callback: android.bluetooth.IBluetoothManagerCallback$Stub$Proxy@42b207b8:true
10-22 18:49:00.487 D/AndroidRuntime(25202): Shutting down VM
10-22 18:49:00.487 W/dalvikvm(25202): threadid=1: thread exiting with uncaught exception (group=0x41879700)
10-22 18:49:00.644 D/dalvikvm(25202): GC_FOR_ALLOC freed 433K, 5% free 9589K/10060K, paused 19ms, total 19ms
10-22 18:49:00.737 E/AndroidRuntime(25202): FATAL EXCEPTION: main
10-22 18:49:00.737 E/AndroidRuntime(25202): java.lang.IllegalStateException: Could not execute method for android:onClick
10-22 18:49:00.737 E/AndroidRuntime(25202):     at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at android.view.View.performClick(View.java:4240)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at android.view.View$PerformClick.run(View.java:17721)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at android.os.Handler.handleCallback(Handler.java:730)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at android.os.Looper.loop(Looper.java:137)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at java.lang.reflect.Method.invokeNative(Native Method)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at java.lang.reflect.Method.invoke(Method.java:525)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at dalvik.system.NativeStart.main(Native Method)
10-22 18:49:00.737 E/AndroidRuntime(25202): Caused by: java.lang.reflect.InvocationTargetException
10-22 18:49:00.737 E/AndroidRuntime(25202):     at java.lang.reflect.Method.invokeNative(Native Method)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at java.lang.reflect.Method.invoke(Method.java:525)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
10-22 18:49:00.737 E/AndroidRuntime(25202):     ... 11 more
10-22 18:49:00.737 E/AndroidRuntime(25202): Caused by: java.lang.NoSuchMethodError: android.bluetooth.BluetoothAdapter.getBluetoothLeAdvertiser
10-22 18:49:00.737 E/AndroidRuntime(25202):     at org.altbeacon.beacon.BeaconTransmitter.<init>(BeaconTransmitter.java:58)
10-22 18:49:00.737 E/AndroidRuntime(25202):     at com.myproject.android.activity.RangingActivity.onClickTransmittingBeacon(RangingActivity.kt:140)
10-22 18:49:00.737 E/AndroidRuntime(25202):     ... 14 more
10-22 18:49:00.753 W/ActivityManager(  378):   Force finishing activity com.myproject.android.debug/com.myproject.android.activity.RangingActivity
10-22 18:49:00.886 D/dalvikvm(  378): GC_FOR_ALLOC freed 1494K, 32% free 21228K/31108K, paused 92ms, total 93ms
10-22 18:49:00.901 I/ActivityManager(  378): Process android.process.acore (pid 24435) has died.
10-22 18:49:01.175 D/dalvikvm(25202): GC_FOR_ALLOC freed 541K, 6% free 9557K/10136K, paused 51ms, total 52ms
10-22 18:49:01.401 W/ActivityManager(  378): Activity pause timeout for ActivityRecord{427f8038 u0 com.myproject.android.debug/com.myproject.android.activity.RangingActivity}
10-22 18:49:01.542 D/dalvikvm(25202): GC_FOR_ALLOC freed 227K, 6% free 9598K/10136K, paused 31ms, total 31ms
10-22 18:49:01.784 I/CrashlyticsCore(25202): Crashlytics report upload complete: 5DAF24D2036F-0001-6272-04D09418ED6A

Android 4.4 太旧了,不支持这个。您需要 Android 5.