【发布时间】:2017-04-18 00:11:04
【问题描述】:
根据here 列出的服务,我正在尝试从 Android 设备订阅服务,但无法使请求部分正常工作。尝试使用7905F431-B5CE-4E99-A40F-4B1E122D00D0 进行广告宣传,但外围设备未显示在 iPhone 的蓝牙设备列表中。还使用(LE)扫描和过滤器来发现 ANCS 服务器,但运气不好。有什么提示吗?
编辑:代码
BleService.java
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
Log.d(TAG, "setCharacteristicNotification");
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.DESCRIPTOR));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor)
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
}
}
SampleGattAttributes.java
public class SampleGattAttributes {
public static String ANCS_SERVICE = "7905F431-B5CE-4E99-A40F-4B1E122D00D0";
public static String NOTIFICATION_SOURCE = "9FBF120D-6301-42D9-8C58-25E699A21DBD";
public static String DATA_CONTROL = "22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB";
public static String DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb";
}
MainActivity.java
private void displayServices(List<BluetoothGattService> services) {
for (BluetoothGattService service : services) {
if (service.getUuid().compareTo(UUID.fromString(ANCS_SERVICE)) == 0) {
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
Log.d(TAG, "charac: " + characteristic.getUuid());
for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
Log.d(TAG, "desc: " + descriptor.getUuid());
}
if (characteristic.getUuid().compareTo(UUID.fromString(NOTIFICATION_SOURCE)) == 0) {
bleService.setCharacteristicNotification(characteristic, true);
}
}
}
}
}
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BleService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
displayServices(bleService.getSupportedGattServices());
}
}
};
更新:连接到 iPhone 上宣传的服务后,我能够找到服务和特征。但是,订阅通知不会在 iPhone 上收到通知时触发onCharacteristicChanged。
update2:所有writedescriptor.setValue调用成功,依次运行。
update3:使用了this sample.的部分代码
update4:测试设备:Nexus 5X Android 6.0.1; iPhone 6S+ iOS 10.3.1
update5:BT 日志
【问题讨论】:
-
好吧,在 Nexus 设备上,在开发人员选项中,有一个复选框,即“启用蓝牙 HCI 监听日志”。当您启用它时,设备将记录蓝牙堆栈上发生的所有事情......请参阅存储在名为“HCIsnoop.log”的 SD 卡中的日志。当您在 HD 上保存文件时,请下载此软件(免费):fte.com/support/… 以查看这些 BT 日志文件。有了这个,你可以看到任何从一个设备交换到另一个设备的数据包(比如 BT 的 Wireshark)。
-
有什么方法可以在 Github 上发布这个应用程序?一个简单的 ANCS Android 客户端会很棒
-
你能在安卓上收到来自 iOS 的通知吗?
标签: android ios notifications bluetooth-lowenergy ancs