Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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
Java Android:蓝牙无法打开服务器套接字 我目前正在构建一个具有蓝牙功能的Android应用程序,并且有一些问题需要建立一个服务器套接字。当前的最终目标是在设备之间交换字符串,然后将这些字符串传递回主活动。尝试打开套接字时,我不断收到错误:_Java_Android_Bluetooth_Android Bluetooth - Fatal编程技术网

Java Android:蓝牙无法打开服务器套接字 我目前正在构建一个具有蓝牙功能的Android应用程序,并且有一些问题需要建立一个服务器套接字。当前的最终目标是在设备之间交换字符串,然后将这些字符串传递回主活动。尝试打开套接字时,我不断收到错误:

Java Android:蓝牙无法打开服务器套接字 我目前正在构建一个具有蓝牙功能的Android应用程序,并且有一些问题需要建立一个服务器套接字。当前的最终目标是在设备之间交换字符串,然后将这些字符串传递回主活动。尝试打开套接字时,我不断收到错误:,java,android,bluetooth,android-bluetooth,Java,Android,Bluetooth,Android Bluetooth,W/System.err:java.io.IOException:read失败,套接字可能关闭或超时,read ret:-1 位于android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:741) 位于android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:753) W/System.err:at-android.bluetooth.BluetoothSock

W/System.err:java.io.IOException:read失败,套接字可能关闭或超时,read ret:-1 位于android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:741) 位于android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:753) W/System.err:at-android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:375) 位于com.example.main.BluetoothConnectionService$ConnectThread.run(BluetoothConnectionService.java:98)

我自己也没能解决这个问题,非常感谢你能给我的任何建议!值得一提的是,这是我第一次在安卓系统中使用蓝牙

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);
    }

}```