別の電話機との Bluetooth 接続を確立しようとしています。後で、HC5 モジュールを搭載したボードになりますが、デバッグには電話を使用しています。
問題は、接続が失敗し、IO Exception をスローすることです: " read failed, socket might close or timeout, read ret: -1"
簡単なグーグル検索では、この問題を抱えていることがたくさんあります。私が解決できる唯一の方法は、APIで公開されていないメソッドを使用することです
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
問題は、それcreateRfcommSocket
が削除され、getMethod の結果が null になることです。
私のコードは例からのものです: https://github.com/xamarin/monodroid-samples/tree/master/BluetoothChat接続用:
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;
}
「元のハック」が機能せず、他の解決策が見つからないため、ここの誰かがこれを修正する方法を知っていることを願っています.
よろしくお願いします!