【问题标题】:Why cant Android open connection with RN-41 Roving network device?为什么Android无法打开与RN-41 Roving网络设备的连接?
【发布时间】:2014-08-02 15:27:00
【问题描述】:

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

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

【问题讨论】:

    标签: android sockets bluetooth


    【解决方案1】:

    我整天都在谷歌上搜索,一切顺利。不幸的是,我仍然不知道它为什么以及如何工作,但它完美地工作。我像这样更改了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

    附:如果有人有时间解释上面的代码,我将不胜感激。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      相关资源
      最近更新 更多