IOException : 읽기 실패, 소켓이 닫힐 수 있음-Android 4.3의 Bluetooth ArrayList<UUID>();

현재 Android 4.3 (Build JWR66Y, 두 번째 4.3 업데이트)을 사용하여 Nexus 7 (2012)에서 BluetoothSocket을 열 때 이상한 예외를 처리하려고합니다. 일부 관련 게시물 (예 : /programming/13648373/bluetoothsocket-connect-throwing-exception-read-failed )을 보았지만이 문제에 대한 해결 방법을 제공하는 것은 없습니다. 또한 이러한 스레드에서 제안했듯이 재 페어링은 도움이되지 않으며 멍청한 루프를 통해 지속적으로 연결을 시도해도 효과가 없습니다.

임베디드 장치 ( http://images04.olx.com/ui/15/53/76/1316534072_254254776_2-OBD-II-BLUTOOTH-ADAPTERSCLEAR-CHECK-ENGINE- 와 유사한 이름 없음 OBD-II 자동차 어댑터)를 다루고 있습니다. LIGHTS-WITH-YOUR-PHONE-Oceanside.jpg ). 내 Android 2.3.7 전화에는 연결 문제가 없으며 동료의 Xperia (Android 4.1.2)도 작동합니다. 다른 Google Nexus ( ‘One’또는 ‘S’인지 모르겠지만 ‘4’는 아님)도 Android 4.3에서 실패합니다.

다음은 연결 설정의 스 니펫입니다. 서비스 내에서 생성 된 자체 스레드에서 실행됩니다.

private class ConnectThread extends Thread {

    private static final UUID EMBEDDED_BOARD_SPP = UUID
        .fromString("00001101-0000-1000-8000-00805F9B34FB");

    private BluetoothAdapter adapter;
    private boolean secure;
    private BluetoothDevice device;
    private List<UUID> uuidCandidates;
    private int candidate;
    protected boolean started;

    public ConnectThread(BluetoothDevice device, boolean secure) {
        logger.info("initiliasing connection to device "+device.getName() +" / "+ device.getAddress());
        adapter = BluetoothAdapter.getDefaultAdapter();
        this.secure = secure;
        this.device = device;

        setName("BluetoothConnectThread");

        if (!startQueryingForUUIDs()) {
            this.uuidCandidates = Collections.singletonList(EMBEDDED_BOARD_SPP);
            this.start();
        } else{
            logger.info("Using UUID discovery mechanism.");
        }
        /*
         * it will start upon the broadcast receive otherwise
         */
    }

    private boolean startQueryingForUUIDs() {
        Class<?> cl = BluetoothDevice.class;

        Class<?>[] par = {};
        Method fetchUuidsWithSdpMethod;
        try {
            fetchUuidsWithSdpMethod = cl.getMethod("fetchUuidsWithSdp", par);
        } catch (NoSuchMethodException e) {
            logger.warn(e.getMessage());
            return false;
        }

        Object[] args = {};
        try {
            BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    BluetoothDevice deviceExtra = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
                    Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");

                    uuidCandidates = new ArrayList<UUID>();
                    for (Parcelable uuid : uuidExtra) {
                        uuidCandidates.add(UUID.fromString(uuid.toString()));
                    }

                    synchronized (ConnectThread.this) {
                        if (!ConnectThread.this.started) {
                            ConnectThread.this.start();
                            ConnectThread.this.started = true;
                            unregisterReceiver(this);
                        }

                    }
                }

            };
            registerReceiver(receiver, new IntentFilter("android.bleutooth.device.action.UUID"));
            registerReceiver(receiver, new IntentFilter("android.bluetooth.device.action.UUID"));

            fetchUuidsWithSdpMethod.invoke(device, args);
        } catch (IllegalArgumentException e) {
            logger.warn(e.getMessage());
            return false;
        } catch (IllegalAccessException e) {
            logger.warn(e.getMessage());
            return false;
        } catch (InvocationTargetException e) {
            logger.warn(e.getMessage());
            return false;
        }

        return true;
    }

    public void run() {
        boolean success = false;
        while (selectSocket()) {

            if (bluetoothSocket == null) {
                logger.warn("Socket is null! Cancelling!");
                deviceDisconnected();
                openTroubleshootingActivity(TroubleshootingActivity.BLUETOOTH_EXCEPTION);
            }

            // Always cancel discovery because it will slow down a connection
            adapter.cancelDiscovery();

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                bluetoothSocket.connect();
                success = true;
                break;

            } catch (IOException e) {
                // Close the socket
                try {
                    shutdownSocket();
                } catch (IOException e2) {
                    logger.warn(e2.getMessage(), e2);
                }
            }
        }

        if (success) {
            deviceConnected();
        } else {
            deviceDisconnected();
            openTroubleshootingActivity(TroubleshootingActivity.BLUETOOTH_EXCEPTION);
        }
    }

    private boolean selectSocket() {
        if (candidate >= uuidCandidates.size()) {
            return false;
        }

        BluetoothSocket tmp;
        UUID uuid = uuidCandidates.get(candidate++);
        logger.info("Attempting to connect to SDP "+ uuid);
        try {
            if (secure) {
                tmp = device.createRfcommSocketToServiceRecord(
                        uuid);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        uuid);
            }
            bluetoothSocket = tmp;
            return true;
        } catch (IOException e) {
            logger.warn(e.getMessage() ,e);
        }

        return false;
    }

}

