Android connect to a paired bluetooth headset - android

I want to simulate the action of going throgh Settings->Wireless->Bluetooth and connect a paired bluetooth headset programmatically. I've done some search in Stackoverflow and Google, both indicating that there is no solution available before API level 11. However, I'm interested in working it out by peeking in the source code of bluetooth implementation of Android. The problem is that I'm not aware of which specific source code I should review. Any suggestions? Many thanks.

After days of struggling, I've now managed to do it, cheers :)
Add android.bluetooth.IBluetoothA2dp.aidl in your /src dir of your app;
Add this private method in 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 with this:
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'm not able to provide references of these code since I've spent lots of time googling, checking out stackoverflow, and reviewing Android source code, but failed to keep track of sources. Many thanks to you guys in Stackoverflow :)

OK, I got this updated to support Honeycomb and up. You need to add new functions to the interface. I did that 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 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 was able to get the same app to work with both interfaces.

I tried this on Android 4.2
and following line returns null.
It was working on 4.1, any ideas ?
IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp");

this is KIND of related to the connect/reconnect issue (the ANSWER being an apple script) ..
i just bought an android htc one V and am using it as a hotspot via the app PdaNet (installed both on my phone and my mac os 10.5.8 ppc laptop).
I can't seem to get the hotspot tethering working via wifi or usb but it DOES work well with BLUETOOTH! only problem is the connection only lasts up to 2 minutes to 40 minutes (looking at it now ha, a record) and i have to reconnect manually, which only takes 2 seconds but it'd be nice if the Network aspect of my mac could AUTO reconnect.
my cell is not the problem as it's sending out a constant signal (although i could be losing signal temporarily from my cell, which is normal connection stuff) .. the problem is having my laptop auto RE-connect. my laptop and htc one v DO remain paired, there's just no AUTO-RECONNECT on the laptop end.
i wish i knew apple scripts bcs then i could maybe write an apple script that AUTO-RECONNECTS a lost bluetooth connection .. or maybe a widget could do this? if so i'd have it made in the shade as bluetooth tethering is working NICELY.
i was hoping this would help both myself and others searching for the same answers .. if you keep this thread open i could return later with some possible apple script solutions (which i will have to quickly learn) .. thanks -marcus

Related

Why do different Android devices allow for different maximum number of simultaneous Bluetooth connections?

I have an Android tablet that acts as a Bluetooth server and allows up to 6 clients to connect (I set this limit, knowing that the theoretical maximum is 7, as per the Bluetooth piconet specification). I tried with two different tablet models from the same manufacturer: Huawei Mediapad T3 and Huawei MediaPad T5. On the T3, I can get up to 6 connections that remain stable and allow communicating almost seamlessly with the clients. On the T5, however, the tablet will not accept more than 4 simultaneous connections. If I disconnect one device, one of the two remaining ones which keep requesting to connect gets accepted in its place. I do not get any error messages from the calls to the accept() method, or any other errors regarding Bluetooth in the Android Logcat.
Both tablets run under EMUI 8.0.0 (the Huawei brew of Android 8.0.0).
Here is the code I run for the thread that accepts the connections :
private class AcceptThread extends Thread {
private BluetoothServerSocket serverSocket;
AcceptThread(String name, String uuidStr) {
btAdapter.cancelDiscovery();
try{
serverSocket = btAdapter.listenUsingInsecureRfcommWithServiceRecord(name, UUID.fromString(uuidStr));
} catch (IOException ex){
onStatusListener.OnStatus("server.error.COULD_NOT_LISTEN");
Log.e(TAG, "Could not open socket", ex);
}
}
public void run() {
onStatusListener.OnStatus("server.listening");
while (connections.size() < maxConnections) {
setState(STATE_LISTENING);
try {
BluetoothSocket socket = serverSocket.accept();
if (socket != null) {
ConnectedThread conn = new ConnectedThread(socket);
setState(STATE_CONNECTING);
conn.start();
connections.add(conn);
conn.setOnDisconnectEvent(() -> {
onStatusListener.OnStatus("server.disconnected." + socket.getRemoteDevice().getAddress());
connections.remove(conn);
});
onStatusListener.OnStatus("server.connected." + socket.getRemoteDevice().getAddress());
}
} catch (IOException ex) {
onStatusListener.OnStatus("server.error.COULD_NOT_ACCEPT");
Log.e(TAG, "Socket's accept() method failed", ex);
break;
}
}
onStatusListener.OnStatus("server.not_listening");
cancel();
}
void cancel(){
try {
setState(STATE_NONE);
serverSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close server socket", e);
}
}
}
What can cause my code to work on the T3 tablet and not on the T5?
Is there anything I could do differently to get my 6 simultaneous connections working with both tablets?
When it comes to the maximum connections allowed by a device, the limitation is usually related to the hardware and not the software. In other words, it is the Bluetooth hardware on the T5 that is preventing you from creating further connections. This is evident by the fact that both tablets are running the same OS yet still exhibit this difference.
What I find strange is the fact that you are not getting any error response back when you attempt to exceed the number of possible connections. Unfortunately I don't think there is a way to increase the limit on the T3. I would contact Huawei if possible as they would be able to confirm this discrepancy.

