I am trying to communicate with a Bluetooth device. I have succeeded in outStream.Write(outBuf, 0, outBuf.Length); but int readLen = inStream.Read(inBuf, 0, inBuf.Length); never returns.
I am sure that the device has successfully received the bytes I sent and returned some bytes. I have written a Java version of this program and it works well, but now get problem on C# Xamarin.
Here is my code, please anybody help me:
BluetoothSocket btSocket = null;
BluetoothAdapter mBluetoothAdapter = null;
try{
mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
}
catch (Exception e){
}
if (mBluetoothAdapter == null){
return null;
}
ICollection<BluetoothDevice> boundSet = mBluetoothAdapter.BondedDevices;
BluetoothDevice btDevice1 = boundSet.ElementAt(0);
btSocket = btDevice1.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));
mBluetoothAdapter.CancelDiscovery();
try{
btSocket.Connect();
}
catch (Exception e)
{
return e.Message;
}
Stream outStream = btSocket.OutputStream;
Stream inStream = btSocket.InputStream;
//inStream.ReadTimeout = 5000;
byte[] outBuf = new byte[5] { 0xAC, 0x00, 0x00, 0x74, 0xAA };
byte[] inBuf = new byte[1];
outStream.Write(outBuf, 0, outBuf.Length);
int readLen = inStream.Read(inBuf, 0, inBuf.Length); //This line NEVER returns.
return string.Format("readLen{0} {1} readRet!", readLen, inBuf[0]);
Thank you very much.