/**
* 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 void connect(final String address, IBleDeviceListener iBleDeviceListener) {
UdooBluException udooBluException = checkBluetooth(getApplicationContext());
if (udooBluException != null) {
if (iBleDeviceListener != null)
iBleDeviceListener.onError(udooBluException);
} else {
final BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
BluetoothGatt bluetoothGatt = checkAndGetGattItem(address);
if (connectionState == BluetoothProfile.STATE_DISCONNECTED) {
// Previously connected device. Try to reconnect.
if (bluetoothGatt != null) {
Log.d(TAG, "Re-use GATT connection");
if (bluetoothGatt.connect()) {
} else {
Log.w(TAG, "GATT re-connect failed.");
}
} else if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
} else {
Log.d(TAG, "Create a new GATT connection.");
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallbackBuilder());
mBluetoothGatts.put(address, bluetoothGatt);
}
} else {
Log.w(TAG, "Attempt to connect in state: " + connectionState);
bond(address);
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallbackBuilder());
mBluetoothGatts.put(address, bluetoothGatt);
}
}
}
UdooBluService.java 文件源码
java
阅读 40
收藏 0
点赞 0
评论 0
项目:UDOOBluLib-android
作者:
评论列表
文章目录