/** * 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 synchronized void connect(BluetoothDevice device) { DebugLog.i(TAG, "connect to: " + device); mConnectThread = new ConnectThread(device); mConnectThread.start(); setState(STATE_CONNECTING); }
public void connect(String targetIP) { if (wifiMode) { tcpManager.tcpConnect(targetIP); } else { ConnectThread connect = new ConnectThread(); connect.start(); } }
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(); } } }
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(); } } }
/** 设备列表的单击响应函数,点击列表项,开始与该蓝牙设备建立连接 */ @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 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(); } } }
/** * 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); }
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)); } } }
public void connect(BluetoothDevice device) { connect = new ConnectThread(device); connect.start(); }