Xamarin Android Bluetooth Socket Connection fails

2019-08-05 06:32发布

问题:

I am trying to create a sample Xamarin Android app to connect devices over bluetooth.

I get below error when i try to connect the bluetooth socket

  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3819/96c7ba6c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00084] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11643 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00017] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:15 
  at Android.Bluetooth.BluetoothSocket.Connect () [0x00000] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Bluetooth.BluetoothSocket.cs:141 
  at Bluetooth.Droid.Activities.Bluetooth.BluetoothActivity.PairedDevicesListView_ItemClick (System.Object sender, Android.Widget.AdapterView+ItemClickEventArgs e) [0x00094] in C:\Git\Bluetooth\Bluetooth\Bluetooth.Droid\Activities\Bluetooth\BluetoothActivity.cs:92 
  --- End of managed Java.IO.IOException stack trace ---
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
    at mono.android.widget.AdapterView_OnItemClickListenerImplementor.n_onItemClick(Native Method)
    at mono.android.widget.AdapterView_OnItemClickListenerImplementor.onItemClick(AdapterView_OnItemClickListenerImplementor.java:30)
    at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
    at android.widget.AbsListView$3.run(AbsListView.java:3860)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

You can find my sample at https://github.com/sstechGitHost/Bluetooth.git

Thanks, Nahid

回答1:

The problem that you are pairing with UUID which is not supported on another device. Here is the sample code to solve this. Please keep in mind this solution might require some robustness, this is just proof of concept. People says that FetchUuidsWithSdp is not very stable, so you might want to register for broadcast action to get UUIDs or something like that. Now the code. Instead of

BluetoothSocket btSocket = btDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));

I used

ParcelUuid[] uuids = null;
                if (btDevice.FetchUuidsWithSdp())
                {
                    uuids = btDevice.GetUuids();
                }
                if ((uuids != null) && (uuids.Length > 0))
                {
                    foreach (var uuid in uuids)
                    {
                        try
                        {
                            btSocket = btDevice.CreateRfcommSocketToServiceRecord(uuid.Uuid);
                            btSocket.Connect();
                            break;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ex: " + ex.Message);
                        }
                    }
                }

and was able to connect to another device with UUIDs

0000111f-0000-1000-8000-00805f9b34fb    
00001132-0000-1000-8000-00805f9b34fb    
00000000-deca-fade-deca-deafdecacafe

but not " Serial" you were using.