我的目标是在蓝牙低功耗设备和手机之间建立自动连接.我按照示例代码找到了该行
// 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;
}