에서 코드가 실패 bluetoothSocket.connect()합니다. 나는 java.io.IOException: read failed, socket might closed, read ret: -1. 이것은 GitHub의 해당 소스입니다 : https://github.com/android/platform_frameworks_base/blob/android-4.3_r2/core/java/android/bluetooth/BluetoothSocket.java#L504 https
에서 호출되는 readInt ()를 통해 호출됩니다. : //github.com/android/platform_frameworks_base/blob/android-4.3_r2/core/java/android/bluetooth/BluetoothSocket.java#L319

사용 된 소켓의 일부 메타 데이터 덤프로 인해 다음 정보가 생성되었습니다. Nexus 7과 2.3.7 휴대 전화에서 정확히 동일합니다.

Bluetooth Device 'OBDII'
Address: 11:22:33:DD:EE:FF
Bond state: 12 (bonded)
Type: 1
Class major version: 7936
Class minor version: 7936
Class Contents: 0
Contents: 0

다른 OBD-II 어댑터 (더 확장 성)가 있으며 모두 작동합니다. 내가 뭔가를 놓치고 있거나 이것이 Android의 버그 일 가능성이 있습니까?



답변

마침내 해결 방법을 찾았습니다. 마법은 BluetoothDevice클래스 내부에 숨겨져 있습니다 ( https://github.com/android/platform_frameworks_base/blob/android-4.3_r2/core/java/android/bluetooth/BluetoothDevice.java#L1037 참조 ).

이제 예외를 받으면 BluetoothSocket아래 소스 코드와 유사한 fallback을 인스턴스화 합니다. 보시다시피 createRfcommSocket반사를 통해 숨겨진 메서드 를 호출합니다 . 이 방법이 왜 숨겨져 있는지 전혀 모르겠습니다. 소스 코드는 그것을 public마치 …

Class<?> clazz = tmp.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};

Method m = clazz.getMethod("createRfcommSocket", paramTypes);
Object[] params = new Object[] {Integer.valueOf(1)};

fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
fallbackSocket.connect();

connect()그러면 더 이상 실패하지 않습니다. 여전히 몇 가지 문제가 발생했습니다. 기본적으로 이것은 때때로 차단되고 실패합니다. SPP- 장치를 재부팅 (플러그 오프 / 플러그인)하면 이러한 경우에 도움이됩니다. 때때로 connect()장치가 이미 연결되어있는 경우에도 다른 페어링 요청을받습니다 .

최신 정보:

