为什么Android无法与RN-41 Roving网络设备开放连接?

时间:2014-06-12 10:27:05

标签: android sockets bluetooth

我只是想用RN-41微芯片打开插座,据我所知,芯片一直在监听传入的连接,是可发现的等等。为什么socket总是直接关闭?

private class Connect extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public Connect(BluetoothDevice device) {

            BluetoothSocket tmp = null;
            mmDevice = device;
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("EB46DDA9-0D00-4C34-9365-D6AA6C111D1C"));
                Log.v("SOCKET SUCCESS", "HAST SOCKET"); 
            } catch (IOException e) { }
            mmSocket = tmp;
        }

        public void run() {


            try {

                mmSocket.connect();
                Log.v("SOCKET SUCCESS", "VERBUNDEN");
            } catch (IOException connectException) {

                Log.v("SOCKET SUCCESS", "KEINE VERBINDUNG");
                try {
                    mmSocket.close();
                    Log.v("SOCKET SUCCESS", "SOCKET CLOSED");
                } catch (IOException closeException) { 
                    Log.v("SOCKET SUCCESS", "SOCKET CLOSE FAIL");
                }
                return;
            }
        }

1 个答案:

答案 0 :(得分:0)

我一整天都在谷歌搜索,让事情奏效。不幸的是,我仍然不知道为什么以及如何工作,但它完美无缺。我改变了我的Connect类构造函数代码:

public Connect(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            //try {
                // MY_UUID is the app's UUID string, also used by the server code
                //ParcelUuid[] ids = device.getUuids();
                //UUID deviceID = ids[0].getUuid();
                //tmp = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));//deviceID);//UUID.fromString("EB46DDA9-0D00-4C34-9365-D6AA6C111D1C"));

                Method m = null;
                try {
                    m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
                try {
                    tmp = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Log.v("SOCKET SUCCESS", "HAST SOCKET");
            //} catch (IOException e) { }
                mmSocket = tmp;
        }

来源:

Android Bluetooth SPP with Galaxy S3

P.S。如果有人有时间解释上面的代码,我会非常感激。谢谢。