protected Socket createSocket( InetAddress hostAddr, int port, InetAddress localAddr, int localPort, int connectTimeout) throws IOException { ConnectThread t = new ConnectThread(); Socket socket = t.createSocket(hostAddr, port, localAddr, localPort, connectTimeout); return socket; }
/** * Start the ConnectThread to initiate a connection to a remote device. * * @param device The BluetoothDevice to connect * @param secure Socket Security type - Secure (true) , Insecure (false) */ public synchronized void connect(BluetoothDevice device, boolean secure) { if (D) Log.d(TAG, "connect to: " + device); // Cancel any thread attempting to make a connection if (mState == STATE_CONNECTING) { if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // zapisz identyfikator urządzenia bluetooth z jakim się łączymy btDeviceID = device.getAddress().hashCode(); // Start the thread to connect with the given device mConnectThread = new ConnectThread(device, secure); mConnectThread.start(); setState(STATE_CONNECTING); }
public void connect(String targetIP) { if (wifiMode) { tcpManager.tcpConnect(targetIP); } else { ConnectThread connect = new ConnectThread(); connect.start(); } }
private void cancelConnectThread() { if (m_connectThread != null && m_connectThread.isAlive()) { try { m_connectThread.cancel(); m_connectThread = null; } catch (Exception ignore) { /**/ } } }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected( BluetoothSocket socket, BluetoothDevice device, final String socketType) { if (D) Log.d(TAG, "connected, Socket Type:" + socketType); // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket, socketType); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(BlueMSP430Service.MESSAGE_DEVICE_NAME, btDeviceID); Bundle bundle = new Bundle(); bundle.putString(BlueMSP430Service.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { if (D) Log.d(TAG, "connected"); // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(MouseActivity.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(MouseActivity.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); // save connected device // mSavedDevice = device; // reset connection lost count // mConnectionLostCount = 0; setState(STATE_CONNECTED); }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity // Message msg = // mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putInt(BUNDLE_TYPE, OperationActivityHD.MESSAGE_DEVICE_NAME); bundle.putString(OperationActivityHD.DEVICE_NAME, device.getName()); bundle.putString(OperationActivityHD.DEVICE_ADDRESS, device.getAddress()); System.out.println("connected()--------------showMessage(bundle)"); showMessage(bundle); // msg.setData(bundle); // mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
/** 设备列表的单击响应函数,点击列表项,开始与该蓝牙设备建立连接 */ @SuppressLint("HandlerLeak") @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { DeviceList remoteDeviceList = deviceList.getItem(position); deviceName = remoteDeviceList.getRemoteDeviceName(); bluetoothAdapter.cancelDiscovery(); // 停止查找设备 AcceptThread aThread = new AcceptThread(bluetoothAdapter, serverName, MY_UUID); aThread.start(); // 启动服务器线程 ConnectThread cThread = new ConnectThread(remoteDeviceList.getRemoteDevice(), MY_UUID, handler); cThread.start(); // 启动客户端线程 mConnectThread = cThread; tvinfo.setText(String.format("正在与%s建立连接...", deviceName)); // 注册一个Broadcast Receiver来监听BluetoothDevice.ACTION_ACL_DISCONNECTED,即与远程设备建立连接失败 registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); }
public synchronized void connect(BluetoothDevice device) { DebugLog.i(TAG, "connect to: " + device); mConnectThread = new ConnectThread(device); mConnectThread.start(); setState(STATE_CONNECTING); }
/** * Start the chat service. Specifically start AcceptThread to begin a session in listening * (server) mode. Called by the Activity onResume() */ public synchronized void start() { Log.d(TAG, "Bluetooth Service start()"); // Cancel any thread attempting to make a connection if (mConnectThread != null && mState != STATE_CONNECTING) { Log.d(TAG, "connect thread null"); mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { Log.d(TAG, "connected thread null"); mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to listen on a BluetoothServerSocket // if connected or connect is null (no trial yet), then wait for connection if (mAcceptThread == null) { Log.d(TAG, "accept thread reset"); mAcceptThread = new AcceptThread(); mAcceptThread.start(); } setState(STATE_LISTEN); }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { if (D) Log.d(TAG, "connected"); // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Cancel the accept thread because we only want to connect to one device if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(BluetoothChat.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
/** Stop all threads */ public synchronized void stop() { if (DEBUG) { Log.i(TAG, "Synchronized stop()"); } if (mConnectThread != null) { mConnectThread.closeSocket(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.closeSocket(); mConnectedThread = null; } if (mSecureAcceptThread != null) { mSecureAcceptThread.closeSocket(); mSecureAcceptThread = null; } if (mInsecureAcceptThread != null) { mInsecureAcceptThread.closeSocket(); mInsecureAcceptThread = null; } setState(STATE_NONE); }
@Override public void onDestroy() { super.onDestroy(); try { unregisterReceiver(msgReceiver); } catch (IllegalArgumentException e) { e.printStackTrace(); } try { unregisterReceiver(wifiReceiver); } catch (IllegalArgumentException e) { e.printStackTrace(); } if (udpReceive != null) { udpReceive.cancel(); } // Stop the connection, if connected if (connect != null) { connect.cancel(); } if (connectedThread != null) { connectedThread.cancel(); } if (watchdog != null) { watchdog.finish_thread(); } }
/** * Start the chat service. Specifically start AcceptThread to begin a session in listening * (server) mode. Called by the Activity onResume() */ public synchronized void start() { Log.d(TAG, "start"); // Cancel any thread attempting to make a connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } setState(STATE_LISTEN); // Start the thread to listen on a BluetoothServerSocket if (mSecureAcceptThread == null) { mSecureAcceptThread = new AcceptThread(true); mSecureAcceptThread.start(); } if (mInsecureAcceptThread == null) { mInsecureAcceptThread = new AcceptThread(false); mInsecureAcceptThread.start(); } }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Cancel the accept thread because we only want to connect to one // device if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); setState(STATE_CONNECTED); }
// Management of BT connection through ConnectedThread public void connected(BluetoothSocket socket, BluetoothDevice device) { // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); }
/** * Start the ConnectThread to initiate a connection to a remote device. * * @param device The BluetoothDevice to connect */ public synchronized void connect(BluetoothDevice device) { if (D) Log.d(TAG, "connect to: " + device); // Cancel any thread attempting to make a connection if (mState == STATE_CONNECTING) { if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to connect with the given device mConnectThread = new ConnectThread(device); mConnectThread.start(); setState(STATE_CONNECTING); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 2) { if (resultCode == Activity.RESULT_OK) { BluetoothDevice device = data.getParcelableExtra("device"); mConnectThread = new ConnectThread(device); mConnectThread.start(); } } }
// Stop all threads public void stop() { if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } }
@Override protected void onDestroy() { if (mConnectThread != null) { mConnectThread.cancel(); // 停止蓝牙线程 } if (bluetoothAdapter != null) { bluetoothAdapter.disable(); // 关闭蓝牙 } stopService(gpsServiceIntent); // 停止GPS服务 super.onDestroy(); }
/** Stop all threads */ public synchronized void stop() { if (D) Log.d(TAG, "stop"); if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;} setState(STATE_NONE); }
private void startConnect() { try { cancelConnectThread(); initConnector(); m_connectThread = new ConnectThread(); m_connectThread.start(); } catch (Exception e) { m_connectException = e; if (m_connectListener != null) { m_connectListener.handleException(); } } }
public void sendMessage(String msg) { if (mBluetoothAdapter == null) { return; } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices == null) { Toast.makeText(mContext, "Please pair with at least one another phone", Toast.LENGTH_SHORT) .show(); } if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { isServer = false; ConnectThread myConnection = new ConnectThread(device, msg); myConnection.start(); String deviceName = device.getName(); Log.d("Bluetooth Device:", deviceName); } } }
/* * Opens the bluetooth connection with the specified port name */ public void open(String port) { synchronized (this.lock) { String error_message = null; try { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); adapter.cancelDiscovery(); Set<BluetoothDevice> paired_devices = adapter.getBondedDevices(); for (BluetoothDevice device : paired_devices) { String name = device.getName(); if (name.equals(port)) { Log.i("BluetoothConnection", "trying " + name); ConnectThread ct = new ConnectThread(device); ct.start(); ct.join(5000); error_message = ct.errorMessage; break; } } if (this.socket != null) { this.recv_stream = this.socket.getInputStream(); this.send_stream = this.socket.getOutputStream(); this.reader = new BufferedReader(new InputStreamReader(this.recv_stream, "UTF-8")); Log.i("BluetoothConnection", "Socket Ready"); } else { error_message = "Could not detect device " + port + ": " + String.valueOf(error_message); } } catch (Exception e) { error_message = "Error opening Bluetooth socket: " + e.getMessage(); } if (this.socket == null) { throw new RuntimeException( "Error opening Bluetooth port: " + String.valueOf(error_message)); } } }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { if (D) Log.d(TAG, "connected"); // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Cancel the accept thread because we only want to connect to one device if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity // mHandler.obtainMessage(),Returns a new Message from the global message pool, and // also sets the what member of the returned Message. // 為msg 貼上what的tag Message msg = mHandler.obtainMessage(MainService.MESSAGE_DEVICE_NAME); Log.i(TAG, "device.getName() : " + device.getName()); DeviceMappingAdapter deviceMappingAdapter = new DeviceMappingAdapter(mainContext); String deviceSn = deviceMappingAdapter.getDeviceSnById(device.getName()); if (deviceSn != null) { Log.i(TAG, "泰博跑到陽明血壓計"); setState(STATE_LISTEN); mHandler.obtainMessage(MainService.MESSAGE_CONNECTION_CLOSE, 1, -1).sendToTarget(); } else { Bundle bundle = new Bundle(); bundle.putString(MainService.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); } }
/** Stop all threads */ public synchronized void stop() { DebugLog.i(TAG, "stop"); if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mSecureAcceptThread != null) { mSecureAcceptThread = null; } this.disconnectAll(); setState(STATE_NONE); }
/** Stop all threads */ public synchronized void stop() { Log.d("BTHandler", "destroying the connection at all..."); if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } setState(STATE_NONE); }
/** * Start the chat service. Specifically start AcceptThread to begin a session in listening * (server) mode. Called by the Activity onResume() */ public synchronized void start() { if (D) Log.d(TAG, "start"); // Cancel any thread attempting to make a connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } setState(STATE_NONE); }
public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CONNECT_DEVICE: if (resultCode == Activity.RESULT_OK) { // Get the device MAC address String address = data.getExtras().getString(DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Attempt to connect to the device mConnectThread = new ConnectThread(device); mConnectThread.start(); } } }
/** * Stops any running threads and starts the thread that listens for incoming connections. * * @see com.example.drawfriends.BluetoothService.AcceptThread * @see com.example.drawfriends.BluetoothService#setState(int) */ public synchronized void start() { // Cancel any thread attempting to make a connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Start the thread to listen on a BluetoothServerSocket if (mAcceptThread == null) { mAcceptThread = new AcceptThread(); mAcceptThread.start(); } setState(STATE_LISTEN); }