다음은 일부 중첩 된 클래스를 포함하는 완전한 클래스입니다. 실제 구현을 위해 이들은 별도의 클래스로 보유 할 수 있습니다.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class BluetoothConnector {

    private BluetoothSocketWrapper bluetoothSocket;
    private BluetoothDevice device;
    private boolean secure;
    private BluetoothAdapter adapter;
    private List<UUID> uuidCandidates;
    private int candidate;


    /**
     * @param device the device
     * @param secure if connection should be done via a secure socket
     * @param adapter the Android BT adapter
     * @param uuidCandidates a list of UUIDs. if null or empty, the Serial PP id is used
     */
    public BluetoothConnector(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
            List<UUID> uuidCandidates) {
        this.device = device;
        this.secure = secure;
        this.adapter = adapter;
        this.uuidCandidates = uuidCandidates;

        if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
            this.uuidCandidates = new ArrayList<UUID>();
            this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        }
    }

    public BluetoothSocketWrapper connect() throws IOException {
        boolean success = false;
        while (selectSocket()) {
            adapter.cancelDiscovery();

            try {
                bluetoothSocket.connect();
                success = true;
                break;
            } catch (IOException e) {
                //try the fallback
                try {
                    bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket());
                    Thread.sleep(500);
                    bluetoothSocket.connect();
                    success = true;
                    break;
                } catch (FallbackException e1) {
                    Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
                } catch (InterruptedException e1) {
                    Log.w("BT", e1.getMessage(), e1);
                } catch (IOException e1) {
                    Log.w("BT", "Fallback failed. Cancelling.", e1);
                }
            }
        }

        if (!success) {
            throw new IOException("Could not connect to device: "+ device.getAddress());
        }

        return bluetoothSocket;
    }

    private boolean selectSocket() throws IOException {
        if (candidate >= uuidCandidates.size()) {
            return false;
        }

        BluetoothSocket tmp;
        UUID uuid = uuidCandidates.get(candidate++);

        Log.i("BT", "Attempting to connect to Protocol: "+ uuid);
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(uuid);
        } else {
            tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
        }
        bluetoothSocket = new NativeBluetoothSocket(tmp);

        return true;
    }

    public static interface BluetoothSocketWrapper {

        InputStream getInputStream() throws IOException;

        OutputStream getOutputStream() throws IOException;

        String getRemoteDeviceName();

        void connect() throws IOException;

        String getRemoteDeviceAddress();

        void close() throws IOException;

        BluetoothSocket getUnderlyingSocket();

    }


    public static class NativeBluetoothSocket implements BluetoothSocketWrapper {

        private BluetoothSocket socket;

        public NativeBluetoothSocket(BluetoothSocket tmp) {
            this.socket = tmp;
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return socket.getInputStream();
        }

        @Override
        public OutputStream getOutputStream() throws IOException {
            return socket.getOutputStream();
        }

        @Override
        public String getRemoteDeviceName() {
            return socket.getRemoteDevice().getName();
        }

        @Override
        public void connect() throws IOException {
            socket.connect();
        }

        @Override
        public String getRemoteDeviceAddress() {
            return socket.getRemoteDevice().getAddress();
        }

        @Override
        public void close() throws IOException {
            socket.close();
        }

        @Override
        public BluetoothSocket getUnderlyingSocket() {
            return socket;
        }

    }

    public class FallbackBluetoothSocket extends NativeBluetoothSocket {

        private BluetoothSocket fallbackSocket;

        public FallbackBluetoothSocket(BluetoothSocket tmp) throws FallbackException {
            super(tmp);
            try
            {
              Class<?> clazz = tmp.getRemoteDevice().getClass();
              Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
              Method m = clazz.getMethod("createRfcommSocket", paramTypes);
              Object[] params = new Object[] {Integer.valueOf(1)};
              fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
            }
            catch (Exception e)
            {
                throw new FallbackException(e);
            }
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return fallbackSocket.getInputStream();
        }

        @Override
        public OutputStream getOutputStream() throws IOException {
            return fallbackSocket.getOutputStream();
        }


        @Override
        public void connect() throws IOException {
            fallbackSocket.connect();
        }


        @Override
        public void close() throws IOException {
            fallbackSocket.close();
        }

    }

    public static class FallbackException extends Exception {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public FallbackException(Exception e) {
            super(e);
        }

    }
}


