• 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);
            }
        }
    }
  • 相关阅读:
    数据库备份 DBS(Database Backup),知识点
    关系型数据库 RDS(Relational Database Service),知识点
    对象存储服务 OSS(Object Storage Service),知识点(待补充上仓库代码)
    Java 为什么需要包装类,如何使用包装类?
    for each 语句
    缓存中,2个注解:@cacheable 与 @cacheput 的区别
    微信小程序,相关代码
    微信小程序中的事件
    通俗易懂:索引、单列索引、复合索引、主键、唯一索引、聚簇索引、非聚簇索引、唯一聚簇索引 的区别与联系
    MySQL 的各种 join
  • 原文地址:https://www.cnblogs.com/shuaiwen/p/3198581.html
Copyright © 2020-2023  润新知