Android学习之Bluetooth开发总结<续>

private class ConnectThread extends Thread {
    private  BluetoothSocket mmSocket;
    private  BluetoothDevice mmDevice;
    ImprovedBluetoothDevice improvedBluetoothDevice;
    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;
  
        improvedBluetoothDevice = new ImprovedBluetoothDevice(device);
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        //setName("ConnectThread");
        // Always cancel discovery because it will slow down a connection
        //蓝牙连接之前需要取消查找
        mAdapter.cancelDiscovery();

        //String connectionType = "?";

    //蓝牙设备有三十个端口号,是从1到30
    for(int port = 1; port < 31; port++) {
        Log.d(TAG, "Connecting with port: " + port);
   
        try {
            //connectionType = "Secure";
            Log.d(TAG, "Attempting createRfcommSocket");
    
            //分别使用1-30这些端口
            BluetoothSocket s = improvedBluetoothDevice.createRfcommSocket(port);
            s.connect();
           
            mmSocket = s;
        } catch (Exception ex) {
            Log.e(TAG, ex.toString());
            //TODO 异常处理
            //赋值为空
            mmSocket = null;
            try {
                //connectionType = "Insecure";
                Log.d(TAG, "Attempting createInsecureRfcommSocket");
            
                //如果在连接过程中出现异常,就使用第二种方案来进行连接
                BluetoothSocket s = improvedBluetoothDevice.createInsecureRfcommSocket(port);
                s.connect();
               
                mmSocket = s;
            } catch (Exception ex2) {
                Log.e(TAG, ex2.toString());
                //异常处理
                //如果再次连接还是出异常,就再次赋值为空
                mmSocket = null;
            }
        }
   
        if (mmSocket != null) {
            Log.d(TAG, "Connection succeeded with " + connectionType + " connection on port " + port);
            break;
        }
    }

        //如果还没有获取到mmSocket ,就使用以下方法
        //蓝牙串口连接有两种方式,一种是使用端口号,另一种是使用UUID,如果1-30的端口号都不能连接上,就使用UUID进行连接
        if (mmSocket == null) {   
            try {
                mmSocket = improvedBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
                mmSocket.connect();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
        // Reset the ConnectThread because we're done
        //synchronized (BluetoothChatService.this) {
            //mConnectThread = null;
        //}


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


    public void cancel() {
        try {
            if(mmSocket!=null)
            {
                mmSocket.close();
                mmSocket = null;
            }               
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}
原文地址:https://www.cnblogs.com/shuaiwen/p/3198581.html