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


Java BluetoothDevice.connectGatt方法代碼示例

本文整理匯總了Java中android.bluetooth.BluetoothDevice.connectGatt方法的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothDevice.connectGatt方法的具體用法?Java BluetoothDevice.connectGatt怎麽用?Java BluetoothDevice.connectGatt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.bluetooth.BluetoothDevice的用法示例。


在下文中一共展示了BluetoothDevice.connectGatt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
 * Synchronously connect to the given bluetooth device, set connection parameters (MTU)
 * and discover the mDL SERVICE_UUID and APDU_UUID.
 *
 * @param device The device to connect to. The device must support the mDL service and characteristic.
 * @throws InterruptedException When the thread is interrupted while waiting for a BT response.
 */
public void connect(BluetoothDevice device) throws InterruptedException, GattException {
    if (VDBG) { Log.d(TAG, "connect: [CMD] " + device.getAddress()); }
    this.mBtGatt = device.connectGatt(this.mActivity, false, this);
    waitUntilConnected();
    setConnectionParameters();
    discoverServices();

    BluetoothGattService service = mBtGatt.getService(Constants.SERVICE_UUID);

    if (service == null) {
        Log.d(TAG, "SERVICES FOUND");
        for (BluetoothGattService s: mBtGatt.getServices()) {
            Log.d(TAG, "Service found: " + s.getUuid());
        }
        Log.d(TAG, "END OF LIST");
        throw new GattException("Could not find service " + Constants.SERVICE_UUID);
    }
    mCharacteristic = service.getCharacteristic(Constants.APDU_UUID);
    if (mCharacteristic == null) {
        throw new GattException("Could not find characteristic " + Constants.APDU_UUID);
    }

    if (VDBG) { Log.d(TAG, "connect: [DONE] "); }
}
 
開發者ID:mDL-ILP,項目名稱:mDL-ILP,代碼行數:32,代碼來源:GattClient.java

示例2: connectBLEDevice

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
 * 方法3:連接 BLE 設備內的 GATT 服務
 *
 * @param device BLE 設備的藍牙 mac 地址,或者使用 BluetoothDevice 對象
 * @return 如果連接操作成功,則返回 true,連接結果是通過 BluetoothGattCallback 中的回調中異步返回
 */
private boolean connectBLEDevice(BluetoothDevice device) {
    if (null == mBluetoothAdapter || null == device) {
        LogUtils.e(TAG, "藍牙適配器對象為null,或 device 為空");
        return false;
    }

    if (mState != State.STATE_IDLE && mState != State.STATE_DISCONNECT) {
        LogUtils.e(TAG, "connectBLEDevice():當前狀態不可連接" + mState.name());
        return false;
    }
    //新建連接,device.connectGatt()方法中 false 表示立刻連接,true 表示在合適的時間連接
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    LogUtils.d(TAG, "新建 Gatt 連接");
    mState = State.STATE_CONNECTING;
    return true;
}
 
開發者ID:TommyFen,項目名稱:NaiveDemos,代碼行數:23,代碼來源:BleOperationService.java

示例3: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
public boolean connect(String address) {
    if (mBluetoothAdapter == null || address == null) {
        ULog.e("BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null) {
        ULog.i("Trying to use an existing mBluetoothGatt for connection.");
        return mBluetoothGatt.connect();
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        ULog.e("Device not found,Unable to connect.");
        return false;
    }
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    ULog.i("Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    return true;
}
 
開發者ID:WillFlower,項目名稱:BluetoothCtrl,代碼行數:23,代碼來源:BluetoothLeService.java

示例4: updateData

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
@Override
public void updateData() {
    mIsConnecting = true;
    BluetoothDevice device = BLEManager.instance.getBleAdapter().getRemoteDevice(getMacAddress());
    mGatt = device.connectGatt(BLEManager.instance.getContext(), false, mGattCallback);
    if(mGatt == null){
        Log.d(mTAG, "Can't connect to " + getMacAddress());
        mIsConnecting = false;
    }
    else{
        while(mIsConnecting){
            try{
                Thread.sleep(500);
            }catch (Exception e){

            }
        }
    }
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:20,代碼來源:nRF51822SensorEntity.java

示例5: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
     * 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);
            }
        }
    }
 
開發者ID:UDOOboard,項目名稱:UDOOBluLib-android,代碼行數:41,代碼來源:UdooBluService.java

