當前位置: 首頁>>代碼示例>>Java>>正文


Java BluetoothGatt.disconnect方法代碼示例

本文整理匯總了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);
}
 
開發者ID:inovait,項目名稱:neatle,代碼行數:20,代碼來源:Device.java

示例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;
	}
}
 
開發者ID:masterjc,項目名稱:bluewatcher,代碼行數:23,代碼來源:BluetoothClientService.java

示例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);
        }
    }
}
 
開發者ID:UDOOboard,項目名稱:UDOOBluLib-android,代碼行數:22,代碼來源:UdooBluService.java

示例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);
    }
}
 
開發者ID:TommyFen,項目名稱:NaiveDemos,代碼行數:39,代碼來源:BleOperationService.java

示例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);
    }
}
 
開發者ID:Twelvelines,項目名稱:AndroidMuseumBleManager,代碼行數:14,代碼來源:ConnectRequestQueue.java

示例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);
    }
}
 
開發者ID:Twelvelines,項目名稱:AndroidMuseumBleManager,代碼行數:16,代碼來源:BluetoothConnectManager.java

示例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);
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:14,代碼來源:DfuBaseService.java

示例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();
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:18,代碼來源:DfuBaseService.java

示例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;
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:13,代碼來源:DfuBaseService.java

示例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();
    }
}
 
開發者ID:qiu-yongheng,項目名稱:Bluetooth_BLE,代碼行數:12,代碼來源:BluetoothUtil.java

示例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");
        }
    }
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:73,代碼來源:BLEManager.java


注:本文中的android.bluetooth.BluetoothGatt.disconnect方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。