I am building a Bluetooth application in Xamarin Android and my app has three BroadcastReceivers. One for detecting when the adapter has started device discovery ActionDiscoveryStarted, a second one when a Bluetooth device has been discovered ActionFound and a third one for detecting when the device discovery has finished ActionDiscoveryFinished. My application toasts a message to the user when an intent is intercepted from the system. Both the first and the third intents work from the screenshots below.
Now I have a Bluetooth device here and it is the laptop am typing this on, I expected my app to detect my laptop and toast the device found message from the second broadcast receiver but that is not working and it shows 0 devices found. I have all the location and bluetooth permissions in my manifest like
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
and the code for the three broadcast receivers is listed below
DeviceDiscoveryStarted receiver
//register a broadcast for listening when the device discovery process has started
[IntentFilter(new[] { BluetoothAdapter.ActionDiscoveryStarted })]
[BroadcastReceiver(Enabled = true, Exported = false)]
public class DeviceDiscoveryStarted : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == BluetoothAdapter.ActionDiscoveryStarted)
{
Toast.MakeText(context,"Bluetooth device discovery started",ToastLength.Long).Show();
}
}
}
This receiver is not working despite having mylaptop bluetooth on
DeviceFound receiver
//register a listener for listening when a new bluetooth device has been discovered
[IntentFilter(new[] { BluetoothDevice.ActionFound })]
[BroadcastReceiver(Enabled = true, Exported = false)]
public class DeviceFound : BroadcastReceiver
{
private List<BluetoothDevice> _bluetoothDevices;
//declare the default constructor
public DeviceFound()
{
}
//declare the custom constructor and require the bluetooth device list from the other class
public DeviceFound(List<BluetoothDevice> devicelist)
{
_bluetoothDevices = devicelist;
}
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == BluetoothDevice.ActionFound)
{
//toast to the user that a bluetooth device has been found
Toast.MakeText(context,"New device found",ToastLength.Long).Show();
//get the device as an extra parcelable
BluetoothDevice device =(BluetoothDevice) intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
//if device is not equal to null append to the list
if(device!= null)
_bluetoothDevices.Add(device);
}
}
}
ActionDiscoveryFinished reciever
//program a receiver for the intent ACTION_DISCOVERY_COMPLETED
[IntentFilter(new[] { BluetoothAdapter.ActionDiscoveryFinished })]
[BroadcastReceiver(Enabled = true, Exported = false)]
public class DiscoveryFinished : BroadcastReceiver
{
private List<BluetoothDevice> _devices;
//default ctor
public DiscoveryFinished()
{
}
public DiscoveryFinished(List<BluetoothDevice> devices)
{
_devices = devices;
}
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == BluetoothAdapter.ActionDiscoveryFinished)
{
//toast to the user that the discovery has been finished
Toast.MakeText(context,"Discovery has finished...Found"+_devices.Count,ToastLength.Short).Show();
}
}
}
and this is how I registered my receivers in the OnCreate of my app
//register a receiver for a bluetooth device discovery process started
DeviceDiscoveryStarted started = new DeviceDiscoveryStarted();
RegisterReceiver(started, new IntentFilter(BluetoothAdapter.ActionDiscoveryStarted));
//register a receiver for a bluetooth device discovered
DeviceFound found = new DeviceFound(_bluetoothDevices);
RegisterReceiver(found, new IntentFilter(BluetoothDevice.ActionFound));
//register listener for discovery finished
DiscoveryFinished finished = new DiscoveryFinished(_bluetoothDevices);
IntentFilter filter = new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished);
RegisterReceiver(finished, filter);
The app is working fine, the reason as to why it was not discovering the laptop as a bluetooth device is because it was already a paired device, I made the laptop forget my phone and the device found broadcast receiver worked.
Related
My Android application is continuously using the microphone and wants to release the microphone to other applications such as VOIP Calls.
I have registered broadcast receiver in the Manifest. But the microphone is not getting released.No sim card in the phones, I am only interested in detecting SIP sessions.
Here is my code in the Manifest
<receiver
android:name=".SipCallReceiver"
android:enabled="true"
android:exported="true">
</receiver>
Broadcastreceiver class
public class SipCallReceiver extends BroadcastReceiver {
ListeningActivity LA = new ListeningActivity();
#Override
public void onReceive(Context context, Intent intent) {
SipAudioCall call = null;
final SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onCallEnded(SipAudioCall call) {
LA.setflgphone(false); LA.onPstop();
}
#Override
public void onCallEstablished (SipAudioCall call) {
LA.setflgphone(true); LA.Lstop();
}
};
}
}
Can anyone please let me know the mistake I am making here.
Thank you
I was able to solve it.
I created two services.
The first service monitors the Audio Manager at schedule interval with ScheduledExecutorService. This will get the mode of the Audio Manager.
When there is no communication, the mode will be 0.
This status broadcast to a locally registered broadcast receiver. The broadcast receiver will stop and start the second service which using the microphone depending on the audio mode.
I am recently learning Android. I tried to implement the function that searches for Bluetooth devices. I am able to list all the previously paired devices, but I am not able to discover new devices.
Permissions are set in AndroidManifest.xml.
Here is my code:
public void listDiscoverableDevices(boolean status) {
// Create a BroadcastReceiver for ACTION_FOUND
mBluetoothAdapter.cancelDiscovery();
mArrayAdapter.clear();
mDisplay.setText("Search");
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//BluetoothDevice.
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mArrayAdapter.add(device.getAddress());
} else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
//mDisplay.setText(action);
mArrayAdapter.add(action);
} else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//mDisplay.setText(action);
mArrayAdapter.add(action);
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
if(!mBluetoothAdapter.startDiscovery()) {
mDisplay.setText("Discover failed!");
}
}
It seems to me that the BluetoothDevice.ACTION_FOUND action is never received. I have been struggling with this for two days. Any idea?
It took me two days. Today I finally found out that why my program didn't work the first time. Starting from Android 6.0, you also need either android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_FINE_LOCATION permission to scan for remote Bluetooth devices.
And you may need to go to "Settings"->"Apps"->yourApp->"Permissions" to turn on the location service permission. Or I guess you can also request the permission programmatically.
To conserver battery in my application, whenever my app needs to sync with the cloud, I first check if the network is available. If no connection is available, I register a network broadcast receiver as follows. But this never gets called.
I'm testing this by putting the device into airplane mode. I see the "Network Receiver ENABLED" message. But after I disable airplane mode, and after my Wifi connects, I expect to see the "Received Network Change Intent" message, and it never appears.
Can anyone point out what I may be doing wrong?
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
networkReceiver = new NetworkReceiver();
registerReceiver(networkReceiver, filter);
Log.d(TAG, "Network Receiver ENABLED");
This is the NetworkReceiver:
public class NetworkReceiver extends BroadcastReceiver {
private static String TAG = NetworkReceiver.class.getSimpleName();
#Override
public void onReceive(Context context_, Intent intent_) {
Log.d(TAG, "Received Network Change Intent");
}
}
You can try below option.
Set Network Access Permission if you haven't declare in manifest file: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>.
Register Receiver using context:
Instead of registerReceiver(networkReceiver, filter); use getApplicationContext().registerReceiver(networkReceiver, filter);
I am running my activity and this has a Broadcast receiver which listens to ble updates from a Ble service class. When the device is disconnected when in not in proximity I am using AltBeacon to start my application when the device is in region and it connects to the device. But the Broadcast receiver is not started.
My code is something as follows:
class MyActivity{
onCreate(){
}
onResume(){
registerReciever(....);
}
private final BroadcastReceiver gattReceiver = new BroadcastReceiver(){
onReceive(Context context, Intent intent){
}
}
}
I have an Android device on Jelly Bean, and i want it to wake up when an already paired device is detected within the bluetooth range.
I guess it's a service broadcast receiver but i don't know how it should work.
Register a BroadcastReceiver to listen for BluetoothDevice.ACTION_BOND_STATE_CHANGED.
Inside a BroadcastReciever:
public void onReceive(Context context, Intent intent)
{
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction()))
{
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
if (bondState == BluetoothDevice.BOND_BONDED)
{
// wake up
}
}
}