- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一个开源计步器编写一个 BLE 应用程序,到目前为止它运行良好,但有一个恼人的问题:在 BLE 服务的 BluetoothGattCallback void 方法“onConnectionStateChange”中,参数“int newState”只能是两个值之一,STATE_DISCONNECTED 或 STATE_CONNECTED”,如此处所述:
问题是当我断开连接并重新尝试连接到我的 BLE 设备时,它工作正常,但当它处于连接状态时我没有任何反馈。屏幕保持静止并从断开连接跳转到连接,这可能需要 3 秒到 15 秒。
因此,我的问题是,我能否直接访问 BluetoothGattCallback 的 onConnectionStateChange 方法并在其中传递“BluetoothProfile.STATE_CONNECTING”的值,以便执行状态“STATE_CONNECTING”的“else if”语句中的代码行?如果是,怎么办?
我已经附加了我的 onConnectionStateChange 和连接方法。它们与开发者网站上提供的示例心率监测应用程序中提供的内容基本相同。我唯一的变化是 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;
}
最佳答案
原来我需要做的就是自己调用 Gatt 回调并创建一个私有(private)静态 int 以传递给回调方法。
mGattCallback.onConnectionStateChange(mBluetoothGatt, GATT_INDETERMINATE, STATE_CONNECTING);
GATT_INDETERMINATE 在哪里
private static int GATT_INDETERMINATE = 8;
我确保 BluetoothGattCallback 类没有使用 8,因此 8 是一个很好的值。最后,在 onConnectionStateChanged 中,我执行以下操作并广播带有标识操作字符串的适当 Intent 。
else if (newState == BluetoothProfile.STATE_CONNECTING) {
intentAction = ACTION_GATT_CONNECTING;
mConnectionState = STATE_CONNECTING;
Log.i(TAG, "Attempting to connect to GATT server...");
broadcastUpdate(intentAction);
}
关于android - 如何将 Android BLE BluetoothGattCallback 设置为具有状态 BluetoothProfile.STATE_CONNECTING?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569278/
我尝试连接到 MiBand 2 并保持连接,但几秒钟后它失败并重新连接。 我扫描可用设备并显示它们。当我点击我想连接的设备时,它会连接,但几秒钟后就会断开连接。 在连接设备上我这样做: private
我的 device.connectGatt() 只触发 onConnectionStateChage。状态为0,连接建立。我在 4.4 和 5.1 系统上对其进行了测试 - 结果相同。这是我的代码:
我的服务有一个 BluetoothGattCallback 实例 public class MyService extends Service { private BluetoothGattC
我试图弄清楚必须实现的 BluetoothGattCallback 中的处理程序是否在单独的线程上,或者它们是否需要及时返回。换句话说,我是否能够在 onCharacteristicChanged()
我想订阅多个特征。目前我正在通过在调用每个 writeDescriptor(descriptor) 之间使用计时器来做到这一点(大部分成功)。我想改为使用 onDescriptorWrite() 回调
我正在尝试开发一个 BT 应用程序以连接到 BLE 设备并从中读取一些数据。对于设备信息部分,我循环遍历设备的所有特征,读取特征并从 BluetoothGattCallback 的 onCharact
我正在开发一个 Android 应用程序来连接蓝牙模块双模式。 由于模块是双模式的,我遇到了与描述相同的问题 here . 但是我试图做一个解决方法,因为在第一次连接后,回调 onConnection
有人可以检查这段代码的问题吗?我已经实现了一项服务并将 BluetoothDevice 对象传递给它(BluetoothDevice 对象通过 intent 传递没有错误/问题)。然后,在 onSta
当调用 onCharacteristicChanged() 回调时,我观察到一个奇怪的权限相关问题。 我正在编写一个 Android 服务,该服务使用低功耗蓝牙 API 与外围角色的 LE(低功耗)传
我正在研究适用于 Android 的低功耗蓝牙(BLE、蓝牙 4.0)。 我引用了链接中的文档:Bluetooth Low Energy . 它使用 connectGatt() 方法连接到 devic
我正在为一个开源计步器编写一个 BLE 应用程序,到目前为止它运行良好,但有一个恼人的问题:在 BLE 服务的 BluetoothGattCallback void 方法“onConnectionSt
我调用我的蓝牙适配器 mBluetoothAdapter.startDiscovery();,然后返回一个 BluetoothDevices 列表。当我按下其中一个设备时,我会这样做: mBlu
我对安卓不太了解。我正在尝试通过 BLE 发送数据。我的代码如下->。我从网上得到的 https://developer.android.com/guide/topics/connectivity/b
我是一名优秀的程序员,十分优秀!