英文:
Android: Bluetooth Trouble opening server socket
问题
我目前正在构建一个具有蓝牙功能的Android应用程序,但在建立服务器套接字方面遇到了一些问题。目前的最终目标是在设备之间交换字符串,然后将其传递回主活动。当尝试打开套接字时,我不断收到以下错误:
W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:741)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:753)
W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:375)
at com.example.main.BluetoothConnectionService$ConnectThread.run(BluetoothConnectionService.java:98)
我自己尝试了解决这个问题,但没有成功,如果你能给我一些建议,我将不胜感激!可能值得提到的是,这是我第一次在Android中使用蓝牙...
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.UUID;
public class BluetoothConnectionService {
// ... 其他代码 ...
private class AcceptThread extends Thread {
// ... 其他代码 ...
public void run() {
// ... 其他代码 ...
try {
socket = mmServerSocket.accept();
Log.d(TAG, "run: RFCOM server socket accepted connection.");
} catch (IOException e) {
Log.e(TAG, "RunThread: IOException: " + e.getMessage());
}
if(socket != null){
connected(socket, mmDevice);
}
}
// ... 其他代码 ...
}
private class ConnectThread extends Thread {
// ... 其他代码 ...
public void run() {
// ... 其他代码 ...
try {
mmSocket.connect();
Log.d(TAG, "run: Socket connected");
} catch (IOException e) {
e.printStackTrace();
try {
mmSocket.close();
Log.d(TAG, "run: Closed Socket");
} catch (IOException ex) {
Log.e(TAG, "Socket Connection failed to close.");
}
}
connected(mmSocket, mmDevice);
}
// ... 其他代码 ...
}
private class ConnectedThread extends Thread {
// ... 其他代码 ...
public void run() {
// ... 其他代码 ...
while(true) {
try {
bytes = mmInStream.read(buffer);
String incomingMessage = new String(buffer, 0 ,bytes);
Log.d(TAG, "Input Stream" + incomingMessage);
Intent incomingMessageIntent = new Intent("incomingMessage");
incomingMessageIntent.putExtra("themessage", incomingMessage);
LocalBroadcastManager.getInstance(mcontext).sendBroadcast(incomingMessageIntent);
} catch (IOException e) {
Log.e(TAG, "write: Error writing to inputstream" + e.getMessage());
break;
}
}
}
// ... 其他代码 ...
}
// ... 其他代码 ...
}
请注意,这只是你提供的代码片段的翻译部分,我已经省略了未翻译的部分。如果您需要更多帮助或解释,请随时提问。
英文:
I am currently in the middle of building an Android app with bluetooth capabilties and am having some issue establishing a server socket. End goal currently is to exchange strings between devices and then pass those back to the main activity. When attempting to open a socket I continually get the error:
> W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:741)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:753)
W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:375)
at com.example.main.BluetoothConnectionService$ConnectThread.run(BluetoothConnectionService.java:98)
I've had no luck troubleshooting this myself, any advice you may be able to give me is appreciated! It might be worth mentioning that this is my first time working with Bluetooth in android...
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.UUID;
public class BluetoothConnectionService {
private static final String TAG = "BluetoothConnectionService";
private static final String appName = "Bluetooth App";
private static final UUID uuid = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
private final BluetoothAdapter mBluetoothAdapter;
private Context mcontext;
private AcceptThread mInsecureAcceptThread;
private ConnectThread mConnectThread;
private BluetoothDevice mmDevice;
private UUID deviceUUID;
private ConnectedThread mConnectedThread;
public BluetoothConnectionService(Context context){
this.mcontext = context;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
start();
}
private class AcceptThread extends Thread{
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket temp = null;
try {
temp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(appName, uuid);
Log.d(TAG,"AcceptThread: Setting up Server using: " + uuid);
} catch (IOException e) {
Log.e(TAG,"AcceptThread: IOException: " + e.getMessage());
}
mmServerSocket = temp;
}
public void run(){
Log.d(TAG,"run: AcceptThread Running.");
BluetoothSocket socket = null;
try {
socket = mmServerSocket.accept();
Log.d(TAG, "run: RFCOM server socket accepted connection.");
} catch (IOException e) {
Log.e(TAG,"RunThread: IOException: " + e.getMessage());
}
if(socket != null){
connected(socket,mmDevice);
}
}
public void cancel(){
try{
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG,"cancel: Close of AcceptThread ServerSocket failed. " + e.getMessage());
}
}
}
private class ConnectThread extends Thread{
private BluetoothSocket mmSocket;
public ConnectThread(BluetoothDevice device,UUID uuid){
mmDevice = device;
deviceUUID = uuid;
}
public void run(){
BluetoothSocket tmp = null;
Log.i(TAG,"Run mConnectThread");
try {
tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(deviceUUID);
} catch (IOException e) {
Log.e(TAG,"could not create insecureRFcommSocket" + e.getMessage());
}
mmSocket = tmp;
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
Log.d(TAG,"run: Socket connected");
} catch (IOException e) {
e.printStackTrace();
try{
mmSocket.close();
Log.d(TAG,"run: Closed Socket");
} catch (IOException ex) {
Log.e(TAG,"Socket Connection failed to close.");
}
}
connected(mmSocket,mmDevice);
}
public void cancel(){
try{
Log.d(TAG,"cancel: Closing Client Socket");
mmSocket.close();
} catch (IOException e) {
Log.e(TAG,"cancel: close() of mmSocket in ConnectThread failed. " + e.getMessage());
}
}
}
public synchronized void start(){
Log.d(TAG,"start");
if(mConnectThread != null){
mConnectThread.cancel();
mConnectThread = null;
}
if(mInsecureAcceptThread == null){
mInsecureAcceptThread = new AcceptThread();
mInsecureAcceptThread.start();
}
}
public void startclient(BluetoothDevice device, UUID uuid){
Log.d(TAG, "startclient: Started");
Log.d(TAG,"Connecting Bluetooth");
mConnectThread = new ConnectThread(device,uuid);
mConnectThread.start();
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "ConnectedTrhead: Starting");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
Log.d(TAG,"Bluetooth Connected");
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
this.mmInStream = tmpIn;
this.mmOutStream = tmpOut;
}
public void run(){
byte[] buffer = new byte[1024];
int bytes;
while(true){
try {
bytes = mmInStream.read(buffer);
String incomingMessage = new String(buffer, 0 ,bytes);
Log.d(TAG,"Input Stream" + incomingMessage);
Intent incomingMessageIntent = new Intent("incomingMessage");
incomingMessageIntent.putExtra("themessage",incomingMessage);
LocalBroadcastManager.getInstance(mcontext).sendBroadcast(incomingMessageIntent);
} catch (IOException e) {
Log.e(TAG,"write: Error writing to inputstream" + e.getMessage());
break;
}
}
}
public void write(byte[] bytes){
String text = new String(bytes, Charset.defaultCharset());
try {
mmOutStream.write(bytes);
} catch (IOException e) {
Log.e(TAG,"write: Error writing to outputstream" + e.getMessage());
}
}
public void cancel(){
try{
mmSocket.close();
} catch (IOException e) {
Log.e(TAG,"Socket should not be closed");
}
}
}
private void connected(BluetoothSocket mmSocket, BluetoothDevice mmDevice){
Log.d(TAG,"connected: Starting.");
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
}
public void write(byte[] out){
ConnectedThread r;
mConnectedThread.write(out);
}
}```
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论