Bluetooth problem

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Bluetooth problem

Daniel Pels
Hello,


I have a problem connecting to a bluetooth device on my arduino(http://www.hobbycomponents.com/index.php?route=product/product&filter_name=bluetooth&product_id=116)


The problem is connecting to it with my phone(Alcatel OT-991)


I use the following code to try and connect to the bluetooth device but i get this exeption: service discovery failed.


bluetoothAdapter.CancelDiscovery();
ConnectionSock = item.CreateRfcommSocketToServiceRecord(SPP_UUID);
ConnectionSock.Connect();


After some searching on Google i found this:


> I'm trying to add support for a bluetooth device in one of our applications based on the code from the latest Bluetooth Chat example on GitHub. On every attempt to perform socket.Connect() I get the Java.IO.IOException "Service discovery failed".

I see you found the advice to call CancelDiscovery():

http://stackoverflow.com/a/8752294/83444

Now you probably need to try the "hack" solution:

http://stackoverflow.com/a/3397739/83444
> Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
> socket = (BluetoothSocket) m.invoke(device, 1);


Unfortunately that solution relies on Java reflection, which we don't currently expose. It thus needs to be translated to JNI:

IntPtr createRfcommSocket = JNIEnv.GetMethodID(device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(device.Handle, createRfcommSocket, new Android.Runtime.JValue(1));
socket = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);

- Jon

(http://lists.ximian.com/pipermail/monodroid/2012-September/012376.html)


So i tried that but then i had the problem of getting infinite pair request no matter how many i fill in it kept coming back.


now i am lost any one have a idea on how to make this work?

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|

Re: Bluetooth problem

Natanael
Same problem here.

Seems to be a android´s problem.

http://code.google.com/p/android/issues/detail?id=32545
Reply | Threaded
Open this post in threaded view
|

Re: Bluetooth problem

Daniel Pels
In reply to this post by Daniel Pels
I have been able to fix the problem by using this code:


IntPtr createRfcommSocket = JNIEnv.GetMethodID(item.Class.Handle, "createInsecureRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(item.Handle, createRfcommSocket, new Android.Runtime.JValue(1));
ConnectionSock = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);


It seems that it was impossible to start a secure connection so a insecure connection must be made.



Subject: [mono-android] Bluetooth problem

Hello,


I have a problem connecting to a bluetooth device on my arduino(http://www.hobbycomponents.com/index.php?route=product/product&filter_name=bluetooth&product_id=116)


The problem is connecting to it with my phone(Alcatel OT-991)


I use the following code to try and connect to the bluetooth device but i get this exeption: service discovery failed.


bluetoothAdapter.CancelDiscovery();
ConnectionSock = item.CreateRfcommSocketToServiceRecord(SPP_UUID);
ConnectionSock.Connect();


After some searching on Google i found this:


> I'm trying to add support for a bluetooth device in one of our applications based on the code from the latest Bluetooth Chat example on GitHub. On every attempt to perform socket.Connect() I get the Java.IO.IOException "Service discovery failed".

I see you found the advice to call CancelDiscovery():

http://stackoverflow.com/a/8752294/83444

Now you probably need to try the "hack" solution:

http://stackoverflow.com/a/3397739/83444
> Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
> socket = (BluetoothSocket) m.invoke(device, 1);


Unfortunately that solution relies on Java reflection, which we don't currently expose. It thus needs to be translated to JNI:

IntPtr createRfcommSocket = JNIEnv.GetMethodID(device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(device.Handle, createRfcommSocket, new Android.Runtime.JValue(1));
socket = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);

- Jon

(http://lists.ximian.com/pipermail/monodroid/2012-September/012376.html)


So i tried that but then i had the problem of getting infinite pair request no matter how many i fill in it kept coming back.


now i am lost any one have a idea on how to make this work?

_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|

Re: Bluetooth problem

najak3d
Thank you for this solution Daniel!  This is very unpublicized.

The key difference is the string:
"createInsecureRfcommSocket"

versus the string used in the standard publicized hack:
"createRfcommSocket"


Apparently some devices demand it to be "unsecured" else it fails with "permission Denied" exception.

So, just changing that string value did the trick!  Thank you.  Our code now tries both in succession, first trying the insecure method.

Thank you, thank you.  This one was a doozie for us.

To ensure my response is clear, here is the code that solved this issue for us:

IntPtr createRfcommSocket = JNIEnv.GetMethodID(item.Class.Handle, "createInsecureRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(item.Handle, createRfcommSocket, new Android.Runtime.JValue(1));
ConnectionSock = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);


Make special note of line #1 the "createInsecureRfcommSocket" vs. the "createRfcommSocket" string posted by others.