示例6: updateData

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
@Override
public void updateData() {
    mIsConnecting = true;
    BluetoothDevice device = BLEManager.instance.getBleAdapter().getRemoteDevice(getMacAddress());
    mGatt = device.connectGatt(BLEManager.instance.getContext(), false, mGattCallback);
    if(mGatt == null){
        Log.d(mTAG, "Can't connect to " + getMacAddress());
        mIsConnecting = false;
    }
    while (mIsConnecting){
        try{
            Thread.sleep(500);
        }catch (Exception e){

        }
    }
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:18,代碼來源:TitagSensorEntity.java

示例7: updateData

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
@Override
public void updateData() {
    mIsConnecting = true;
    BluetoothDevice device = BLEManager.instance.getBleAdapter().getRemoteDevice(getMacAddress());
    BluetoothGatt mGatt = device.connectGatt(BLEManager.instance.getContext(), false, mGattCallback);
    if(mGatt == null){
        Log.d(mTAG, "Can't connect to " + getMacAddress());
        mIsConnecting = false;
    }
    while(mIsConnecting){
        try{
            Thread.sleep(500);
        }catch (Exception e){

        }
    }
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:18,代碼來源:MiFloraSensorEntity.java

示例8: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
public boolean connect(final String address) {
	if (bluetoothAdapter == null || address == null) {
		Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
		return false;
	}

	if (bluetoothDeviceAddress != null && address.equals(bluetoothDeviceAddress)
			&& bluetoothGatt != null) {
		Log.d(TAG, "Trying to use an existing bluetoothGatt for connection.");
		if (bluetoothGatt.connect()) {
			connectionState = STATE_CONNECTING;
			return true;
		} else {
			return false;
		}
	}

	final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
	if (device == null) {
		Log.w(TAG, "Device not found.  Unable to connect.");
		return false;
	}

	bluetoothGatt = device.connectGatt(this, false, gattCallback);
	Log.d(TAG, "Trying to create a new connection.");
	bluetoothDeviceAddress = address;
	connectionState = STATE_CONNECTING;

	return true;
}
 
開發者ID:Make-A-Pede,項目名稱:Make-A-Pede-Android-App,代碼行數:31,代碼來源:BluetoothLeService.java

示例9: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
 * 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.
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}
 
開發者ID:richhowley,項目名稱:Android-BLE-to-Arduino,代碼行數:42,代碼來源:BluetoothLeService.java

示例10: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
public boolean connect(final String address) {
    Log.e(TAG, "connect");

    // check bluetooth adapter & ble address
    if (mBluetoothAdapter == null || address == null) {
        Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    // check ble address & ble Gatt
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        Log.e(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    // get ble remote device
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    // check device validate
    if (device == null) {
        Log.e(TAG, "Device not found.  Unable to connect.");
        return false;
    }

    // try connect to Gatt server
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.e(TAG, "Trying to create a new connection.");

    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;

    return true;
}
 
開發者ID:skydoves,項目名稱:MagicLight-Controller,代碼行數:39,代碼來源:BluetoothLeService.java

示例11: mensaje_conectarDispositivo

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
public boolean mensaje_conectarDispositivo(String mac){
    boolean conectado = false;

    BluetoothDevice device = this.listaDevices.get(mac);
    if(device != null){
        this.mBluetoothGatt = device.connectGatt(this, true, this.mBtleCallback);
        conectado = true;
    }

    return conectado;
}
 
開發者ID:TfgReconocimientoPulseras,項目名稱:TrainAppTFG,代碼行數:12,代碼來源:BluetoothLeService.java

示例12: connectDevice

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
 * Creates a GATT connection to the given device.
 *
 * @param address String containing the address of the device
 */
private void connectDevice(String address) {
    if (!mBluetoothAdapter.isEnabled()) {
        Toast.makeText(getActivity(), R.string.state_off, Toast.LENGTH_SHORT).show();
        getActivity().finish();
    }
    mListener.onShowProgress();
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    mGatt = device.connectGatt(getActivity(), false, mCallback);
}
 
開發者ID:martindisch,項目名稱:SensorTag-Accelerometer,代碼行數:15,代碼來源:DeviceFragment.java

示例13: onScanResult

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
 * Callback when a BLE advertisement has been found.
 *
 * @param callbackType Determines how this callback was triggered.
 * @param result       A Bluetooth LE scan result.
 */
@Override
public void onScanResult(int callbackType, final ScanResult result) {
    super.onScanResult(callbackType, result);

    // Get the ScanRecord and check if it is defined (is nullable)
    final ScanRecord scanRecord = result.getScanRecord();
    if (scanRecord != null) {
        // Check if the Service UUIDs are defined (is nullable) and contain the discovery
        // UUID
        final List<ParcelUuid> serviceUuids = scanRecord.getServiceUuids();
        if (serviceUuids != null && serviceUuids.contains(DISCOVERY_UUID)) {
            // We have found our device, so update the GUI, stop scanning and start
            // connecting
            final BluetoothDevice device = result.getDevice();

            // We'll make sure the GUI is updated on the UI thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // At this point we have the device address and RSSI, so update those
                    deviceAddressTextView.setText(device.getAddress());
                    rssiTextView.setText(getString(R.string.rssi, result.getRssi()));
                }
            });

            stopDiscovery();

            bluetoothGatt = device.connectGatt(
                    MainActivity.this,
                    // False here, as we want to directly connect to the device
                    false,
                    bluetoothGattCallback
            );
        }
    }
}
 
開發者ID:SPINremote,項目名稱:sdc-1-quickstart-android,代碼行數:43,代碼來源:MainActivity.java

示例14: startClient

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
private void startClient() {
    BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
    mBluetoothGatt = bluetoothDevice.connectGatt(mContext, false, mGattCallback);

    if (mBluetoothGatt == null) {
        Log.w(TAG, "Unable to create GATT client");
        return;
    }
}
 
開發者ID:Nilhcem,項目名稱:blefun-androidthings,代碼行數:10,代碼來源:GattClient.java

示例15: connect

import android.bluetooth.BluetoothDevice; //導入方法依賴的package包/類
/**
 * Connects to the Bluetooth Smart device
 *
 * @param device a device to connect to
 */
public void connect(final BluetoothDevice device) {
	if (mConnected)
		return;

	if (mBluetoothGatt != null) {
		mBluetoothGatt.close();
		mBluetoothGatt = null;
	}

	final boolean autoConnect = shouldAutoConnect();
	mUserDisconnected = !autoConnect; // We will receive Linkloss events only when the device is connected with autoConnect=true
	mBluetoothGatt = device.connectGatt(mContext, autoConnect, mGattCallback);
}
 
開發者ID:runtimeco,項目名稱:Android-DFU-App,代碼行數:19,代碼來源:BleManager.java


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