首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android上订阅ANCS

在Android上订阅ANCS
EN

Stack Overflow用户
提问于 2017-04-18 00:11:04
回答 2查看 1.4K关注 0票数 5

基于列出的这里服务,我试图订阅安卓设备上的服务,但无法让征集部分正常工作。试着用7905F431-B5CE-4E99-A40F-4B1E122D00D0做广告,但是外围设备没有出现在iPhone的蓝牙设备列表中。还玩(LE)扫描和过滤器,以发现ANCS服务器没有运气。有什么暗示吗?

编辑:代码

BleService.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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上广告的服务之后,我能够找到服务和特性。但是,订阅通知不会在onCharacteristicChanged上收到通知时触发iPhone。

update2:所有的写descriptor.setValue调用都是成功的,并且按顺序运行。

update3:使用来自这个样本。的部分代码

update4:测试设备:Nexus5XAndroid6.0.1;iPhone 6S+ iOS 10.3.1

update5: BT日志

写入请求

写响应

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-21 22:02:46

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

票数 3
EN

Stack Overflow用户

发布于 2017-04-20 03:08:08

最近我遇到了一个类似的问题。为了解决这个问题,我所做的就是在我的BluetoothGatt中添加一个BluetoothGatt。我的setCharacteristicNotification现在看起来是这样的:

代码语言:javascript
复制
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);
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43461544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档