Android connects to a paired Bluetooth headset
I want to simulate the action "Settings"> "Wireless"> Bluetooth and programmatically connect a paired Bluetooth headset. I did a search on Stackoverflow and Google, both indicated that there was no solution up to the API level. However, I am interested in understanding the source code for the Android bluetooth implementation. The problem is that I donβt know which specific source code I should consider. Any suggestions? Thank you very much.
OK, I got this update to support cellular and above. You need to add new features to the interface. I did it here:
interface IBluetoothA2dp { boolean connectSink(in BluetoothDevice device); // Pre API 11 only boolean disconnectSink(in BluetoothDevice device); // Pre API 11 only boolean connect(in BluetoothDevice device); // API 11 and up only boolean disconnect(in BluetoothDevice device); // API 11 and up only boolean suspendSink(in BluetoothDevice device); // all boolean resumeSink(in BluetoothDevice device); // all BluetoothDevice[] getConnectedSinks(); // change to Set<> once AIDL supports, pre API 11 only BluetoothDevice[] getNonDisconnectedSinks(); // change to Set<> once AIDL supports, int getSinkState(in BluetoothDevice device); boolean setSinkPriority(in BluetoothDevice device, int priority); // Pre API 11 only boolean setPriority(in BluetoothDevice device, int priority); // API 11 and up only int getPriority(in BluetoothDevice device); // API 11 and up only int getSinkPriority(in BluetoothDevice device); // Pre API 11 only boolean isA2dpPlaying(in BluetoothDevice device); // API 11 and up only }
Then you need to check the API version before calling the functions in this interface. Here is my example:
if (android.os.Build.VERSION.SDK_INT < 11) { IBluetoothA2dp ibta = getIBluetoothA2dp(); try { Log.d(LOG_TAG, "Here: " + ibta.getSinkPriority(device)); if (ibta != null) ibta.connectSink(device); } catch (Exception e) { Log.e(LOG_TAG, "Error " + e.getMessage()); } } else { IBluetoothA2dp ibta = getIBluetoothA2dp(); try { Log.d(LOG_TAG, "Here: " + ibta.getPriority(device)); if (ibta != null) ibta.connect(device); } catch (Exception e) { Log.e(LOG_TAG, "Error " + e.getMessage()); } } Hope this helps. I managed to get the same application to work with both interfaces.
After several days of battle, I managed to do this, greetings :)
- Add the android.bluetooth.IBluetoothA2dp.aidl file to your / src directory of your application;
Add this private method to your code:
private IBluetoothA2dp getIBluetoothA2dp() { IBluetoothA2dp ibta = null; try { Class c2 = Class.forName("android.os.ServiceManager"); Method m2 = c2.getDeclaredMethod("getService", String.class); IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp"); Log.d("Felix", "Test2: " + b.getInterfaceDescriptor()); Class c3 = Class.forName("android.bluetooth.IBluetoothA2dp"); Class[] s2 = c3.getDeclaredClasses(); Class c = s2[0]; // printMethods(c); Method m = c.getDeclaredMethod("asInterface", IBinder.class); m.setAccessible(true); ibta = (IBluetoothA2dp) m.invoke(null, b); } catch (Exception e) { Log.e("flowlab", "Erroraco!!! " + e.getMessage()); }Test it as follows:
private void testBluetoothA2dp(BluetoothDevice device) { // TODO Auto-generated method stub // TODO Auto-generated method stub IBluetoothA2dp ibta = getIBluetoothA2dp(); try { Log.d("Felix", "Here: " + ibta.getSinkPriority(device)); ibta.connectSink(device); } catch (RemoteException e) { // * TODO Auto-generated catch block e.printStackTrace(); }}
I cannot provide links to this code, since I spent a lot of time searching on Google, checked stackoverflow and looked at the Android source code, but could not track the sources. Many thanks to you guys at Stackoverflow :)
this is KIND related to connect / reconnect problem (ANSWER is an apple script) ..
I just bought Android htc one V and use it as a hot spot through the PdaNet application (installed both on my phone and on my mac os 10.5.8 ppc laptop).
It seems that I canβt connect to a modem that works via Wi-Fi or USB, but it works well with BLUETOOTH! the only problem is that the connection lasts only from 2 minutes to 40 minutes (looking at it now ha, recording), and I need to reconnect manually, which takes only 2 seconds, but it would be nice if the network aspect of my mac could AUTO reconnect.
My cell is not a problem, because it sends a constant signal (although I can temporarily lose the signal from my cell, which is a normal connection). The problem is that my laptop automatically connects to RE-connect. my laptop and htc one v DO remain paired, on the laptop there is simply no AUTO-REPAIR.
I wish I knew the bcs apple scripts, then I could write an apple script that AUTO RECONSTRUCTION lost the Bluetooth connection .. or maybe the widget could do this? if so, I would do it in the shade, since the bluetooth modem is NICELY.
I was hoping this would help both me and other people to look for the same answers. If you leave this thread open, I can come back later with some possible apple script solutions (which I will need to quickly learn) .. thanks -marcus