gpt4 book ai didi

java - Android 蓝牙套接字 IOException : 'read failed, socket might be closed or timeout'

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:47 26 4
gpt4 key购买 nike

我有一个蓝牙 Remote (我想是自拍杆)通过蓝牙与我的手机连接。它可以工作,它可以拍照,但我想在我的应用程序中接收来自 Remote 的信号来远程控制音乐。

问题:创建套接字后,如果没有 IOException,我无法连接到它。

这是所有代码:

public class MainActivity extends Activity {

BluetoothAdapter btAdapter;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
IntentFilter filter;
String tag = "debugging";
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
connectedThread.write(s.getBytes());
Log.i(tag, "connected");
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
Toast.makeText(getApplicationContext(), string, 0).show();
break;
}
}
};
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try{
btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = getDeviceByName("AB Shutter 3");

ConnectThread connect = new ConnectThread(device);
connect.start();


} catch (Exception e) {
e.printStackTrace();

}

}

private BluetoothDevice getDeviceByName(String printerName)
{
Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();

BluetoothDevice remoteDevice;

for (BluetoothDevice device : pairedDevices)
{
if (device.getName() == null)
continue;
if (device.getName().contains(printerName))
{
Log.e("","device name: "+device.getName());
remoteDevice = device;
return remoteDevice;
}
}
return null;
}
private class ConnectThread extends Thread {

private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
Log.i(tag, "construct");
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

} catch (IOException e) {
Log.i(tag, "get socket failed");
e.printStackTrace();

}
mmSocket = tmp;
}

public void run() {
try {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");

mmSocket.connect();
Log.i(tag, "connect - succeeded");
} catch (Exception connectException) {
Log.i(tag, "connect failed");
// Unable to connect; close the socket and get out
connectException.printStackTrace();
return;
}

// Do work to manage the connection (in a separate thread)

mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}

}

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()

// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();

} catch (Exception e) {
e.printStackTrace();
break;
}
}
}

public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
}
}

错误出现在第 131 行,ConnectThread run()

    try {   
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");

mmSocket.connect(); //Here it fails
Log.i(tag, "connect - succeeded");
} catch (Exception connectException) {
Log.i(tag, "connect failed");
// Unable to connect; close the socket and get out
connectException.printStackTrace();
return;
}

错误显示: java.io.IOException: read failed, socket might be closed or timeout

有人以前遇到过这个错误并能指出我正确的方向吗?

最佳答案

这不是错误,而是警告。警告发生后您是否会失去某种功能?如果没有,听起来你无法控制它,但根据这个答案中的几个人的说法,也不需要担心它:https://stackoverflow.com/a/24229604/3299157

关于java - Android 蓝牙套接字 IOException : 'read failed, socket might be closed or timeout' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35758038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com