0

我正在尝试使用 Nearby Messages API 在 Android 12 beta 5 上检测信标。我按照文档中的描述做了所有事情。我的应用程序仅用于检测信标。在低于 android 12 的版本上,一切正常,但使用 android 12 我很难。我已经检查了所有权限。我也可以在附近的设备应用程序列表中看到我的应用程序。我可以在同一部手机上的 3rd 方应用程序中扫描信标。也许其他人也面临这个问题,可以帮助我。

这是我的代码:

显现 -

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

…………

    <meta-data
        android:name="com.google.android.nearby.messages.API_KEY"
        android:value="@string/nearby_api_key" />

……

near_api_key 取自 Google Play 控制台。

Activity类相关代码:

     fun getMessagesClient(): MessagesClient {
            if (messageClient == null) {
                messageClient = Nearby.getMessagesClient(this, MessagesOptions.Builder()
                        .setPermissions(NearbyPermissions.BLE)
                        .build())
            }
            return messageClient!!
        }
    
 val statusCallback = object : StatusCallback() {
        override fun onPermissionChanged(p0: Boolean) {
            super.onPermissionChanged(p0)
            //all permissions are given
            startScan()
          }
    }


    private fun getSubscribeOptions(): SubscribeOptions {
        val messageFilter = MessageFilter.Builder()
                .includeIBeaconIds(Identifiers.beaconType1Uuid, null, null)
                .includeIBeaconIds(Identifiers.beaconType2Uuid, null, null)
                .includeAllMyTypes()
                .build()

        val options = SubscribeOptions.Builder()
                .setStrategy(Strategy.BLE_ONLY)
                .setFilter(messageFilter)
                .setCallback(object : SubscribeCallback() {
                    override fun onExpired() {
                        Log.v(TAG, "options  onExpired")
                    }
                })
                .build()
        return options
    }

    private fun startScan() {
        getMessagesClient()
                .subscribe(beaconMessageListener, getSubscribeOptions()) // Do NOT receive messages within this callback
                .addOnSuccessListener {
                    Log.d(TAG, "subscribe on Success")
                }
                .addOnFailureListener {
                    Log.d(TAG, "subscribe on Fail $it")
                }
    }

    val beaconMessageListener = object : MessageListener() {
        override fun onFound(message: Message) { // Is not called
            super.onFound(message)
            message?.let { 
                onFoundBeacon(it)
            }
        }

        override fun onLost(message: Message) {
            super.onLost(message)
            message?.let {
                onLostBeacon(it)
            }
        }

        override fun onDistanceChanged(message: Message, distance: Distance) {
            super.onDistanceChanged(message, distance)
            message?.let {
                onBeaconDistanceChanged(message, distance)
            }
        }

        override fun onBleSignalChanged(p0: Message, p1: BleSignal) {
            super.onBleSignalChanged(p0, p1)
            Log.i(LogTag.BEACON, "onBleSignalChanged $p0 ,\nisStarted() $isStarted\nBleSignal $p1")
        }
    }

在 build.gradle

implementation 'com.google.android.gms:play-services-nearby:18.0.0'

提前谢谢了!

4

1 回答 1

0

也许我会为某人节省一些时间,所以:

根据https://developers.google.com/nearby/messages/android/get-beacon-messages

Google 信标平台已于 2020 年 12 月 7 日弃用。该平台将于 2021 年 4 月 1 日关闭。关闭后,附近消息 API 将不再支持信标。

相关票证 https://issuetracker.google.com/issues/202977209

于 2021-11-15T13:17:49.467 回答