Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android连接中的Xamarin蓝牙失败_Android_Xamarin_Bluetooth_Xamarin.droid - Fatal编程技术网

Android连接中的Xamarin蓝牙失败

Android连接中的Xamarin蓝牙失败,android,xamarin,bluetooth,xamarin.droid,Android,Xamarin,Bluetooth,Xamarin.droid,我正在尝试与另一个电话建立蓝牙连接。稍后,它将是一个带有HC5模块的板,但为了调试,我只使用了一个电话 问题是,连接失败并引发IO异常: 读取失败,套接字可能已关闭或超时,读取重试:-1 快速的谷歌搜索显示很多人都有这个问题。我认为解决这个问题的唯一方法是使用API中不公开的方法 Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); mmSocket = (Bluetoot

我正在尝试与另一个电话建立蓝牙连接。稍后,它将是一个带有HC5模块的板,但为了调试,我只使用了一个电话

问题是,连接失败并引发IO异常: 读取失败,套接字可能已关闭或超时,读取重试:-1

快速的谷歌搜索显示很多人都有这个问题。我认为解决这个问题的唯一方法是使用API中不公开的方法

Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
问题是,
createRfcommSocket
已被删除,getMethod的结果将为null

我的代码来自示例:用于连接的代码是:

        public ConnectThread(BluetoothDevice device, BluetoothChatService service)
        {
            UUID MY_UUID = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");
            mmDevice = device;
            _service = service;
            BluetoothSocket tmp = null;

            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try
            {
                if ((int)Android.OS.Build.VERSION.SdkInt >= 10) // Gingerbread 2.3.3 2.3.4
                    tmp = device.CreateInsecureRfcommSocketToServiceRecord(MY_UUID);
                else
                    tmp = device.CreateRfcommSocketToServiceRecord(MY_UUID);


            }
            catch (Java.IO.IOException e)
            {
                Log.Error(TAG, "create() failed", e);
            }
            mmSocket = tmp;
        }
由于“原始黑客”不起作用,而且我还没有找到任何其他解决方案,我希望这里的人知道如何解决这个问题


致以最良好的祝愿

这是我和我公司的其他人在使用安卓手机的蓝牙设备时遇到的问题。这里对其进行了详细描述:

事实上,您描述为被删除的方法仍然是可操作的。这是我们成功使用的一个。我们尝试使用

tmp = device.CreateRfcommSocketToServiceRecord(MY_UUID);
您在上面显示的方法。 在tmp.Connect()周围使用try…catch块(您的代码不显示Connect调用) 在catch块中,使用createRfcommSocket方法“重新创建”BluetoothSocket。我使用了一个小方法来实现这一点:

       private BluetoothSocket CreateRfcommSocket(BluetoothDevice bTdevice)
    { // This is an "undocumented" call that is needed to (mostly) avoid a Bluetooth Connection error
      // introduced in Android v4.2 and higher. It is used as a "fallback" connection.
      // Full paths version of code!
      //Java.Lang.Reflect.Method mi = device.Class.GetMethod("createRfcommSocket", new Java.Lang.Class[] { Java.Lang.Integer.Type });
      //_bluetoothSocket = (BluetoothSocket)mi.Invoke(device, 1);

        // Compact version of above
        var mi = bTdevice.Class.GetMethod("createRfcommSocket", Integer.Type);
        return (BluetoothSocket)mi.Invoke(bTdevice, 1);
    }
这是此处指示的方法:

我已经在安卓4.4.2和安卓8.0上进行了测试,它可以在这些系统上运行。该公司的另一个人已经在Android上测试了Java等效代码:4.2.2、4.4.2、7.0和8.0,并且在任何情况下都能工作