Java Android蓝牙通信不工作

Java Android蓝牙通信不工作,java,android,bluetooth,chat,Java,Android,Bluetooth,Chat,问候 我有Micromax A110安卓操作系统4.0.4和三星galaxy pop安卓操作系统2.2.1 从sdk下载bluetoothchat示例并尝试运行它 当我试图从micromax连接到三星时,它抛出异常消息e.getMessage 连接:未创建连接失败或中止 当我尝试从三星连接到micromax时,它说 "Service discovery failed" 现在我将UUID更改为 私有静态最终UUID MY_UUID=UUID.fromStringFafa-afac-11de-8a

问候

我有Micromax A110安卓操作系统4.0.4和三星galaxy pop安卓操作系统2.2.1

从sdk下载bluetoothchat示例并尝试运行它

当我试图从micromax连接到三星时,它抛出异常消息e.getMessage 连接:未创建连接失败或中止

当我尝试从三星连接到micromax时,它说

"Service discovery failed"
现在我将UUID更改为 私有静态最终UUID MY_UUID=UUID.fromStringFafa-afac-11de-8a39-0800200c9a66

现在micromax向三星发送相同的异常消息 连接:未创建连接失败或中止。 但三星表示,将向micromax进军 拒绝连接

然后我换了线

 tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

根据stackoverflow中的信息

现在 现在micromax向三星发送相同的异常消息 连接:未创建连接失败或中止

但三星向micromax表示 已连接,当我键入一些消息时,它会发送到micromax

但micromax端未播放消息,仍在等待客户端连接 socket=mmServerSocket.accept

我不明白发生了什么事。有趣的是,它和以前的工作原理是一样的 大约一年前。不知道这些手机上发生了什么软件升级 请告诉我可能有什么问题

来自示例的代码
通过micromax A110的出厂重置解决了此问题

通过micromax画布的出厂重置解决了此问题
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                Method m;
                m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                tmp = (BluetoothSocket) m.invoke(device, 1);
private class AcceptThread extends Thread {
        // The local server socket
        private final BluetoothServerSocket mmServerSocket;

        public AcceptThread() {
            BluetoothServerSocket tmp = null;

            // Create a new listening server socket
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "listen() failed", e);
            }
            mmServerSocket = tmp;
        }

        public void run() {
            if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
            setName("AcceptThread");
            BluetoothSocket socket = null;

            // Listen to the server socket if we're not connected
            while (mState != STATE_CONNECTED) {
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    socket = mmServerSocket.accept();
                } catch (IOException e) {
                    Log.e(TAG, "accept() failed", e);
                    break;
                }

                // If a connection was accepted
                if (socket != null) {
                    synchronized (BluetoothChatService.this) {
                        switch (mState) {
                        case STATE_LISTEN:
                        case STATE_CONNECTING:
                            // Situation normal. Start the connected thread.
                            connected(socket, socket.getRemoteDevice());
                            break;
                        case STATE_NONE:
                        case STATE_CONNECTED:
                            // Either not ready or already connected. Terminate new socket.
                            try {
                                socket.close();
                            } catch (IOException e) {
                                Log.e(TAG, "Could not close unwanted socket", e);
                            }
                            break;
                        }
                    }
                }
            }
            if (D) Log.i(TAG, "END mAcceptThread");
        }

        public void cancel() {
            if (D) Log.d(TAG, "cancel " + this);
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of server failed", e);
            }
        }
    }


    /**
     * This thread runs while attempting to make an outgoing connection
     * with a device. It runs straight through; the connection either
     * succeeds or fails.
     */
    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            mmDevice = device;
            BluetoothSocket tmp = null;

            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                Method m;
                m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                tmp = (BluetoothSocket) m.invoke(device, 1);

            } catch (IOException e) {
                e.printStackTrace();
            }
            catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mmSocket = tmp;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectThread");
            setName("ConnectThread");

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

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
            } catch (IOException e) {

                connectionFailed();
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() socket during connection failure", e2);
                }
                // Start the service over to restart listening mode
                BluetoothChatService.this.start();
                return;
            }

            // Reset the ConnectThread because we're done
            synchronized (BluetoothChatService.this) {
                mConnectThread = null;
            }

            // Start the connected thread
            connected(mmSocket, mmDevice);
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }