在Android上订阅ANCS

mbm*_*bmc 5 notifications android ios bluetooth-lowenergy ancs

根据此处列出的服务,我正在尝试从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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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";
}
Run Code Online (Sandbox Code Playgroud)

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());
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

更新:连接到在iPhone上发布的服务后,我可以找到服务和特征。但是,onCharacteristicChanged在iPhone上收到通知后,不会触发订阅通知。

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

update3:此示例中使用的部分代码

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

update5:BT日志

写请求 在此处输入图片说明

写回应 在此处输入图片说明

Kei*_*ith 3

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