답변

글쎄, 나는 내 코드에 동일한 문제가 있었고 그것은 안드로이드 4.2 블루투스 스택이 변경 되었기 때문입니다. 그래서 내 코드는 android <4.2 인 장치에서 잘 실행되고 있었고 다른 장치에서는 “읽기 실패, 소켓이 닫혔거나 시간 초과, 읽기 ret : -1″라는 유명한 예외가 발생 했습니다.

socket.mPort매개 변수에 문제가 있습니다. 당신이 사용하여 소켓을 만드는 경우 socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);는이 mPort값의 정수 “얻는다 -1 “, 및 “로 설정해야합니다 있도록이 값은 4.2> = 안드로이드에 대한 작업을하지 않는 것 같다 (1) “. 나쁜 소식은 createRfcommSocketToServiceRecordUUID 만 매개 변수로 받아들이고 mPort다른 aproach를 사용해야한다는 것입니다. @matthes가 게시 한 답변 도 저에게 효과적 이지만 단순화했습니다 socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);.. 두 번째 소켓 속성을 폴백으로 사용해야합니다.

따라서 코드는 다음과 같습니다 (ELM327 장치의 SPP에 연결).

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

    if (btAdapter.isEnabled()) {
        SharedPreferences prefs_btdev = getSharedPreferences("btdev", 0);
        String btdevaddr=prefs_btdev.getString("btdevaddr","?");

        if (btdevaddr != "?")
        {
            BluetoothDevice device = btAdapter.getRemoteDevice(btdevaddr);

            UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // bluetooth serial port service
            //UUID SERIAL_UUID = device.getUuids()[0].getUuid(); //if you don't know the UUID of the bluetooth device service, you can get it like this from android cache

            BluetoothSocket socket = null;

            try {
                socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
            } catch (Exception e) {Log.e("","Error creating socket");}

            try {
                socket.connect();
                Log.e("","Connected");
            } catch (IOException e) {
                Log.e("",e.getMessage());
                try {
                    Log.e("","trying fallback...");

                    socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
                    socket.connect();

                    Log.e("","Connected");
                }
             catch (Exception e2) {
                 Log.e("", "Couldn't establish Bluetooth connection!");
              }
            }
        }
        else
        {
            Log.e("","BT device not selected");
        }
    }


답변

먼저 블루투스 2.x 장치와 통신해야하는 경우이 문서에 다음과 같이 명시되어 있습니다.

힌트 : Bluetooth 직렬 보드에 연결하는 경우 잘 알려진 SPP UUID 00001101-0000-1000-8000-00805F9B34FB 를 사용해보십시오 . 그러나 Android 피어에 연결하는 경우 고유 한 UUID를 생성하십시오.

나는 그것이 작동 할 것이라고 생각하지 않았지만 UUID를 00001101-0000-1000-8000-00805F9B34FB그것 으로 교체 해야만 작동합니다. 그러나이 코드는 SDK 버전 문제를 처리하는 것으로 보이며 다음과 같은 메소드를 정의한 후 함수 device.createRfcommSocketToServiceRecord(mMyUuid);tmp = createBluetoothSocket(mmDevice);다음으로 대체 할 수 있습니다 .

private BluetoothSocket createBluetoothSocket(BluetoothDevice device)
    throws IOException {
    if(Build.VERSION.SDK_INT >= 10){
        try {
            final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
            return (BluetoothSocket) m.invoke(device, mMyUuid);
        } catch (Exception e) {
            Log.e(TAG, "Could not create Insecure RFComm Connection",e);
        }
    }
    return  device.createRfcommSocketToServiceRecord(mMyUuid);
}

소스 코드는 내 것이 아니지만 이 웹 사이트 에서 가져온 입니다.


답변

