IOS BLE 缓存清除

问题

在做一款基于蓝牙BLE通信的智能硬件,设备的蓝牙服务是动态变化的,发现 IOS 存在蓝牙服务缓存。
具体表现是,IOS 搜索发现过硬件的蓝牙服务后,断开蓝牙连接,设备重启更新了蓝牙服务, IOS 再次搜索,扫描到的蓝牙服务仍然是旧的。

原因

事实上,缓存现象不是 IOS 独有的,在 BLE 规范中定义了缓存标准,参考 蓝牙4.0规范 vol 3, Part G, 2.5.2 Attribute Caching。这是为了优化,在客户端重新连接服务端时,可以重用上次发现的蓝牙服务信息。

Attribute caching is an optimization that allows the client to discover the Attribute information such as Attribute Handles used by the server once and use
the same Attribute information across reconnections without rediscovery. Without caching the Attribute information, the client shall rediscover the Attribute
information at each reconnection. With caching, time is saved and a significant
amount of packets exchanged between the client and server is not required.
The Attribute information that shall be cached by a client is the Attribute Handles of all server attributes and the GATT service characteristics values.

解决

由于我们的硬件设备程序是自己开发的,所以可以通过在设备上开启 ** Generic Attribute Profile Service ** ,在蓝牙连接过程中,设备通过改服务向手机发送Service Changed Indication ,这样 IOS 系统内部会处理蓝牙服务缓存更新。

下面是一段基于 Dialog 芯片的设备端代码

void handle_ble_service_event (ble_evt_hdr * hdr){
    switch(hdr->evt_code){
  	case BLE_EVT_GAP_CONNECTED: {
  	    // 通知客户端更新 0x0001 ~ 0xFFFF 的服务缓存(即更新所有服务缓存)
  	    ble_gatts_service_changed_ind(evt->conn_idx, 0x0001, 0xFFFF);
  		break;
  }
  }
}

如果无法更改硬件程序,那么也可以手动在 IOS 的系统设置里,重新开启一次蓝牙开关,这样蓝牙缓存也会被清除。注意:必须是系统设置里的开关,快捷蓝牙开关无效。

IOS BLE 缓存清除_第1张图片

你可能感兴趣的:(嵌入式)