2

从 Android Bluetooth Insecure Rfcomm private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 使用这个 UUID,我可以连接除 android 之外的其他手机(Bada、Blackberry)。下面是我从 android 示例中获得的代码 /

**
     * 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;
        BluetoothSocket tmp = null;
        public ConnectThread(BluetoothDevice device) 
        {
            mmDevice = device;


            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try 
            {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                Log.d(TAG, "tmp = "+tmp.toString());
            }
            catch (IOException e) 
            {
                Log.e(TAG, "create() failed", e);
            }
            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
                Log.d(TAG, " before connecting mmsocket.connect()");
                mmSocket.connect();
                Log.d(TAG, " after connecting mmsocket.connect()");
            }
            catch (IOException e) 
            {
                Log.d(TAG, " before connectionFailed()");
                connectionFailed();
                Log.d(TAG, " after connectionFailed()");
                Log.d(TAG, "after connectiofailed exception is: "+ e.toString());
                // Close the socket
                try 
                {
                    mmSocket.close();
                } 
                catch (IOException e2) 
                {
                    Log.d(TAG, "unable to close() socket during connection failure: "+ e2.toString());
                }

//                // Start the service over to restart listening mode
//                BluetoothChatService.this.start();
                return;
            }

            // Reset the ConnectThread because we're done
            synchronized (BluetoothService.this)
            {
                mConnectThread = null;
            }
            Log.d(TAG, " before connected mmsocket and mmdevice");

            // Start the connected thread
            connected(mmSocket, mmDevice);
            Log.d(TAG, "after connected mmsocket and mmdevice");
        }

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

我收到错误服务发现失败。我想配对 2 部安卓手机请帮忙!!!

4

0 回答 0