여기에 설명 된 것과 같은 증상이있었습니다. 블루투스 프린터에 한 번 연결할 수 있었지만 이후 연결은 내가 무엇을하더라도 “소켓이 닫힘”으로 실패했습니다.

여기에 설명 된 해결 방법이 필요하다는 것이 조금 이상하다는 것을 알았습니다. 내 코드를 살펴본 후 소켓의 InputStream 및 OutputSteram을 닫는 것을 잊고 ConnectedThreads를 제대로 종료하지 않았 음을 발견했습니다.

내가 사용하는 ConnectedThread는 여기 예제와 동일합니다.

http://developer.android.com/guide/topics/connectivity/bluetooth.html

ConnectThread와 ConnectedThread는 서로 다른 두 클래스입니다.

ConnectedThread를 시작하는 모든 클래스는 스레드에서 interrupt () 및 cancel ()을 호출해야합니다. ConnectedTread.cancel () 메서드에 mmInStream.close () 및 mmOutStream.close ()를 추가했습니다.

스레드 / 스트림 / 소켓을 제대로 닫은 후 문제없이 새 소켓을 만들 수 있습니다.


답변

글쎄, 나는 실제로 문제를 발견했습니다.

를 사용하여 연결을 시도하는 대부분의 사람들은 socket.Connect();라는 예외 를 받습니다 Java.IO.IOException: read failed, socket might closed, read ret: -1.

경우에 따라 Bluetooth 장치에 따라 달라집니다. BLE (저에너지)와 클래식이라는 두 가지 유형의 Bluetooth가 있기 때문입니다.

Bluetooth 장치 유형을 확인하려면 다음 코드를 참조하십시오.

        String checkType;
        var listDevices = BluetoothAdapter.BondedDevices;
        if (listDevices.Count > 0)
        {
            foreach (var btDevice in listDevices)
            {
                if(btDevice.Name == "MOCUTE-032_B52-CA7E")
                {
                    checkType = btDevice.Type.ToString();
                    Console.WriteLine(checkType);
                }
            }
        }

며칠 동안 문제를 해결하려고 노력했지만 오늘부터 문제를 발견했습니다. @matthes의 솔루션에는 그가 이미 말했듯이 불행히도 여전히 몇 가지 문제가 있지만 여기에 내 솔루션이 있습니다.

현재 Xamarin Android에서 작업하지만 다른 플랫폼에서도 작동합니다.

해결책

페어링 된 장치가 두 개 이상인 경우 페어링 된 다른 장치를 제거해야합니다. 따라서 연결하려는 하나만 유지하십시오 (오른쪽 이미지 참조).

여기에 이미지 설명 입력
여기에 이미지 설명 입력

왼쪽 이미지에는 “MOCUTE-032_B52-CA7E”와 “Blue Easy”라는 두 개의 페어링 된 장치가 있습니다. 그게 문제지만 왜 그 문제가 발생하는지 모르겠습니다. Bluetooth 프로토콜이 다른 Bluetooth 장치에서 정보를 얻으려고 할 수 있습니다.

그러나 socket.Connect();지금은 아무 문제없이 훌륭하게 작동합니다. 그래서 저는 이것을 공유하고 싶었습니다. 그 오류가 정말 짜증나 기 때문입니다.

행운을 빕니다!


답변

최신 버전의 Android에서 소켓에 연결하려고 할 때 어댑터가 여전히 발견 중이기 때문에이 오류가 발생했습니다. Bluetooth 어댑터에서 cancelDiscovery 메서드를 호출했지만 BluetoothAdapter.ACTION_DISCOVERY_FINISHED 작업으로 BroadcastReceiver의 onReceive () 메서드에 대한 콜백이 호출 될 때까지 기다려야했습니다.

어댑터가 검색을 중지 할 때까지 기다린 후 소켓의 연결 호출이 성공했습니다.


답변

당신은
registerReceiver(receiver, new IntentFilter("android.bleutooth.device.action.UUID"));
“bluetooth”철자가 “bleutooth”로 넣습니다.