List of usage examples for android.bluetooth BluetoothDevice connectGatt
public BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback)
From source file:com.example.bluetoothmodule.TreadmillService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback.//w w w. java 2s . c o m */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { //mConnectionState = STATE_CONNECTING; return true; } else { return false; } } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; //mConnectionState = STATE_CONNECTING; return true; }
From source file:com.qi.airstat.BluetoothLeService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback./*from w w w . j a va 2 s . com*/ */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); BluetoothState.isBLEConnected(false); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { mConnectionState = STATE_CONNECTING; return true; } else { BluetoothState.isBLEConnected(false); return false; } } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); BluetoothState.isBLEConnected(false); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; Constants.MAC_POLAR = mBluetoothDeviceAddress; try { Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); latitude = location.getLatitude(); longitude = location.getLongitude(); } catch (SecurityException exception) { exception.printStackTrace(); } JSONObject jsonObject = new JSONObject(); try { jsonObject.put("userID", Constants.UID); jsonObject.put("conCreationTime", new SimpleDateFormat("yyMMddHHmmss").format(new java.util.Date())); jsonObject.put("flagValidCon", 1); jsonObject.put("devMAC", "x'" + Constants.MAC_POLAR.replaceAll(":", "") + "'"); jsonObject.put("devType", Constants.DEVICE_TYPE_POLAR); jsonObject.put("devPortability", 0x01); jsonObject.put("latitude", "x'" + latitude); jsonObject.put("longitude", "x'" + longitude); } catch (JSONException exception) { exception.printStackTrace(); } HttpService httpService = new HttpService(); String res = httpService.executeConn(null, "POST", "http://teamc-iot.calit2.net/IOT/public/deviceReg", jsonObject); try { JSONObject resJson; resJson = new JSONObject(res); Log.d("BLEService", "DevReg sent, response was " + res); if (resJson.getInt("status") == 0) { return true; } else { mConnectionState = STATE_DISCONNECTED; Constants.MAC_POLAR = null; BluetoothState.isBLEConnected(false); return false; } } catch (JSONException exception) { exception.printStackTrace(); return false; } }
From source file:com.cqupt.sensor_ble.activity.UartService.java
/** * ?? ?/*from w w w . j a va 2 s . c o m*/ * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); return mBluetoothGatt.connect(); } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. //autoConnect (boolean indicating whether to automatically connect to the BLE device // as soon as it becomes available) mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; return true; }
From source file:com.chenls.smartlock.UartService.java
/** * ?? ?//from w w w. java 2 s . co m * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { mConnectionState = STATE_CONNECTING; return true; } else { return false; } } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. //autoConnect (boolean indicating whether to automatically connect to the BLE device // as soon as it becomes available) mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; return true; }
From source file:com.github.akinaru.bleanalyzer.bluetooth.BluetoothCustomManager.java
/** * Connect to device's GATT server/*ww w . ja va2 s. co m*/ */ @SuppressLint("NewApi") public boolean connect(String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); boolean alreadyInList = false; if (bluetoothConnectionList.containsKey(address)) { alreadyInList = true; } if (alreadyInList) { Log.i(TAG, "reusing same connection"); BluetoothDeviceConn conn = (BluetoothDeviceConn) bluetoothConnectionList.get(address); conn.setGatt(device.connectGatt(context, false, conn.getGattCallback())); } else { BluetoothDeviceConn conn = new BluetoothDeviceConn(address, device.getName(), this); bluetoothConnectionList.put(address, conn); Log.i(TAG, "new connection"); //connect to gatt server on the device conn.setGatt(device.connectGatt(context, false, conn.getGattCallback())); } return true; }
From source file:net.kenevans.android.hxmmonitor.HxMBleService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address//from ww w. j a v a2 s . c om * The device address of the destination device. * * @return Return true if the connection is initiated successfully. The * connection result is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback. */ public boolean connect(final String address) { Log.d(TAG, "connect"); if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "connect: BluetoothAdapter not initialized or unspecified address"); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "connect: Trying to use an existing mBluetoothGatt for connection"); if (mBluetoothGatt.connect()) { mConnectionState = BluetoothProfile.STATE_CONNECTING; return true; } else { return false; } } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect"); return false; } // We want to directly connect to the device, so we are setting the // autoConnect parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection"); mBluetoothDeviceAddress = address; mConnectionState = BluetoothProfile.STATE_CONNECTING; return true; }
From source file:de.blinkt.openvpn.bluetooth.service.UartService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback./* w w w . j av a 2 s .com*/ */ //mac? public boolean connect(String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } address = address.toUpperCase(); final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } if (mBluetoothGatt != null) { mBluetoothGatt.close(); //LogUtil.info("-------------mBluetoothGatt"); } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mConnectionState = STATE_CONNECTING; return true; }
From source file:net.jpuderer.android.bluedoor.DoorlockService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback.//from w ww .j av a 2s .co m */ public boolean connect(final String address) { Log.d(TAG, "connect"); if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // Disconnect before creating a new connection. Otherwise, the device seems to remain // connected, but we no longer have a handle to it. if (mConnectionState != STATE_DISCONNECTED) { mBluetoothGatt.disconnect(); } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; broadcastConnectionUpdate(); return true; }
From source file:undot.safedrivers.BLE.BluetoothLeService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback.// w w w . j ava 2 s. co m */ public boolean connect(final String address) { System.out.println("BluetoothLeService connect" + address + mBluetoothGatt); if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. // if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) // && mBluetoothGatt != null) { // Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); // if (mBluetoothGatt.connect()) { // System.out.println("mBluetoothGatt connect"); // mConnectionState = STATE_CONNECTING; // return true; // } else { // System.out.println("mBluetoothGatt else connect"); // return false; // } // } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. System.out.println("device.connectGatt connect"); synchronized (this) { mBluetoothGatt = device.connectGatt(this, false, mGattCallback); } Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; return true; }
From source file:com.google.android.apps.forscience.ble.MyBleService.java
boolean connect(String address) { BluetoothDevice device = btAdapter.getRemoteDevice(address); if (device == null) { return false; }/* w w w.j a v a 2s . c om*/ BluetoothGatt bluetoothGatt = addressToGattClient.get(address); int connectionState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT); if (bluetoothGatt != null && connectionState != BluetoothProfile.STATE_CONNECTED) { return bluetoothGatt.connect(); } if (bluetoothGatt != null && connectionState == BluetoothProfile.STATE_CONNECTED) { sendGattBroadcast(address, BleEvents.GATT_CONNECT, null); return true; } if (bluetoothGatt != null) { bluetoothGatt.close(); } bluetoothGatt = device.connectGatt(this, false, // autoConnect = false gattCallbacks); addressToGattClient.put(address, bluetoothGatt); return true; }