How to differentiate the connected bluetooth device in android programmatically? - android

Whether it is a bluetooth headset or mobile phones?
how to differentiate the bluetooth headset and bluetooth enabled android device in android code.
I am developing a small application,in that I have a feature of blocking the data transfer via bluetooth but it need to allow communication via bluetooth headset.
I referred the examples and codes in that they suggest me to
pair/unpair the bluetooth devices only.
Android: How to pair bluetooth devices programmatically?
or else
get all connected devices.
In Android, how to get the profile of a connected bluetooth device?
Whether can i get any broadcast message in device related to type of
device connection?
Please help me on this to differentiate the connected bluetooth device as headset /android devices(mobile phone).etc.,
Thank you in advance.

Once you scan and find a BluetoothDevice call the method BluetoothDevice.getBluetoothClass(). This will return a BluetoothClass object and the documentation states the following:
Represents a Bluetooth class, which describes general characteristics
and capabilities of a device. For example, a Bluetooth class will
specify the general device type such as a phone, a computer, or
headset, and whether it's capable of services such as audio or
telephony.
So before you allow the user to select the device to connect to, or to filter the list of BluetoothDevices shown, try seeing if the BluetoothClass has the correct device type.
BluetoothClass bluetoothClass = bluetoothDevice.getBluetoothClass();
if(bluetoothClass.getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES) {
// allow user to select this device. I'm not sure exactly which type
// headphones will be but this is a good guess. You can connect to
// your Bluetooth headset to find out for sure.
}
The different device class constants can be found here in case you want to differentiate by device class further.

Related

Android & Dual mode Bluetooth device: Custom BLE service not found when paired to BR/EDR (Classic)

I've built a dual mode Bluetooth device using the BT 121 from Silicon Labs. The device implements SPP over the classic connection. The device name of the classic node is "XYZ Classic". It also implements a custom service (128-bit UUID) in BLE. The device name of the BLE node is "XYZ_BLE". Both nodes have the same MAC address.
When pairing under 'Bluetooth' settings, sometimes I will see 'XYZ Classic' and sometimes I will see 'XYZ_BLE'. It seems random which one it will pair to but many Android devices I've tested have a tendency to want to pair to the classic node. After pairing, a connection over classic/SPP ALWAYS works. However, I can only connect to the custom service if paired to the BLE node. If paired to classic I can see non-custom BLE services but not my custom service. To summarize:
Function Classic Pairing BLE Pairing
Spp Works Works
Cust BLE Doesn't Work Works
When I pair in code (Android) the problem also happens despite the fact that I search for the devices named 'XYZ_BLE' and then pair to the device returned. I've looked but I can't see a way to force Android to pair to the BLE node.
My only workaround thus far has been to modify my BT 121 firmware to not allow bonding in classic mode. This is not an ideal solution has any Bluetooth Classic only devices will not be able to bond with my device (In reality this might not be too big of a problem but I have one tablet in my possession that only has Bluetooth Classic).
It should be noted that I've used 'BLE Scanner' from the play store to verify that my custom service works/doesn't work depending on the pairing mode. In other words, it's not just my code. :)
So, does anybody have any ideas on how to force Android to pair to the BLE node?
I see similar issues. Some hints:
Android behaviour differs dependent on the bit flags you set in the BLE advertising. There is for example a bit called "no br/edr support" or so. There are also bits telling explicitly that br/edr dual role is supported.
Also ble pairing may be enforced if you access a characteristic which returns a "not authenticated error" from a ble app. At least on iohone, the os will initiate the ble pairing automatically.

Android BLE peripheral with pin pairing

I am developing an Android app which behaves as Bluetooth peripheral role with a service.
When I start advertising, other Android devices searching for devices offering this service, can see my device (and can pair to it without pin) - ok.
But how to enable PIN pairing?
You can force the device has to be paired, when using specific characteristics by protecting them with PERMISSION_READ_ENCRYPTED_MITM or PERMISSION_WRITE_ENCRYPTED_MITM.
The client/central side can force pairing by calling BluetoothDevice.creteBond().
The pairing method itself is determinated by the bluetooth protocol (see this thread)
You dont actually require pairing to be done to transmit/receive data via ble, take a look into this tutorial

Android Intent: detect when Bluetooth A2DP device gets connected (Not whether a2dp routing is active)

How can I tell when a Bluetooth device connects to the phone, whether it's A2DP or not?
This tutorial gives a really easy-to-follow guide on listening for Bluetooth connections.
I want to use that to determine if any connected Bluetooth device is an A2DP type.
AudioManager has a method isBluetoothA2dpOn() but I don't think this will return true straight away, (or even for certain at all), since just because an A2DP device is connected, that doesn't mean the audio is being routed through it?
I would have thought the information on the type of device connected would be available in the intent received (see tutorial above) but I can't find any documentation on this?
I can get the BluetoothClass with this code:
BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
but that doesn't help me determine if it is an A2DP profile, since the BluetoothClass documentation says:
BluetoothClass is useful as a hint to roughly describe a device (for
example to show an icon in the UI), but does not reliably describe
which Bluetooth profiles or services are actually supported by a
device. Accurate service discovery is done through SDP requests, which
are automatically performed when creating an RFCOMM socket with
createRfcommSocketToServiceRecord(UUID) and
listenUsingRfcommWithServiceRecord(String, UUID)
...which is pretty meaningless to me.

Can I notify when any bluetooth of paired device is enabled?

Hi I am working with embedded bluetooth device connection with my android application.
My application is installed in Android phone and phone has some paired bluetooth devices, Now I want that if any paired bluetooth device is enabled(starts/turns on) the application should receive notification.
of-course bluetooth will be enabled in phone and bluetooth devices will be within range.
please let me know How this is possible ?
Thanks!
How about using http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_CONNECTION_STATE_CHANGED
To check all changed states and then comparing what the new state is:
STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED etc.
There is nothing automatic that can detect this, you will have to make both your App and the paired device do some work to detect this. TYhere could be 2 options :
Your app could do some periodic device discovery (low frequency - else it will kill the battery) to search for the paired devices in vicinity.
The paired devices on getting enabled / starting on should put itself to be discoverable.
A better way is for the apired device to autimaically initiate a connection once it comes on to the device it was paired with, and typically on most phones by default if Bluetooth is turned on its also scanning for incoming connection , so your app can be ready to accept incoming connections.

Programmatically connect to a Bluetooth headset from an Android application

I am looking for a way to initiate the audio connection between the Android phone and my headset within my application. The idea is to simplify the connection process in such a way that the user doesn't have to go through the different settings menus anymore (Apps -> Settings -> Wireless & Networs -> Bluetooth settings).
Both devices are supposed to be already paired and the Bluetooth address of the headset to be known.
As far as I learned, the Bluetooth capabilities (available since version 2.0 of the Android SDK) are restricted to Bluetooth discovery and the connection of RFComm channels (host/client) between the Android phone and a Bluetooth device. Is there another way to request Bluetooth profiles on the Android system to initiate a connection to a known device from an app or is this impossible?
Have you looked at the BluetoothAdapter class? It seems like you can do everything you need via that class, unless I misunderstand the question.
As for initiating a connection to a specific device, won't the phone auto-pair with the known device once you enable bluetooth?
I'm using follow code to get IBluetooth service, and it has sevral function including connect headset.
IBinder b = ServiceManager.getService("bluetooth");
if (b != null) {
IBluetooth mBluetoothService = IBluetooth.Stub.asInterface(b);
}
But, these apis are hidden for app level, and they are different on different android sdk version.You must comply the code in android whole source enviroment, or use reflection to access them.

Categories

Resources