I'm trying to detect beacons on Android 12 beta 5 with Nearby Messages API. I did everything as it was described in the doc. My application is used for detecting beacons only. And on versions bellow android 12 everything works properly, but with android 12 I have hard time. I have checked all permissions. Also I can see my application in Nearby devices list of applications. I can scan beacons within 3rd party app on same phone. Maybe someone else also faced this issue and can help me.
Here is my code:
Manifest -
<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" />
....
Where nearby_api_key is taken from Google Play Console.
Activity class related code:
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")
}
}
In build.gradle
implementation 'com.google.android.gms:play-services-nearby:18.0.0'
Many thanks in advance!
Maybe I will save some time to somebody, so:
According to https://developers.google.com/nearby/messages/android/get-beacon-messages
The Google beacon platform is deprecated as of December 7, 2020. The platform will shut down on April 1, 2021. After this shut down, beacons will no longer be supported within the Nearby Messages API.
Related ticket https://issuetracker.google.com/issues/202977209