当前位置: 首页>>代码示例>>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;未经允许,请勿转载。