在Android上订阅ANCS

时间:2017-04-18 00:11:05

标签: android ios notifications bluetooth-lowenergy 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:所有写入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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

我最近遇到了类似的问题。我最终要解决的问题是向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);
}

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