本文整理汇总了Java中android.bluetooth.BluetoothGatt.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Java BluetoothGatt.disconnect方法的具体用法?Java BluetoothGatt.disconnect怎么用?Java BluetoothGatt.disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.bluetooth.BluetoothGatt
的用法示例。
在下文中一共展示了BluetoothGatt.disconnect方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: disconnect
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void disconnect() {
NeatleLogger.i("Disconnecting");
stopDiscovery();
BluetoothGatt target;
int oldState;
synchronized (lock) {
target = gatt;
gatt = null;
this.serviceDiscovered = false;
oldState = state;
state = BluetoothGatt.STATE_DISCONNECTED;
}
if (target != null) {
target.disconnect();
}
notifyConnectionStateChange(oldState, BluetoothGatt.STATE_DISCONNECTED);
}
示例2: onConnectionStateChange
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected to GATT server.");
if( gatt.getDevice().getBondState() != BluetoothDevice.BOND_BONDED ) {
broadcastUpdate(ACTION_NOT_PAIRED);
gatt.disconnect();
return;
}
Log.i(TAG, "Attempting to start service discovery" );
mBluetoothGatt.discoverServices();
connected = true;
broadcastUpdate(ACTION_GATT_CLIENT_CONNECTED);
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_CLIENT_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
connected = false;
}
}
示例3: disconnect
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Disconnects an existing connection or cancel a pending connection. The disconnection result is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} callback.
*/
public void disconnect(String address) {
if (mBtAdapter == null) {
Log.w(TAG, "disconnect: BluetoothAdapter not initialized");
return;
}
final BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
BluetoothGatt bluetoothGatt = checkAndGetGattItem(address);
if (bluetoothGatt != null) {
Log.i(TAG, "disconnect");
if (connectionState != BluetoothProfile.STATE_DISCONNECTED) {
bluetoothGatt.disconnect();
} else {
Log.w(TAG, "Attempt to disconnect in state: " + connectionState);
}
}
}
示例4: onConnectionStateChange
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
LogUtils.d(TAG, "onConnctionStateChange:" + Thread.currentThread().toString());
if (BluetoothProfile.STATE_CONNECTED == newState) {
//连接成功
LogUtils.i(TAG, "onConnectionStateChange():连接成功,得到gatt,status = " + status);
mState = State.STATE_CONNECTED;
//发现服务
boolean b = gatt.discoverServices();
LogUtils.d(TAG, "试图查询 gatt 上的 services :" + b);
//发送消息给 Activity
sendMsgToClient(MsgClient.MSG_GATT_CONNECTED, null);
} else if (BluetoothProfile.STATE_DISCONNECTED == newState) {
//返回连接断开的消息
sendMsgToClient(MsgClient.MSG_CONNECTED_DISMISS, null);
//取消周期行读取 rssi 的操作
cancelAllTasks();
//清除当前 gatt
refreshDeviceCache();
gatt.disconnect();//不使用自带的重连功能
closeGatt();
//定时重连操作
mScheduledThreadPool.schedule(task1, Attributes.RE_CONNECTING_TIME, TimeUnit.MILLISECONDS);
LogUtils.i(TAG, "onConnectionStateChange():连接断开,status = " + status);
mState = State.STATE_DISCONNECT;
} else {
LogUtils.i(TAG, "其它状态: status = " + status + ", newState = " + newState);
}
}
示例5: disconnect
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* 断开蓝牙连接,不会释放BluetoothGatt持有的所有资源,可以调用mBluetoothGatt.connect()很快重新连接上
* 如果不及时释放资源,可能出现133错误,http://www.loverobots.cn/android-ble-connection-solution-bluetoothgatt-status-133.html
* @param address
*/
public void disconnect(String address){
if (!isEmpty(address) && gattMap.containsKey(address)){
Logger.w("disconnect gatt server " + address);
BluetoothGatt mBluetoothGatt = gattMap.get(address);
mBluetoothGatt.disconnect();
updateConnectState(address, ConnectState.NORMAL);
}
}
示例6: disconnect
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* 断开蓝牙连接,不会释放BluetoothGatt持有的所有资源,可以调用mBluetoothGatt.connect()很快重新连接上
* 如果不及时释放资源,可能出现133错误,http://www.loverobots.cn/android-ble-connection-solution-bluetoothgatt-status-133.html
* @param address
*/
public void disconnect(String address){
if (!isEmpty(address) && gattMap.containsKey(address)){
reconnectParamsBean = new ReconnectParamsBean(address);
reconnectParamsBean.setNumber(1000);
Logger.w("disconnect gatt server " + address);
BluetoothGatt mBluetoothGatt = gattMap.get(address);
mBluetoothGatt.disconnect();
updateConnectStateListener(address, ConnectState.NORMAL);
}
}
示例7: cancelPairing
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
private void cancelPairing(final BluetoothGatt gatt) {
logi("----> cancelPairing");
if (mConnectionState != STATE_DISCONNECTED) {
// Disconnect from the device
mConnectionState = STATE_DISCONNECTED;
logi("Disconnecting from the device...");
gatt.disconnect();
sendLogBroadcast(LOG_LEVEL_INFO, "Disconnected");
}
// Close the device
refreshDeviceCache(gatt, false); // This should be set to true when DFU Version is 0.5 or lower
close(gatt);
}
示例8: disconnect
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Disconnects from the device. This is SYNCHRONOUS method and waits until the callback returns new state. Terminates immediately if device is already disconnected. Do not call this method
* directly, use {@link #terminateConnection(android.bluetooth.BluetoothGatt, int)} instead.
*
* @param gatt the GATT device that has to be disconnected
*/
private void disconnect(final BluetoothGatt gatt) {
if (mConnectionState == STATE_DISCONNECTED)
return;
mConnectionState = STATE_DISCONNECTING;
logi("Disconnecting from the device...");
gatt.disconnect();
// We have to wait until device gets disconnected or an error occur
waitUntilDisconnected();
}
示例9: close
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Closes the GATT device and cleans up.
*
* @param gatt the GATT device to be closed
*/
private void close(final BluetoothGatt gatt) {
logi("Cleaning up...");
sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.close()");
gatt.disconnect();
gatt.close();
mConnectionState = STATE_CLOSED;
}
示例10: closeBluetoothGatt
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* 关闭GATT连接
* @param gatt
*/
public static void closeBluetoothGatt(BluetoothGatt gatt) {
if (gatt != null) {
gatt.disconnect();
refreshDeviceCache(gatt);
gatt.close();
}
}
示例11: onConnectionStateChange
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(DEBUG) {
logi("BluetoothGattCallback.onConnectionStateChange() :: start : status = " + status + " newState = " +
"" + newState);
}
int state = BLE_DISCONNECTED;
int error = 0;
boolean gattForceClosed = false;
switch(status) {
case BluetoothGatt.GATT_SUCCESS: {
if(newState == BluetoothProfile.STATE_CONNECTED) {
state = BLE_CONNECTED;
} else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
state = BLE_DISCONNECTED;
if(gatt != null) {
if(DEBUG) {
logi("onConnectionStateChange() :: gatt != null : closing gatt");
}
gatt.disconnect();
gatt.close();
gattForceClosed = true;
if(BLEManager.this.gatt.getDevice().getAddress().equals(gatt.getDevice().getAddress())) {
BLEManager.this.gatt = null;
}
}
}
}
break;
default:
Log.e(TAG, "Connection error: " + GattError.parseConnectionError(status));
break;
}
if(status != BluetoothGatt.GATT_SUCCESS) {
state = BLE_DISCONNECTED;
error = BLE_ERROR_FAIL;
}
synchronized(locker) {
if(inBleOp == OP_CONNECT) {
if(DEBUG) {
logi("BluetoothGattCallback.onConnectionStateChange() :: inBleOp == OP_CONNECT");
}
if(state != (bleState & BLE_CONNECTED)) {
bleState = state;
}
callbackCompleted = true;
BLEManager.this.error = error;
extendedError = status;
locker.notify();
} else {
if(DEBUG) {
logi("onConnectionStateChange() :: inBleOp != OP_CONNECT");
}
bleState = state;
unexpectedDisconnectionListener.handleConnectionEvent(bleState, gattForceClosed);
}
if(DEBUG) {
logi("BluetoothGattCallback.onConnectionStateChange() :: end");
}
}
}