Android: Ошибка Bluetooth при открытии серверного сокета - PullRequest
1 голос
/ 30 мая 2020

В настоящее время я работаю над созданием приложения Android с возможностями Bluetooth, и у меня возникли проблемы с установкой сокета сервера. Конечная цель в настоящее время - обмен строками между устройствами, а затем их передача обратно в основное действие. При попытке открыть сокет я постоянно получаю сообщение об ошибке:

W / System.err: java .io.IOException: ошибка чтения, сокет может быть закрыт или тайм-аут, прочтите ret: -1 в android .bluetooth.BluetoothSocket.readAll (BluetoothSocket. java: 741) в android .bluetooth.BluetoothSocket.readInt (BluetoothSocket. java: 753) W / System.err: в android .bluetooth. BluetoothSocket.connect (BluetoothSocket. java: 375) в com.example.main.BluetoothConnectionService $ ConnectThread.run (BluetoothConnectionService. java: 98)

Мне не удалось устранить эту проблему Я очень благодарен за любой совет, который вы можете мне дать! Стоит отметить, что я впервые работаю с Bluetooth в 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);
    }

}```
...