Bluetooth Auto connection - android

I am currently working on AOSP. I wanted to know how to stop the auto connection procedure which is in progress while a user requests for an inquiry of available devices. And I also want to re-start the auto connection once the inquiry is completed or user has aborted the enquiry.
I have to make these changes in the Android Source Code and I am currently working on Android P.

I am not sure if I understood your question well but reconnection or auto connection of bluetooth profiles is always handled by the state machine and there is no way to stop or pause it.
The only way to achieve this type of behaviour is to access the particular profile service via proxy and then on that service call this method:
profileService.setPriority(bluetoothDevice, BluetoothProfile.PRIORITY_OFF);
The above statement will make state machine not to process any connection requests on the specific profile on which you called this method and on the bluetoothDevice which you passed as parameter and once you are done with the query just call the same method again like this:
profileService.setPriority(device, BluetoothProfile.PRIORITY_ON);

Related

How to detect when requestNetwork error dialog has been dismissed by the user in Android

Context
I am developing an Android app that programmatically switches the phone's WiFi connection to a device's access point. The device has two separate controllers that can be independently turned on/off, and each controller has its own access point. The app will try connecting to one of the access points first, and if it fails then it will try connecting to the other access point.
For API >=29 I am using ConnectivityManager.requestNetwork with a timeout to attempt connection to the first AP, and ConnectivityManager.NetworkCallback to detect when the connection succeeds or fails. If NetworkCallback.onUnavailable is called while attempting connection to the first AP, then I initiate a new connection request to the second access point.
In case it matters, I am currently testing on a Samsung A50 phone running Android 11 (API 30).
Problem
Everything works well if the first AP is online - the phone successfully connects to the first AP. But if the first AP is offline, then two things happen simultaneously:
The NetworkCallback.onUnavailable function is called.
A system dialog appears near the bottom of the screen with the following text:
"Couldn't connect. Check your network connection and try again."
If I initiate a new network request to the 2nd AP before the user dismisses the system dialog, then the new network request does not work. Even though the 2nd AP is online, the phone doesn't connect to it. Eventually the new network request times out and calls the new NetworkCallback.onUnavailable function which terminates the 2nd network request. However, if I wait until the user closes the system dialog before initiating a new network request, then the 2nd network request succeeds; the phone connects to the 2nd AP.
I discovered this through trial and error by implementing a fixed delay, but this is not a viable solution since there's no way to know how long it will take a user to dismiss the system dialog.
Question
Is there any way to programmatically detect when the user has dismissed the system dialog? If so, then I could use that to trigger the start of the 2nd network request.
Alternatively, is there any alternate approach that allows sequentially attempting connection to a list of AP's without encountering this issue?

Pjsip call not disconnects when any one side goes out of network coverage

I am able to make calls, if anyone sides whether call initiator or receiver goes out of network coverage that time no voice packets transferring is possible, in fact when anyone side get reconnects the network, again call gets reconnects successfully.
But biggest problem now I am facing is when anyone end goes out of network for more than specified time then both call should disconnect automatically is there any possible way to handle such a scenario?
Thank you in advance
Look into SIP session timers feature configuration. Or (I prefer this for interoperability) disconnect if there is no RTP traffic or connection state does not change for a long time (depending on call state media session may not exist or be deactivated with mute, so checking just RTP may be not sufficient).

Android bluetooth - Accept a call, reject a call, and display caller ID

I'm integration bluetooth support in my Android app for VOIP calling scenarios. Goal: display caller ID on bluetooth device, allow accept/reject of incoming/active calls through the BT peripheral. Without any work, everything works as expected when calling with the native dialer on Android but I can't find the appropriate APIs to implement the same behavior in our app - are these not exposed?
If a VOIP call is initiated through our app, we start the bluetooth SCO connection and listen for any media /state changes. Pressing the 'answer/reject' button results in a DISCONNECTED state change event (could use this to reject the call but that's not reliable as the same event is received if the BT device is disconnected). I haven't found any leads on displaying the appropriate caller ID (a custom string that I want to provide) or answering calls. Anyone been on this path and have some pointers?
Thanks!

Specifying a link key in android without pairing

I am trying to determine if there is a way in android to associate a specific link key with an already existing remote bluetooth device instance.
Essentially what I want to do is create a connection with a non-discoverable bluetooth device without going through the pairing or re-pairing procedure.
I am not able to establish the link key with the device in a standard pairing procedure because I am working with a custom proprietary pairing mechanism. I would prefer to accomplish this task without using native code, but if I have to then I will.
I needed to solve this problem myself. The crucial step was finding this code, which told me to add the android.bluetooth package to my project, and add the files IBluetooth.aidl and IBluetoothCallback.aidl (which you'll find at the link).
Once you instantiate the IBluetooth object, you have access to the BluetoothService class, and can use any of the methods in IBluetooth.aidl. The method I was interested in was
setPin(String address, byte[] pin)
The problem with using it is the other Bluetooth code expects the pairing dialog to have been called already, and keeps track of that in a HashMap in the BluetoothEventLoop class. If you try calling setPin() without having initiated a pairing request, you'll see an error like this:
setPin(<address>) called but no native data available, ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device or by bluez.
So the workaround for me (using the Chat example) was starting the connect thread to initiate the pairing request, then sleeping 500 ms to ensure the thread had started, then calling setPin().
there is no public api mechanism to associate a link key with a device without going through the pairing process.
After pairing this association is automatically created (aka bonding) once devices are bonded then further connection will re-use the link key that was generated previously.
even if device is non-discoverable you should still be able to connect bond / pair with it, if you know the device bluetooth address.
internal / private mechanism by changing the underlying android bluez code, and hooking up to feed a pre-generated link key etc is theoretically possible and it will be a difficult project and a custom solution.
that is assuming that you have the link keys to feed / associate. note - link key is a function of the device address of both devices in addition to device clock etc.
bluetooth

Android: Using service, check if connection get broken

Iam connection to services trougth my app, I woul like to check if the connections gets broken, alert a message than.
Or is this taken carre of in android?
If not, How should I take care of it? is there a timer?
If you are connecting to built-in services of Android, for example Connection with GPRS, WIFI or MOBILE 3G connection, then you can register a Broadcast Receiver that will tell the user when the connection is lost.
But if you are connecting to your custom-made services, for example ABC Application, then you would have to make your own service, that will periodically (example every 15mins) will check the connectivity with ABC Application. Hope this helps.

Categories

Resources