- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名 iOS 开发人员,正在构建我的第一个 Android 蓝牙应用程序。在我的应用程序中,我有两个类,一个调用第二个类的主类是我的 BluetoothGattCallback
。
扫描设备后,我找到了我想要的设备,然后连接到它。这一切都有效。我的蓝牙设备每 5 秒传输一次数据,到目前为止我只能获得第一个 byteArray 传输。这是我在主课中调用的内容。
val gatt = device?.connectGatt(context, true, callBack)
然后在 onConnectionStateChange
中,我检查它是否已建立连接并调用 gatt?.discoverServices()
。从那里我可以过滤 onServicesDiscovered
中的 UUID 并调用 gatt.readCharacteristic(characteristic)
。我终于能够在 onCharacteristicRead
中读取我的字节数据。我的问题是,在连接设备后每次传输时我错过了哪一步来获取新数据?
奖金问题:我将如何管理多个设备回叫?如果我尝试连接多个设备,我是否需要保留对该设备的回调或 BluetoothGatt 的每个实例的引用?
最佳答案
蓝牙 GATT 要求中心订阅以接收特性更改的“通知”。连接到外围设备后(发现服务后),您需要订阅该特性。当 Android 中心接收到通知(来自外围设备)时,调用 BluetoothGattCallback 的 onCharacteristicChanged
函数(与在 iOS 上不同,在 iOS 上调用 characteristicRead 用于通知和手动请求的读取)。不幸的是,在 Android 上订阅一个特性比在 iOS 上更复杂。在 iOS 上,CBPeripheral 具有 setNotifyValue
函数。 android BluetoothDevice 对象没有这样的功能。我在我的 Android 应用程序中使用此功能,它与 iOS 的 setNotifyValue
功能相同。
/**
* Subscribe to a characteristic to receive notifications when is's value is changed
* @param gattConnection Which gattConnection to subscribe to characteristic on
* @param characteristic The characteristic to subscribe to
* @param subscribe Whether not to subscribe to the characteristic (false to unsubscribe)
*/
fun subscribeToCharacteristic(gattConnection: BluetoothGatt, characteristic: BluetoothGattCharacteristic, subscribe: Boolean = true) {
gattConnection.setCharacteristicNotification(char, subscribe) // Tell the connection we intend to subscribe or unsubscribe
// Now write the correct value to the client characteristic config descriptor
val descriptor = char.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")) // Client characteristic config UUID
if(descriptor != null){
descriptor.value = if(subscribe) BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE else BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
gattConnection?.writeDescriptor(descriptor)
}
}
只需使用上述函数订阅onServicesDiscovered
中的characteristic
即可。
响应处理多个连接:
不需要多个 BluetoothGattCallback
。您需要跟踪的只是从 connectGatt
函数返回的 BluetoothGatt
对象。 BluetoothGattCallback 中的每个函数都有一个 BluetoothGatt
对象作为其第一个参数。只需将传递给函数的 BluetoothGatt
对象与从 connectGatt
返回的 BluetoothGatt
对象进行比较,即可确定事件来自哪个连接。
例如:
// This is in the BluetoothGattCallback
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
if(gatt === gattConnection1){
// This event came from the first connection
}else if(gatt === gattConnection2){
// This event came from the second connection
}
}
关于Android BluetoothDevice 读取新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50974504/
这是我的程序的两个部分 1. Set pairedDevices; BluetoothDevice[] pairedDevicesArray; 2. pairedDevices = btAdapter
我是一名 iOS 开发人员,正在构建我的第一个 Android 蓝牙应用程序。在我的应用程序中,我有两个类,一个调用第二个类的主类是我的 BluetoothGattCallback。 扫描设备后,我找
我有一个项目,两者都有 在其中我使用(简化)调用扫描 BT 设备: final BluetoothLeScanner scanner = BluetoothAdapter.getDefaultAd
这个问题在这里已经有了答案: Android 6.0 - Bluetooth - No code exists for Action_Found broadcast intent (1 个回答) 关
在我使用蓝牙的应用程序中,日志中可能充斥着数百行“D/BluetoothDevice: mAddress: (address)”行。假设我可以忽略它们,我该如何过滤掉这些行? 最佳答案 显然,这已在
在 Android 中,您可以为配对设备指定一个不同于设备本身的广告名称的名称。但是,如果我在 BluetoothDevice 上调用 getName(),我会得到设备自己的名称。 有没有办法获取设备
我想保存我的应用程序连接到的最后一个蓝牙设备。如果以前有蓝牙连接,我根本不想提示用户。他们可以选择连接到新设备,但他们不需要这样做。如果他们选择不选择连接,他们会定期使用该应用程序,然后在需要蓝牙设备
我刚开始使用 Android,并使用低功耗蓝牙在 Android Studio 中设置了一个 API 21 项目。 深入研究 BluetoothDevice 向我展示了 ConnectGatt() 方
我正在使用此 sample-code 对 Android 的蓝牙 LE 功能进行一些简单的扫描 + 广告测试 我正在使用三星 6 和 Nexus 9 对其进行测试,出于某种原因,如果我让应用程序在三星
使用 Log 类跟踪运行时显示 onReceive() 方法没有调用,为什么? 动态注册广播接收器 private void discoverDevices () { Log.e("MOHA
这里是 Kotlin 菜鸟。 当我在 BluetoothDevice 上 ctrl+click 时,它会打开... \AppData\Local\Android\Sdk\sources\android
我的应用程序在连接到某些设备上的 BluetoothGatt 时出现问题。 所有 BLE 相关代码都驻留在 android Service 中,除 Huawei CAM-L03 外一切正常。据报道,一
我发现崩溃实例大幅增加,几个不同的崩溃报告都以相同的致命异常结束: [BluetoothDevice classicDevice]:无法识别的选择器发送到实例 0x2805a8ba0 这种情况在 iO
我有一个 BluetoothDevice 及其关联的 BluetoothGatt 对象。这是用户每天连接的设备,它没有任何经常变化的特性或服务。 现在为了与设备交互,每次连接后我必须首先发现所有服务,
这是我的扫描仪回调代码。 private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeSc
我的应用程序与 BLE 外围设备通信。有时,应用程序会在已连接外围设备的情况下启动。我可以通过调用以下方式取回设备: BluetoothManager manager = (BluetoothMana
我的 Android 手机允许我重命名已配对的设备,方法是转至 [设置 > 无线和网络 > 蓝牙] Activity 页面并单击已配对蓝牙设备右侧的设置按钮。但是,当我使用 BluetoothAdap
我正在尝试通过我的应用程序管理多个蓝牙事件,这样用户就不需要离开应用程序并尝试从 Android 设置中搜索/配对蓝牙设备。 我能够枚举以前配对的设备并开始发现,但是我找不到附近的设备。 背景信息:
有时,BluetoothDevice.getName() 返回 null。我该如何解决?remoteDeviceName 在以下代码中可能为 null。我需要通过 remoteDeviceName 区
我想知道远程物理设备中的哪些事件会触发监听设备中的 ACTION_ACL_CONNECTED 和 ACTION_ACL_DISCONNECTED。我的测试结果毫无意义。我收集了几个彼此相距几分米的设备
我是一名优秀的程序员,十分优秀!