android connect窗口,android – 在BLE的connectGatt中哪个正确的autoConnect标志?

我的目标是在蓝牙低功耗设备和手机之间建立自动连接.我按照示例代码找到了该行

// We want to directly connect to the device,so we are setting the autoConnect parameter to false.

mBluetoothGatt = device.connectGatt(this,false,mGattCallback);

上面的代码表示虚假用于自动连接.但是,我在here发现了API,它说

BluetoothGatt connectGatt(Context context,boolean autoConnect,BluetoothGattCallback callback,int transport)

Connect to GATT Server hosted by this device.

我还尝试了两个标志:真和假,只有真的是工作.我使用的是版本> = Android 5.0.代码和API之间有什么不一致吗?哪个标志是正确的?如果我想进行自动连接,是否需要注意?

这是我的代码

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,true,mGattCallback);

Log.d(TAG,"Trying to create a new connection.");

mBluetoothDeviceAddress = address;

mConnectionState = STATE_CONNECTING;

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值