Ethernet Connectivity through Programmatically (Android) (Rooted Device)

I have a small issue regarding Ethernet.
My three questions are:
Can we programmatically Turn-On/Off Ethernet?
Can we programmatically Enable/Disable Ethernet?
Can we programmatically Connect Ethernet?
The above Questions are done with the Wifi. Like
We can programmatically Turn-On/Off Wifi.
We can programmatically Enable/Disable Wifi.
We can programmatically Connect Wifi using WifiManager.
Does android provides any EthernetManager like as WifiManager to handle Ethernet?
Or, if this doesn't seem feasible, then my original requirement is:
The first thing I am going to clear is "DEVICE IS ROOTED" .
Can I manipulate the Settings (Default)? Like I don't want any other option in the Settings.apk other than WIFI and Ethernet. It should show only Wifi and Ethernet. That's it. Can I disable all the options from the Settings or Can I remove all the other options from the Settings?
The solution I will present here is a hack using reflection and does only work on a rooted android system.
Your device might have the popular android.net.ethernet package. In an Activity, try
Object emInstance = getSystemService("ethernet");
It returns an valid instance of the EthernetManager or null. Null means you are out of luck.
An additional requirement might be depending on your device: Ethernet and Wifi might only work exclusively. You might need to disable Wifi to enable Ethernet and vice versa.
To enable Ethernet by reflection use your instance of the EthernetManager.
The method you want to invoke is setEthEnabled(boolean enabled)
Class<?> emClass = null;
try {
emClass = Class.forName("android.net.ethernet.EthernetManager");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object emInstance = getSystemService("ethernet");
Method methodSetEthEnabled = null;
try {
methodSetEthEnabled = emClass.getMethod("setEthEnabled", Boolean.TYPE);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
methodSetEthEnabled.setAccessible(true);
try {
// new Boolean(true) to enable, new Boolean(false) to disable
methodSetEthEnabled.invoke(emInstance, new Boolean(false));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Your application manifest needs these permissions
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
The permission WRITE_SECURE_SETTINGS can only be acquired by system apps. The app does not need to be signed by a system key. It can be any valid sign (like the regular Android App Export function). Use busybox to remount the system partition for write access and move your apk into the /system/app folder. Reboot the device and it should work.
Can we programmatically Connect Ethernet ?
There is no Access Point to connect you like with Wifi. You either configure it for DHCP or provide static values. This can of course also be done via reflection.
You will need the class EthernetDevInfo for that.
The actual implementation of the EthernetManager and EthernetDevInfo might slightly differ between Android versions and devices as it doesn't have to conform to a public api (yet) and might even be a custom version.
To get a list of getters and setters you can use a Introspector or reflection in general.
Ok here are some methods i made for manipulating with the ETHERNET INTERFACE (eth0).
1) A method for checking if an ethernet interface exists
public static boolean doesEthExist() {
List<String> list = getListOfNetworkInterfaces();
return list.contains("eth0");
}
public static List<String> getListOfNetworkInterfaces() {
List<String> list = new ArrayList<String>();
Enumeration<NetworkInterface> nets;
try {
nets = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
return null;
}
for (NetworkInterface netint : Collections.list(nets)) {
list.add(netint.getName());
}
return list;
}
2) A method for checking if the ETHERNET is enabled or ON
public static boolean isEthOn() {
try {
String line;
boolean r = false;
Process p = Runtime.getRuntime().exec("netcfg");
BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if(line.contains("eth0")){
if(line.contains("UP")){
r=true;
}
else{
r=false;
}
}
}
input.close();
Log.e("OLE","isEthOn: "+r);
return r;
} catch (IOException e) {
Log.e("OLE","Runtime Error: "+e.getMessage());
e.printStackTrace();
return false;
}
}
3) A method for enabling or disabling the Ethernet depending on the state in which it is
public static void turnEthOnOrOff() {
try {
if(isEthOn()){
Runtime.getRuntime().exec("ifconfig eth0 down");
}
else{
Runtime.getRuntime().exec("ifconfig eth0 up");
}
} catch (IOException e) {
Log.e("OLE","Runtime Error: "+e.getMessage());
e.printStackTrace();
}
}
4) A method for connecting via ethernet depending on the chosen type (dhcp/static)
private boolean connectToStaticSettingsViaIfconfig(StaticConnectionSettings scs) {
try {
if(typeChosen.equalsIgnoreCase("dhcp")){
Runtime.getRuntime().exec("ifconfig eth0 dhcp start");
}
else{
Runtime.getRuntime().exec("ifconfig eth0 "+scs.getIp()+" netmask "+scs.getNetmask()+" gw "+scs.getGateway());
}
} catch (IOException e) {
Log.e("OLE","Runtime Error: "+e.getMessage());
e.printStackTrace();
return false;
}
return true;
}
There is one more class which i created for storing all the eth values needed. This class is than initialized with the values the user inserts.
public class StaticConnectionSettings {
private String ip, netmask, dns, mac, gateway, type;
//Getters and Setters
}
This is it ... I will test it shortly... This code lacks a test phase (ping). And maybe it needs setting of DNS. But this can be done easily. I have not included it because i think on our device it will work also without the DNS setting.
It works for Android 6.0.1
Class<?> ethernetManagerClass = Class.forName("android.net.ethernet.EthernetManager");
Method methodGetInstance = ethernetManagerClass.getMethod("getInstance");
Object ethernetManagerObject = methodGetInstance.invoke(ethernetManagerClass);
Method methodSetEthEnabled = ethernetManagerClass.getMethod("setEthernetEnabled", Boolean.TYPE);
methodSetEthEnabled.invoke(ethernetManagerObject, isEnabled);
Three Answeres to your above questions:
Yes. You could try using ifconfig eth0 down ; ifconfig eth0 up. But i have not tested it by myself yet.
Yes, but you do not have to. Android does the switching for you. If you connect to WiFi, Ethernet disables. If you are already connected to WiFi and you plug your ethernet cable into the device; you need only to disable WiFi (which you know how to) and android switches automatically to ethernet.
Not so easy as you may think. I have the same problem and until now i have found only one solution which i have not yet tested. Since android runs on the linux kernel, we can use ifconfig in order to manipulate the ethernet connection.
An explanation is hidden here:
http://elinux.org/images/9/98/Dive_Into_Android_Networking-_Adding_Ethernet_Connectivity.pdf
And the youtube video of this lecture
http://www.youtube.com/watch?v=LwI2NBq7BWM
And a reference on how to use ifconfig for android
Android ethernet configure IP using dhcp
So if you come to a possible solution, please share it!! If i will do it before you i will certenly.

