Disable Bluetooth sound output - java

I am writing an app which monitors the volume setting of the notification stream (using a broadcast receiver for android.media.VOLUME_CHANGED_ACTION - kind of a hack I know) and when it is set to silent I would like to stop my Bluetooth headset from ringing but without disconnecting it, so that if I choose to answer the call (it vibrates or I see it ringing) I can do so from my Bluetooth headset.
So far I've tried muting the voice stream, the Bluetooth stream (by providing the int 6), setting setBluetoothScoOn(false), using stopBluetoothSco() and muting the previously mentioned streams but nothing seems to work. I've got the modify audio permission so I'm beginning to think it isn't possible.
Does anyone have an idea?

When connected on bluetooth headset, call notifications are send using HFP protocol , this protocol only internally sends certain AT commands(phone to headset) which lets headset know about the call , settings SCO ON/OFF will turn call audio to headset/phone, ringing is internal to bluetooth headset

Related

Can I listen to a Bluetooth data receipt on a broadcast receiver on android and if I can, how?

I'm trying to build a Bluetooth emergency beacon(a MCU hooked up to a Bluetooth module),which after I press a button sends some data to my android app on my phone. I want to listen to this data reception (even when the app is not in the foreground) and then do something in my app, like call 911. So what's the way to do that, if there's any.

Hi, I have a question about Bluetooth profile

I'm making a speech recognize App.
But there is problem.
App have to listen user's voice, So Bluetooth profile change to sco.
But I want to change Bluetooth profile to A2dp when I listen the music.
The Service can close by AudioManager.isMusicActive() but it can't change back A2dp.
Is there answer for it? I know that 2 profile can't be incompatible.
So, When i use for speech recognize , Bluetooth profile -> Sco
When i listen to music, Bluetooth profile -> A2dp
How can i handle it?

Communication between BLE device and different android apps on same device

I'm new in Android BLE so my question may be uncorrect or naive in some way. If this is the case please explain me where I'm wrong and kindly show me the correct way to manage this scenario.
Scenario is the following: my Android app communicates with a BLE device sending commands and getting answers from device using BLE characteristics.
Sequence is:
Device wakes up the app (the onConnectionStateChange method is called)
My app writes a command in a characteristic (I call writeCharacteristic putting the command in value parameter).
Device sends back the answer to command to my app (the onCharacteristicChanged method is triggered and value parameter contains the answer)
After waking up the app, the device doesn't do anything until a command is sent via writeCharacteristic. The device accepts different commands.
All good so far, but recently I developed a second different app to communicate with same device.
When I run both apps on same Android phone, one app sends a command to the device and the response is received by both apps! Of course the app that didn't sent the command receives an unexpected answer and goes to an unexpected status.
Ok, knowing the problem I can modify both my apps to handle this situation, but the question is: Is this behavior normal when two apps in same device communicate with same BLE device?
Is there a way for an app to establish a communication channel with a BLE device to avoid sending answer to specific commands to any other app except the one that sent the request?
My guess is that writeCharacteristic and onNotificationChanged aren't the right functions for such kind of communication, but in this case which are the alternatives?
The Bluetooth standard itself doesn't define anything how multiple apps would behave if both have a GATT connection to the same device. In the standard there is just one "GATT client".
Now both iOS and Android have taken one step further in a way that might seem unintuitive. Instead of only allowing one app at a time to communicate, multiple apps can be connected over the same GATT client to a device. The OS "multiplexes" the communication from/to the apps. The behaviour is that responses to read and write requests can only be seen by the app that made the request. So if you do readCharacteristic only that app will get the onCharacteristicRead callback. Notifications however will be delivered to both apps to the onCharacteristicChanged callback, since it wouldn't make any sense to send the notification to only one.
When you say that the "response" to a write request is the notification, that's not correct in terms of GATT terminology. The response to a write request is always empty per specification (or an error). If your peripheral emits a notification, then in your case that might be the "answer" according to your own logic, but it's not a response or any way related to your write request per the GATT specification. That's why Android can't (and shouldn't) send the notification to only one device.
I suggest that you simply ignore notifications you are not expecting. If you want to associate an "answer" to a write request, you can change your protocol to include a transaction id in both packets so they can be matched.
When I write "app" above, I really mean BluetoothGatt objects. You can call connectGatt twice with the same remote device in the same app, which will behave the same as if you connected from two different apps.

Can we distinguish Blackberry is in USB mode or charge mode?

I am new to blackberry-7. In my application when I plug my phone into the cable (by means of the USB cable) I want to find if my blackberry connected with USB is in charging mode or transfer mode. How is it possible?
The USB can be detected asynchronously using SystemListener2 and also synchronously using DeviceInfo.getBatteryStatus.
For what you need the battery status won't work because it probably charges in both normal and transfer modes, and then there's the problem of detecting the cable when the battery is full. So I'd try registering a SystemListener2 instance and hopefully the callback usbConnectionStateChange will be passed the flag SystemListener2.USB_STATE_CABLE_CONNECTED as argument. When it is transferring, I'd expect the flags USB_STATE_ENUMERATED and USB_STATE_MS_INTERFACE_ENUMERATED to be received in the callback as well, but I've never tested it on a real device.
And I don't know what would happen if the device is already connected when the listener is registered.
To register the listener, call
Application.getApplication().addSystemListener(mySysListener2);

Android Bluetooth Development - connecting to an UNPAIRED device

First off, I AM able to programmatically connect to a device via bluetooth if I have manually paired it beforehand. However, I haven't found a (user-friendly) way to connect with unpaired devices (that are discoverable, obviously), even using the unchanged sample Android Bluetooth code.
I would like to pop up the pairing dialog box that requests the user to input a passkey for the device. What happens with my code, and the sample Android Bluetooth code, is that making a socket with a discoverable bluetooth device
device.createRfcommSocketToServiceRecord(MY_UUID);
will add a notification to the notification bar at the top of the screen; however, it is pretty unlikely that the user will even notice a new notification (I know I didn't).
How would I pop up that pairing dialog box, prompting the user to enter the passkey - or better yet, supply a passkey for a particularly device automatically?
You can use BlueCove library to do that. However, it may be a bit heavier than you'd want as it requires a native component but in return gives you real access to Bluetooth on your phone.
Tim: I can see how a rogue app could abuse this mechanism but there are devices out there which don't support traditional PIN-based authentication method at all (for example the WiiMote).
I've seen a few threads on this but haven't really found a good
answer.
I am connecting my nexus 1 to a bluetooth spp server device to read
and write data.
After some experimentation I have found the following
If I offload the establishment of the connection from the UI thread
(which I would consider a good idea) the pairing dialog (enter pin) is
not displayed - rather a notification item appears which the user must
click to pair the devices.
If I establish the connection from within the UI thread then the
paring dialog will come to the foreground but obviously there is a
small ugly UI pause while waiting for this.
In either case the request to establish the connection is timing out
before the user (me) can enter the pin with an IOException Service
discovery failed.
My ideal would be that the pairing dialog comes to the front (without
have to block my UI thread) and that the connection attempt would not
time out while waiting on pairing entry.
Has anyone else come up with a way to make this work?

Categories

Resources