【问题标题】:Subscribe to ANCS on Android在 Android 上订阅 ANCS
【发布时间】: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 And​​roid 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


【解决方案1】:

哦,我遇到了类似的问题,请确保在 iOS 上进行测试时不要使用本地通知,因为这些通知不会通过 ANCS 传播。无论出于何种原因,您都需要真正的推送通知。

【讨论】:

    【解决方案2】:

    我最近遇到了类似的问题。我最终解决它的方法是将writeDescriptor 添加到我的BluetoothGatt。我的setCharacteristicNotification 现在看起来像这样:

    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
        UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
    }
    

    也许如果您可以发布/链接您的代码,我将能够进一步诊断您的确切问题,但上面的代码是我的问题的解决方案。

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 2021-06-29
      • 2016-10-18
      • 2014-06-13
      • 2019-01-03
      • 2013-07-04
      • 2021-12-12
      • 2015-06-30
      相关资源
      最近更新 更多