微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Android上订阅ANCS

基于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:所有write descriptor.setValue调用都成功,并按顺序运行.

update3:使用了this sample.的部分代码

update4:测试设备:Nexus 5X And​​roid 6.0.1; iPhone 6S iOS 10.3.1

update5:BT日志

写请求

enter image description here


回复

enter image description here

解决方法:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