Access point in nexus 7 and nexus 10 issue

I'm developing an app that needs that the device creates an access point. I'm testing the code on a Nexus 7 (Android 4.2.1) and a Nexus 10 (4.2.2) and I get the same problem in both. I manage to enable the access point but every device that tries to connect stays "Obtaining ip address". Any idea why? I tested the same code in a galaxy tab 10" (Android 4.0.3) and it works perfect.
private void createWifiAccessPoint(String pSSID)
{
if(mWifiManager.isWifiEnabled())
{
mWifiManager.setWifiEnabled(false);
}
Method[] wmMethods = mWifiManager.getClass().getDeclaredMethods();
boolean methodFound=false;
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled"))
{
methodFound=true;
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = pSSID;
try {
boolean apstatus = (Boolean) method.invoke(mWifiManager, netConfig,true);
for (Method isWifiApEnabledmethod: wmMethods)
{
if(isWifiApEnabledmethod.getName().equals("isWifiApEnabled"))
{
while(!(Boolean)isWifiApEnabledmethod.invoke(mWifiManager)){
};
for(Method method1: wmMethods){
if(method1.getName().equals("getWifiApState")){
int apstate;
apstate=(Integer)method1.invoke(mWifiManager);
Toast.makeText(this, netConfig.SSID + String.valueOf(apstate), Toast.LENGTH_LONG).show();
}
}
}
}
if(apstatus)
{
System.out.println("success");
}else
{
System.out.println("failed");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
if(!methodFound){
}
}
I suspect your problem is that the device you are trying to connect with is expected to be supplied with an IP address etc, from the DHCP server in the Nexus device. Your code is just creating an access point. You probably need a static IP address on both ends of the link. That said, I'm not sure how that should be done, as I have more or less exactly the same problem myself.
See Unable to programatically create working Wi-Fi access point on Jellybean tablet
I have attempted to associate an IP address with the access point, and although I appear to have done that, I have still been unable to establish a connection so I only have a partial answer to your question I'm afraid.

Enabling USB tethering programmatically - there is an app that did it for 2.3

I've read many questions here on SO that ask how to enable USB tethering programmatically.
The answer is always the same, ordinary applications can't do it, only system apps.
Yet for 2.3 you could download an app in the market that would do it for you.
https://play.google.com/store/apps/details?id=org.tdtran.autousbtethering
On ICS (Android 4.0.3) it no longer works.
How did they do it for 2.3? Is it possible also for 4.0?
using the following code you can enable USB tethering. i didt test in 4.0.
public void switchOnTethering() {
Object obj = getSystemService(Context.CONNECTIVITY_SERVICE);
for (Method m : obj.getClass().getDeclaredMethods()) {
if (m.getName().equals("tether")) {
try {
m.invoke(obj, "usb0");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ICS and above: To execute the reflection method, the application would require the WRITE_SECURE_SETTINGS permission.
This is not available unless the phone is rooted.
since this is one of the most popular pages in the Google results for this topic I'd like to contribute my code which is checking the available interfaces. It does work on a Gingerbread phone I have, but not my Galaxy S3.
// DETECT INTERFACE NAME
Log.i("UsbTethering","Detecting tetherable usb interface.");
String[] available = null;
ConnectivityManager connMgr = (ConnectivityManager)connectivityServiceObject;
Method[] wmMethods = connMgr.getClass().getDeclaredMethods();
for(Method getMethod: wmMethods)
{
if(getMethod.getName().equals("getTetherableUsbRegexs"))
{
try
{
available = (String[]) getMethod.invoke(connMgr);
break;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
// DETECT INTERFACE NAME
if(available.length > 0)
{
for(String interfaceName : available)
{
Log.i("UsbTethering", "Detected " + String.valueOf(available.length) + " tetherable usb interfaces.");
Log.i("UsbTethering", "Trying to " + desiredString + " UsbTethering on interface " + interfaceName + "...");
Integer returnCode = (Integer)method.invoke(connectivityServiceObject, interfaceName);
if(returnCode == 0)
{
Log.i("UsbTethering", "UsbTethering " + desiredString + "d.");
return true;
}
else
{
Log.w("UsbTethering", "Failed to " + desiredString + "Usb Tethering. ReturnCode of method " + method.getName() + ": " + String.valueOf(returnCode));
}
}
}
On Samsumg Galaxy Tab 2 10.1 the interface isn't called "usb0" but "rndis0". Maybe that's the same for Galaxy SII
The port "rndis0" is enabled dynamically not availble in the the Tetherable interfaces list.
It gets added when the user selects the USB Tethering option in the menu.
The function ConnectivityManager::setUsbTethering(bool) is called when the option is selected. And this function call is allowed only for System applications.
I know its an old thread but i hope this could help some other people in the future ,
That code worked for me in Android 4.4(with root privilege)
code:
the trick is to use reflection , the method which changes the usb tethring is called "setUsbTethering"
I wont write the entire class but here is what you need:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Method usbTethering = connectivityManager.getClass().getMethod('setUsbTethering')
int returnCode = (Integer)usbTethering.invoke(connectivityManager, true);
0 = success
you can print the entire class methods using the following code
private static void printClassMethod(#NonNull Class aClazz) {
Method[] wmMethods = aClazz.getDeclaredMethods();
for (Method method : wmMethods) {
Log.i('anytag', method.getName());
}
}
then call printClassMethod(ConnectivityManager.class)
More Over, you can go onto the class itself and check the methods and arguments needed.
to get things work:
you must set your app as System app,
Declare in manifest : android:sharedUserId="android.uid.system"
add writing secure permission<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
Sign the apk using google certificate key(apk-with-system-privileges) and push it to /system/app
Mind, that usbTethering is turned on while USB is attached, so a nicer solution will be to register to both USB_STATE and USB_ATTACH and enable/disable it correspondingly in onReceive Method. (USB tethering is turned off automatically when USB is de-attached)

Connecting with embedded device over bluetooth? [duplicate]

I'm currently working on an Android application that connects to an instrument via Bluetooth and need to write string commands and receive string responses back. Currently I have the connect/read/write working for TCP/IP over Wi-Fi and now trying to implement Bluetooth. But I am running into some roadblocks. I have been searching the web trying to find examples of something similar and haven't had any luck. I have been using the Android developer resource example: Bluetooth Chat as my main reference point.
My current code seems to work.. Then it throws a Service Discovery Failed exception at the point of the connection. I am using the DeviceListActivity class to do the discovery and selecting of the device I want to connect to. It returns anActivityResult and then my Bluetooth class waits for it to handle that and then does the connect to it. The code beneath is almost identical to the Bluetooth Chat App.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!m_BluetoothAdapter.isEnabled())
{
m_BluetoothAdapter.enable();
}
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
}
else {
// User did not enable Bluetooth or an error occured
Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show();
finish();
}
}
}
This is my connect function:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private void connect(BluetoothDevice device) {
m_Device = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
}
catch (IOException e) {
}
m_Socket = tmp;
m_BluetoothAdapter.cancelDiscovery();
try {
// This is a blocking call and will only return on a
// successful connection or an exception
m_Socket.connect();
}
catch (IOException e) {
try {
m_Socket.close();
}
catch (IOException e2) {
}
return;
}
}
Hopefully, whatever I am doing wrong is simple, but I'm afraid it's never that easy. This is my first time doing any Bluetooth development, and maybe I'm doing something blatantly wrong... But I'm not sure why I get the service discovery failed exception.
You can pair/find the device at all times manually on the phone... It does require a passcode, but I don't think that is the problem that I am having.
After three days I got it figured out thanks to some very helpful posts.
I had to replace:
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
with:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
and voilĂ  it works!
As of API 15 you can use the following method:
Try replacing your UUID with the return value of getUuids() method of BluetoothDevice class.
What worked for me was something like this:
UUID uuid = bluetoothDevice.getUuids()[0].getUuid();
BluetoothSocket socket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
The reason this works is that different devices support different UUIDs and by getting the UUIDs of the device using getUuids you are supporting all features and devices.
Another interesting new method (supported since API 14) is this: BluetoothHealth.getConnectionState. Haven't tried it but looks promising...
This was a suggested edit from an anonymous user attempting to reply to the accepted answer.
One big difference between your before and after code is the UUID you are passing. I found my answer here: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createRfcommSocketToServiceRecord(java.util.UUID)
I had to replace:
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
with:
private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
tmp = device.createRfcommSocketToServiceRecord(SPP_UUID);
and voila it works!
The original code is for a peer to peer android app. It makes no sense to use the app UUID when connecting to a simple serial bluetooth device. Thats why discovery fails.
So as it mentioned above, the point is that you need to use the UUID that the server is waiting for.
If you are connecting to a bluetooth device, such as a headset or mouse, you need to check which UUIDs the device is listening for. You can see the UUIDs like this.
UUID[] uuids = bluetoothDevice.getUuids();
And if you want to know what these UUIDs mean, see this.
This is a realy old one question but i found that using the createInsecureRfcommSocketToServiceRecord() instead of createRfcommSocketToServiceRecord() along with the getUuids() previously mentioned do the trick for me
UUID uuid = bluetoothDevice.getUuids()[0].getUuid();
BluetoothSocket socket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);

Categories

Resources