I am trying to scan for beacons using startScan(filters, settings, callbackIntent). I have an implementation that works fine for Sony Xperia XZ, and Nexus 5X. The only other device with Android O I have available is a Samsung Galaxy S8, and what works for the other devices produce nothing on the Samsung. (The bluetooth scan is really imbedded in a library module, but even when creating a dummy app the samsung fails, so I'll use that in this example). I have removed the filter and the settings used for startScan
since the scan doesn't work without them anyway and these are optional.
MainActivity
- checks and asks for permissions (ACCESS_COARSE_LOCATION)
- simplified onStart
override fun onStart() {
super.onStart()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val startScan = bleScanner.startScan(null, null, getPendingIntent())
Log.d("testApp", "Start scan! ${startScan == 0}")
}
}
PendingIntent:
private fun getPendingIntent(): PendingIntent {
return PendingIntent.getBroadcast(
this, REQ_CODE,
Intent(this.applicationContext, BleReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT)
}
Manifest
Permissions:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Receiver:
<receiver android:name="com.testapp.samsungoscan.BleReceiver" >
<intent-filter>
<action android:name="BluetoothDevice.ACTION_FOUND" />
<action android:name="BluetoothDevice.EXTRA_UUID" />
<action android:name="BluetoothDevice.EXTRA_RSSI" />
</intent-filter>
</receiver>
Receiver implementation:
class BleReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.e("testApp", "On Receive!")
}
}
So! Why is this not working for Samsung, while it works for Sony and Nexus?
Note: If I change the receivers android:name
to a relative path .BleReceiver
instead of com.testapp.samsungoscan.BleReceiver
, then the Nexus stops working, but Sony still works!
By work I mean all classes gets used and the logs are triggered. What is wrong?