I need to check if the device is connected to any device via Bluetooth.
This is my activity where I launch activity for Bluetooth settings. ``
receiver = BluetoothModeReceiver()
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED).also {
registerReceiver(receiver, it)
val settingsIntent = Intent(Settings.ACTION_BLUETOOTH_SETTINGS)
startActivity(settingsIntent)
}
And this is the broadcast receiver:
class BluetoothModeReceiver :BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val isDeviceConnected = intent?.getBooleanExtra("state",false) ?: return
if(isDeviceConnected){
Log.i("Broadcast", "Happened ")
}
}
}
I also tried to use intents like BluetoothDevice.ACTION_ACL_CONNECTED and ACTION_ACL_DISCONNECTED but none of them work. When I connect the phone to any device that log in receiver doesn't get printed in the logcat. Any ideas why it doesn't work? They would be appreciated.
Related
We are currently using a zebra device for company asset management so we are developing a small prototype android app to scan RFID tags. I've read from the data wedge API that the app can get scanned output has an intent broadcast.
But the app is unable to receive any intents.
Device : Zebra MC33
Data wedge version : 7.3
I've tried using the following
Profile Settings:
Intent Action : my.prototype.app.SCAN
Intent Delivery Type: Broadcast Intent.
Intent Category: Default.
Added to Associated Apps
AndroidManifest.xml
<receiver
android:name=".ScanIntentReceiver"
android:enabled="true"
android:exported="true" />
ScanIntentReceiver.kt
abstract class ScanIntentReceiver : BroadcastReceiver() {
abstract fun onReceiveScan(data: ScannerOutput)
override fun onReceive(p0: Context?, p1: Intent?) {
Timber.d("S1: Broadcast Scan Intent Received.")
p0?.let { context ->
p1?.let { intent ->
when (intent.action) {
BuildConfig.intentAction -> {
try {
val data = parseData(intent, context)
Timber.d("Data received: $data")
onReceiveScan(data)
} catch (ex: Exception) {
Timber.d("Parsing error")
Timber.d(ex)
}
}
else -> {
Timber.d("No Suitable Action.")
}
}
}
}
}
}
Also tried using the "Send via Start Activity"
Profile Settings:
Intent Action : my.prototype.app.SCAN
Intent Delivery Type: Send via StartActivity.
Intent Category: Default.
Added to Associated Apps
AndroidManifest.xml
<activity
android:name=".activity.ScanActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="${intentAction}" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
ScanActivity.kt
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Timber.d("Received Intent via Activity.")
intent?.let {
try {
val data = parseData(it, this)
viewModel.processOutput(data)
} catch (ex: Exception) {
Timber.e(ex)
}
}
}
Any help is appreciated. Thanks in advance.
UPDATE:
private fun parseData(intent: Intent, ctx: Context): ScannerOutput {
val decodedSource =
intent.getStringExtra(ctx.getString(R.string.datawedge_intent_key_source))
val decodedData =
intent.getStringExtra(ctx.getString(R.string.datawedge_intent_key_data))
val decodedLabelType =
intent.getStringExtra(ctx.getString(R.string.datawedge_intent_key_label_type))
....
}
UPDATE:
val filter = IntentFilter()
filter.addCategory(Intent.CATEGORY_DEFAULT)
filter.addAction(BuildConfig.intentAction)
registerReceiver(scanIntentReceiver, filter)
Let's clarify things a bit. If you want to read RFID tags using the MC33R, then you must use Zebra RFID API3, not intents. Zebra is considering using the intents also for RFID, but at the moment the best option is to use the SDK, not the intent Broadcaster/Receiver. If you intend to use the barcode scanner, then the official (new) way of doing it is through intents. To get the intents you must configure a profile in the Data Wedge, you must activate intent broadcasing and specify the intent action in the profile, if you do so, you'll receive the intent. Look for the following settings in the data Wedge profile (default profile is good):
Intent Output = ON
Intent action = my.prototype.app.SCAN
Intent distribution (or delivery): Broadcast
I can assure you that these settings will work for the barcode scanner, but in case you want to use the RFID antenna, download the API3 SDK from Zebra Developer site and follow the examples.
***UPDATE
val filter = IntentFilter()
filter.addCategory(Intent.CATEGORY_DEFAULT)
filter.addAction("my.prototype.app.SCAN")//here set the action (same as in DataWedge app)
this.requireContext().registerReceiver(this, filter)
Implement
BroadcastReceiver
and add:
override fun onReceive(context: Context?, intent: Intent?) {
//Receives readings from barcode scanner
val action = intent!!.action
if (action == "my.prototype.app.SCAN") {
val decodedData = intent.getStringExtra("com.symbol.datawedge.data_string")
if (decodedData != null) {
//decodedData is your barcode
}
}
}
Is there a way to make a customView for the battery status?
Like i'm trying to make a kiosk app so the status bar is hided and i'm showing the wi-fi status with the following library, is there a way to make something for the battery or is there yet an existing library for it?
Just register broadcast receiver and get what you need in onReceive
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val battery = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
}
//todo
}
registerReceiver(receiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
I've got a bit of my app that is dedicated to sharing files between devices over bluetooth using a quick, ad-hoc protocol that I put together. Currently, in the containing Activity I begin discovery, and add any device that I find into a RecyclerView. Here is the code for the BroadcastReceiver that is handling that:
private val scanReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == BluetoothDevice.ACTION_FOUND) {
val dev = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
Log.d(TAG, "Got device ${dev.name} with address ${dev.address}")
if (dev.name != null) {
Log.d(TAG, "Found nonnull device name, adding")
if (!viewAdapter.dataset.any { it.name == dev.name }) {
viewAdapter.dataset.add(dev)
viewAdapter.notifyDataSetChanged()
}
}
}
}
}
I wanted to modify this in such a way that it would only add devices who were broadcasting with the service UUID that I set up in the server portion of the app. After doing some research I came to this method that I could use to get the UUIDs of the services on the device. I integrated that into my BroadcastReceiver as such
private val scanReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
val dev = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
Log.d(TAG, "Got device ${dev.name} with address ${dev.address}")
if (dev.name != null) {
dev.fetchUuidsWithSdp()
}
}
//TODO: Untested code
BluetoothDevice.ACTION_UUID -> {
val id = intent.getParcelableExtra<ParcelUuid>(BluetoothDevice.EXTRA_UUID)
if (id.uuid == ShareServerSocket.SERVICE_UUID) {
val dev = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
if (!viewAdapter.dataset.any { it.name == dev.name }) {
viewAdapter.dataset.add(dev)
viewAdapter.notifyDataSetChanged()
}
}
}
}
}
}
(With requisite modifications to the IntentFilter I'm registering it with).
The code in the new branch gets called, I validated that with some debugging output. However, the ParcelUuid[] that I am given never contains the UUID of my service, and the device therefore never gets added. If I keep the entire setup the same on the device acting as a server, and bypass the new check on the client, I am able to connect and interact just fine. I'm unsure as to why my service wouldn't be being shown at this point.
P.S. I did also check the SDP cache, my service UUID is not there, either.
It turns out I was running into the same issue as described in Strange UUID reversal from fetchUuidsWithSdp. Stealing that workaround made it work.
I am new to Kotlin, and it seems awesome! Though today, I've been trying to do something that in Java was super simple, but I've got totally stuck.
I am using a broadcast receiver to determine when the device is connected/ disconnected from a power source. And all I need to do it update my UI accordingly.
My Code
Here's my BroadcastReceiver classs, and it seems to work fine.
class PlugInReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action == Intent.ACTION_POWER_CONNECTED) {
// Do stuff when power connected
}
else if (action == Intent.ACTION_POWER_DISCONNECTED) {
// Do more stuff when power disconnected
}
}
}
Now in my MainActivity (but somewhere else, later on), I want to update my UI when the intent is fired, for example in the function below, the background color changes.
private fun updateBackgroundColor( newBgColorId: Int = R.color.colorAccent){
val mainLayout = findViewById<View>(R.id.mainLayout)
val colorFade = ObjectAnimator.ofObject(
mainLayout, "backgroundColor", ArgbEvaluator(), oldBgColor, newBgColor)
colorFade.start()
}
The Question
How can I call a function on the MainActivity, or update my UI when the BroadcastReceiver fires an event?
What I've tried so far
I looked into having a static variable somewhere, storing the result of the BroadcastReciever, then an observable in my UI class, watching and calling appropriate function accordingly. Though after Googling how to do this, looks like that's not really a good approach in Kotlin.
Considered trying to run the BroadcastReciever on the UI thread, but that sounds like a terrible idea.
Tried mixing a Java implementation with my Kotlin class, but couldn't get it to work.
Frustratingly I found several very similar questions on SO. However their implementations seem to all use Java-specific features:
Android BroadcastReceiver onReceive Update TextView in MainActivity
How to update UI in a BroadcastReceiver
Calling a Activity method from BroadcastReceiver in Android
How to update UI from BroadcastReceiver after screenshot
I'm sure this is a trivial question for most Android developers, but I am lost! Let me know if you need any more details. Thanks very much in advance!
Sharing the info to register BroadcastReceiver in Kotlin
Step 1. Create BroadcastReceiver in MainActivity.kt
private val mPlugInReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
Intent.ACTION_POWER_CONNECTED -> {
//update your main background color
updateBackgroundColor()
}
Intent.ACTION_POWER_DISCONNECTED -> {
//update your main background color
updateBackgroundColor()
}
}
}
}
Step 2. Create IntentFilter
private fun getIntentFilter(): IntentFilter {
val iFilter = IntentFilter()
iFilter.addAction(Intent.ACTION_POWER_CONNECTED)
iFilter.addAction(Intent.ACTION_POWER_DISCONNECTED)
return iFilter
}
Step 3. Register receiver at onStart()
override fun onStart() {
super.onStart()
registerReceiver(mPlugInReceiver, getIntentFilter())
}
Step 4. Unregister receiver at onStop()
override fun onStop() {
super.onStop()
unregisterReceiver(mPlugInReceiver)
}
If you have custom BroadcastReceiver, you can register using LocalBroadcastManager and create your local IntentFilter
private val mLocalBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
AnyService.UPDATE_ANY -> {
}
}
}
}
private fun getLocalIntentFilter(): IntentFilter {
val iFilter = IntentFilter()
iFilter.addAction(AnyService.UPDATE_ANY)
return iFilter
}
Register local receiver
LocalBroadcastManager.getInstance(applicationContext).registerReceiver(mLocalBroadcastReceiver, getLocalIntentFilter())
Unregister local receiver LocalBroadcastManager.getInstance(applicationContext).unregisterReceiver(mLocalBroadcastReceiver)
The best way to achieve that is to create an abstract method in the BroadcastReceiver, and when onReceive() method is called, you can invoke that method that will be implemented by your activity.
BroadcastReceiver example:
abstract class ConnectionBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
//Do the checks or whatever you want
var isConnected = true
broadcastResult(isConnected)
}
protected abstract fun broadcastResult(connected: Boolean)
}
And the code in your activity (in the onCreate or onStart for example). Here you register the broadcast receiver with the method implementation, and here you can update the UI:
var connectionBroadcastReceiver = object : ConnectionBroadcastReceiver() {
override fun broadcastResult(connected: Boolean) {
if(isConnected){
refreshList()
}
}
}
val intentFilter = IntentFilter()
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
this.registerReceiver(connectionBroadcastReceiver, intentFilter)
Don't forget to unregister the receiver in (onPause||onStop||onDestroy), but it's not strictly necessary.
The onReceive(...) method runs on the main thread. You can register your Activity in onStart() and unregister it in onStop(), which will guarantee that your UI is present when the event is received.
I am registering BroadcastReceiver in application class and its register method is getting called and completed successfully without issue during application start. I am sending a broadcast from the module. I can print the action, it is there. But the app is not catching the broadcast. I don't know where I am doing wrong.
Below is the method to register receiver.
fun registerRecordingDataReceiver () {
info("AppReceiver register called=>${CommonsDataVars.TEMPERATURE.action()}")
val temperatureCompleted = CommonsDataVars.TEMPERATURE.action()
val temperatureCompletedIntentFilter = IntentFilter(temperatureCompleted)
val broadcastManager = LocalBroadcastManager.getInstance(this)
broadcastManager.registerReceiver(appReceiver,temperatureCompletedIntentFilter)
info("AppREceiver register completed")
}
Part of Broadcast Receiver
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.getAction()
debug("Action in App Receiver =>"+action)
if (action == CommonsDataVars.TEMPERATURE.action()) {
info("AppReceiver****************************************")
sendBroadcast part:
info("TEMPERATURE register called=>${CommonsDataVars.TEMPERATURE.action()}")
val intent = Intent()
intent.setAction(CommonsDataVars.TEMPERATURE.action())
//intent.putExtra(CommonsDataVars.TEMPERATURE.name, temperatureData)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)