如何将 Android BLE BluetoothGattCallback 设置为具有状态 BluetoothProfile.STATE_CONNECTING?
Posted
技术标签:
【中文标题】如何将 Android BLE BluetoothGattCallback 设置为具有状态 BluetoothProfile.STATE_CONNECTING?【英文标题】:How to set Android BLE BluetoothGattCallback to have state BluetoothProfile.STATE_CONNECTING? 【发布时间】:2014-10-23 12:35:26 【问题描述】:我正在为开源计步器编写一个 BLE 应用程序,到目前为止,它运行良好,但有一个烦人的问题:在 BLE 服务的 BluetoothGattCallback void 方法“onConnectionStateChange”中,参数“int newState”只能是此处记录的两个值之一,STATE_DISCONNECTED 或 STATE_CONNECTED”:
BluetoothGattCallback docs
问题是当我断开连接并重新尝试连接到我的 BLE 设备时,它可以工作,但是当它处于连接状态时我没有反馈。屏幕保持静止,从断开连接到连接,可能需要 3 秒到 15 秒。
因此,我的问题是,我是否可以直接访问 BluetoothGattCallback 的 onConnectionStateChange 方法并在其中传递“BluetoothProfile.STATE_CONNECTING”的值,以便状态“STATE_CONNECTING”的“else if”语句中的代码行执行?如果有,怎么做?
我已经附加了我的 onConnectionStateChange 和 connect 方法。它们与开发人员网站上提供的示例心率监测器应用程序中提供的内容基本没有变化。我唯一的改变是 STATE_CONNECTING 的“else if”。
谢谢。
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED)
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
else if (newState == BluetoothProfile.STATE_CONNECTING)
intentAction = ACTION_GATT_CONNECTING;
mConnectionState = STATE_CONNECTING;
Log.i(TAG, "Attempting to connect to GATT server...");
broadcastUpdate(intentAction);
else if (newState == BluetoothProfile.STATE_DISCONNECTED)
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
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.i(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.i(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
【问题讨论】:
【参考方案1】:原来我需要做的只是自己调用 Gatt 回调并创建一个私有静态 int 以传递给回调方法。
mGattCallback.onConnectionStateChange(mBluetoothGatt, GATT_INDETERMINATE, STATE_CONNECTING);
GATT_INDETERMINATE 在哪里
private static int GATT_INDETERMINATE = 8;
我确保 BluetoothGattCallback 类没有使用 8,因此 8 是一个很好的使用值。 最后在 onConnectionStateChanged 中,我执行以下操作并通过识别操作字符串广播适当的意图。
else if (newState == BluetoothProfile.STATE_CONNECTING)
intentAction = ACTION_GATT_CONNECTING;
mConnectionState = STATE_CONNECTING;
Log.i(TAG, "Attempting to connect to GATT server...");
broadcastUpdate(intentAction);
【讨论】:
它使用这个值...在一种奇怪的情况下,我将其作为状态变量的 onConnectionStateChange(BluetoothGatt gatt, int status...) 的错误代码。以上是关于如何将 Android BLE BluetoothGattCallback 设置为具有状态 BluetoothProfile.STATE_CONNECTING?的主要内容,如果未能解决你的问题,请参考以下文章
Android-低功耗蓝牙(BLE)-客户端(主机/中心设备)和服务端(从机/外围设备)