|
35 | 35 | # for __version__
|
36 | 36 | import adafruit_ble
|
37 | 37 |
|
38 |
| -from bleio import Attribute, Characteristic, Peripheral, UUID |
| 38 | +from bleio import Attribute, Characteristic, Descriptor, Peripheral, Service, UUID |
39 | 39 | from .advertising import ServerAdvertisement
|
40 | 40 | from .device_information_service import DeviceInformationService
|
41 | 41 |
|
@@ -215,74 +215,77 @@ def __init__(self, name=None, tx_power=0):
|
215 | 215 | self._periph, software_revision=adafruit_ble.__version__,
|
216 | 216 | manufacturer="Adafruit Industries")
|
217 | 217 |
|
218 |
| - service = self._periph.add_service(UUID(_HID_SERVICE_UUID_NUM)) |
| 218 | + hid_service = Service.add_to_peripheral(UUID(_HID_SERVICE_UUID_NUM)) |
219 | 219 |
|
220 | 220 | self._input_chars = {}
|
221 | 221 | for report_id in sorted(self._INPUT_REPORT_SIZES.keys()):
|
222 |
| - input_char = service.add_characteristic( |
223 |
| - self._REPORT_UUID, properties=Characteristic.READ | Characteristic.NOTIFY, |
| 222 | + input_char = Characteristic.add_to_service( |
| 223 | + hid_service, self._REPORT_UUID, |
| 224 | + properties=Characteristic.READ | Characteristic.NOTIFY, |
224 | 225 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.NO_ACCESS,
|
225 | 226 | max_length=self._INPUT_REPORT_SIZES[report_id], fixed_length=True)
|
226 |
| - input_char.add_descriptor( |
227 |
| - self._REPORT_REF_DESCR_UUID, |
| 227 | + Descriptor.add_to_characteristic( |
| 228 | + input_char, self._REPORT_REF_DESCR_UUID, |
228 | 229 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.NO_ACCESS,
|
229 | 230 | initial_value=struct.pack('<BB', report_id, _REPORT_TYPE_INPUT))
|
230 | 231 | self._input_chars[report_id] = input_char
|
231 | 232 |
|
232 | 233 | self._output_chars = {}
|
233 | 234 | for report_id in sorted(self._OUTPUT_REPORT_SIZES.keys()):
|
234 |
| - output_char = service.add_characteristic( |
235 |
| - self._REPORT_UUID, |
| 235 | + output_char = Characteristic.add_to_service( |
| 236 | + hid_service, self._REPORT_UUID, |
236 | 237 | properties=(Characteristic.READ | Characteristic.WRITE |
|
237 | 238 | Characteristic.WRITE_NO_RESPONSE),
|
238 | 239 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.ENCRYPT_NO_MITM,
|
239 | 240 | max_length=self._OUTPUT_REPORT_SIZES[report_id], fixed_length=True)
|
240 |
| - output_char.add_descriptor( |
241 |
| - self._REPORT_REF_DESCR_UUID, |
| 241 | + Descriptor.add_to_characteristic( |
| 242 | + output_char, self._REPORT_REF_DESCR_UUID, |
242 | 243 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.NO_ACCESS,
|
243 | 244 | initial_value=struct.pack('<BB', report_id, _REPORT_TYPE_OUTPUT))
|
244 | 245 | self._output_chars[report_id] = output_char
|
245 | 246 |
|
246 | 247 | # Protocol mode: boot or report. Make it read-only for now because
|
247 | 248 | # it can't be changed
|
248 | 249 |
|
249 |
| - service.add_characteristic( |
250 |
| - UUID(_PROTOCOL_MODE_UUID_NUM), |
| 250 | + Characteristic.add_to_service( |
| 251 | + hid_service, UUID(_PROTOCOL_MODE_UUID_NUM), |
251 | 252 | properties=Characteristic.READ | Characteristic.WRITE_NO_RESPONSE,
|
252 | 253 | read_perm=Attribute.OPEN, write_perm=Attribute.OPEN,
|
253 | 254 | max_length=1, fixed_length=True,
|
254 | 255 | initial_value=_PROTOCOL_MODE_REPORT)
|
255 | 256 |
|
256 |
| - service.add_characteristic( |
257 |
| - UUID(_BOOT_KEYBOARD_INPUT_REPORT_UUID_NUM), |
| 257 | + Characteristic.add_to_service( |
| 258 | + hid_service, UUID(_BOOT_KEYBOARD_INPUT_REPORT_UUID_NUM), |
258 | 259 | properties=Characteristic.READ | Characteristic.NOTIFY,
|
259 | 260 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.NO_ACCESS,
|
260 | 261 | max_length=self._INPUT_REPORT_SIZES[self.REPORT_ID_KEYBOARD], fixed_length=True)
|
261 | 262 |
|
262 |
| - service.add_characteristic( |
263 |
| - UUID(_BOOT_KEYBOARD_OUTPUT_REPORT_UUID_NUM), |
| 263 | + Characteristic.add_to_service( |
| 264 | + hid_service, UUID(_BOOT_KEYBOARD_OUTPUT_REPORT_UUID_NUM), |
264 | 265 | properties=(Characteristic.READ | Characteristic.WRITE |
|
265 | 266 | Characteristic.WRITE_NO_RESPONSE),
|
266 | 267 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.ENCRYPT_NO_MITM,
|
267 | 268 | max_length=self._OUTPUT_REPORT_SIZES[self.REPORT_ID_KEYBOARD], fixed_length=True)
|
268 | 269 |
|
269 | 270 | # This is the USB HID descriptor (not to be confused with a BLE Descriptor).
|
270 |
| - service.add_characteristic( |
271 |
| - UUID(_REPORT_MAP_UUID_NUM), properties=Characteristic.READ, |
| 271 | + Characteristic.add_to_service( |
| 272 | + hid_service, UUID(_REPORT_MAP_UUID_NUM), properties=Characteristic.READ, |
272 | 273 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.NO_ACCESS,
|
273 | 274 | max_length=len(self.HID_DESCRIPTOR), fixed_length=True,
|
274 | 275 | initial_value=self.HID_DESCRIPTOR)
|
275 | 276 |
|
276 | 277 | # bcdHID (version), bCountryCode (0 not localized), Flags: RemoteWake, NormallyConnectable
|
277 | 278 | # bcd1.1, country = 0, flag = normal connect
|
278 |
| - service.add_characteristic( |
279 |
| - UUID(_HID_INFORMATION_UUID_NUM), properties=Characteristic.READ, |
| 279 | + Characteristic.add_to_service( |
| 280 | + hid_service, UUID(_HID_INFORMATION_UUID_NUM), |
| 281 | + properties=Characteristic.READ, |
280 | 282 | read_perm=Attribute.ENCRYPT_NO_MITM, write_perm=Attribute.NO_ACCESS,
|
281 | 283 | initial_value=b'\x01\x01\x00\x02')
|
282 | 284 |
|
283 | 285 | # 0 = suspend; 1 = exit suspend
|
284 |
| - service.add_characteristic( |
285 |
| - UUID(_HID_CONTROL_POINT_UUID_NUM), properties=Characteristic.WRITE_NO_RESPONSE, |
| 286 | + Characteristic.add_to_service( |
| 287 | + hid_service, UUID(_HID_CONTROL_POINT_UUID_NUM), |
| 288 | + properties=Characteristic.WRITE_NO_RESPONSE, |
286 | 289 | read_perm=Attribute.NO_ACCESS, write_perm=Attribute.ENCRYPT_NO_MITM)
|
287 | 290 |
|
288 | 291 | self._advertisement = ServerAdvertisement(self._periph, tx_power=tx_power,
|
|
0 commit comments