- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Android 中使用 connectGatt()
方法连接到 BLE 设备。这很好用。
当我断开连接时,我使用以下内容:
private void disconnectDevice() {
gatt.disconnect();
}
当我收到回调时,我会关闭。
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
Log.d("BLED-GATT", "STATE_CONNECTED");
setUIConnectionStatus("Discover services on device...", Color.YELLOW);
checkEnableAddButton(simpleTrackEditText.getText().toString());
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.d("BLED-GATT", "STATE_DISCONNECTED");
setUIConnectionStatus("Not Connected!", Color.RED);
gatt.close();
break;
default:
Log.d("BLED-GATT", "STATE_OTHER");
}
}
}
这已执行,调用 disconnectDevice()
后我无法再控制设备。设备本身似乎认为它仍然处于连接状态,因为我无法将它置于广播可见性模式(如果它已经连接,则会发生这种情况)。但是,如果我终止应用程序并再次打开它,那么我可以将设备设置为广播模式。这告诉我应用程序未正确断开连接。
知道我错过了什么吗?
最佳答案
问题是我在扫描期间多次连接到同一台设备,导致我的应用程序同时打开许多连接。添加 !isConnected()
解决了问题:
/**
* Connects to the device. Does nothing if already connected.
* @param macAddress the address of the device.
*/
private void connectDevice(String macAddress) {
if (isConnected()) {
return;
}
device = bluetoothAdapter.getRemoteDevice(macAddress);
if (device == null) {
showToast("Device not available");
} else {
showToast("Connecting...");
gatt = device.connectGatt(this, true, gattCallback);
}
}
关于android - 完全断开蓝牙低功耗设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35986549/
具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI人工智能 卷积神经网络(CNN)通过从原始数据中自动学习层次特征表示,在图像识别任务中取得了巨大成功。虽然
具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI人工智能 血压的测量和预测是心脏病患者和有心脏问题的人的一个重要条件,应该保持持续的控制。在这项研究中,基
具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI人工智能 心血管疾病是最严重的死亡原因之一,每年在全世界造成严重的生命损失。持续监测血压似乎是最可行的选择
大家好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给大家介绍的是 恩智浦MCX系列MCU的新品MCXN947 。 自 2015 年恩智浦和飞思卡尔合并成新恩智浦之后,关于它们各
我正在开发一个应用程序,该应用程序接受语音输入,并将该输入与 list 中的已知项目进行匹配。 list 中的每个项目都有一个别名列表,以便长标题的项目可以与较短的名称相匹配。 例如: class P
两个双模蓝牙设备连接时,必须使用EDR,不能并联使用LE。然后必须通过 EDR 链路传输 BT 低功耗 GATT 配置文件。这是我从规范中读到的内容。 但是 iOS EDR 堆栈(在没有 MFi 许可
我正在尝试为 S3 开发蓝牙 4.0 应用程序。问题是,手机的行为就像它甚至没有蓝牙 4.0。它不会发现 4.0 设备,并且无法通过 4.0 设备发现。我在手机设置和应用程序中都尝试过,使用 Broa
我正在寻找一种与 Adafruit bluefruit LE(nRF8001 芯片组)板进行交互的方法,在 Windows 桌面应用程序中使用 c#(据我所知,我无法使用 Windows.Device
我是一名优秀的程序员,十分优秀!