蓝牙插座没有连接Android蓝牙

时间:2014-04-21 14:54:50

标签: android

我想通过蓝牙连接两个Android设备,但它根本没有连接。这是我到目前为止连接的代码.....可以做什么,我已经在互联网上搜索但我还没有解决方案....... 请帮助我

// Unique UUID for this application
  private static UUID MY_UUID_SECURE =
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private static UUID   MY_UUID_INSECURE=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
 private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    private String mSocketType;

    public ConnectThread(BluetoothDevice device, boolean secure) {
        mmDevice = device;
        BluetoothSocket tmp = null;
        mSocketType = secure ? "Secure" : "Insecure";

        System.out.println("UUID created "+MY_UUID_SECURE);
        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {

            if (secure) { 
                Method m = device.getClass().getMethod("createRfcommSocket", new  Class[] { int.class });
                 tmp = (BluetoothSocket)m.invoke(mmDevice,Integer.valueOf(1));
                tmp = device.createRfcommSocketToServiceRecord(
                        MY_UUID_SECURE);
                System.out.println("mm socket value :-"+tmp);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        MY_UUID_INSECURE);
                System.out.println("mm socket value :-"+tmp);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            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();
        }
        mmSocket = tmp;
    }

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

        // 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
              System.out.println("new value socket value :-"+mmSocket);             
            mmSocket.connect();
        } catch (IOException e) {
            // Close the socket
            try {

                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() " + mSocketType +
                        " socket during connection failure", e2);
            }
            connectionFailed();
            return;
        }

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

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


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

/**
 * This thread runs during a connection with a remote device.
 * It handles all incoming and outgoing transmissions.
 */

0 个答案:

没有答案