ble 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/ble.gemspec +2 -3
- data/lib/ble.rb +1 -0
- data/lib/ble/characteristic.rb +20 -1
- data/lib/ble/db_ollie.rb +31 -0
- data/lib/ble/device.rb +60 -26
- data/lib/ble/version.rb +4 -1
- metadata +19 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 17aaccf4610ffbc88f45cea4e7551b54bf44618c725dac2d00d6bafe64510fda
|
4
|
+
data.tar.gz: fca52cb329e71cf3a3c275748fa2efdb6ec9884382f518a203a3bb7d299b720d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f50cfc18e960210812508fc2db9520c091509a1cb9cc6c83e39de9e9978ba05a7fa788ac8e9cffebf988df40f6a96361568013d472fe1c8fbe8fae308a3338a0
|
7
|
+
data.tar.gz: 9bc27795aa5926ca1693a8c50d8dcc5a5e166680fccb975e65530ed4b21df0acd29c9bacad06f41ce447e578fb622dc3dc95f045bce5b758527faa595e4fe751
|
data/ble.gemspec
CHANGED
@@ -7,19 +7,18 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = BLE::VERSION
|
8
8
|
s.authors = [ "Stephane D'Alu" ]
|
9
9
|
s.email = [ "stephane.dalu@gmail.com" ]
|
10
|
-
s.homepage = "http://
|
10
|
+
s.homepage = "http://gitlab.com/sdalu/ruby-ble"
|
11
11
|
s.summary = "Bluetooth Low Energy (BLE) API"
|
12
12
|
s.description = "Allow access to Bluetooth Low Energy device from ruby"
|
13
13
|
|
14
14
|
s.add_dependency "ruby-dbus"
|
15
|
+
s.add_dependency "concurrent-ruby"
|
15
16
|
|
16
17
|
s.add_development_dependency "yard"
|
17
18
|
s.add_development_dependency "rake"
|
18
19
|
s.add_development_dependency "redcarpet"
|
19
20
|
s.add_development_dependency "github-markup"
|
20
21
|
|
21
|
-
s.has_rdoc = 'yard'
|
22
|
-
|
23
22
|
s.license = 'MIT'
|
24
23
|
|
25
24
|
|
data/lib/ble.rb
CHANGED
data/lib/ble/characteristic.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'forwardable'
|
2
|
+
require 'concurrent'
|
2
3
|
module BLE
|
3
4
|
# Build information about {https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx Bluetooth Characteristics}
|
4
5
|
#
|
@@ -39,7 +40,16 @@ module BLE
|
|
39
40
|
|
40
41
|
def write(val, raw: false)
|
41
42
|
val= _serialize_value(val, raw: raw)
|
42
|
-
@dbus_obj[I_GATT_CHARACTERISTIC].WriteValue(val)
|
43
|
+
@dbus_obj[I_GATT_CHARACTERISTIC].WriteValue(val, [])
|
44
|
+
end
|
45
|
+
|
46
|
+
def async_write(val, raw: false)
|
47
|
+
val= _serialize_value(val, raw: raw)
|
48
|
+
Concurrent::Promise.execute do
|
49
|
+
@dbus_obj[I_GATT_CHARACTERISTIC].WriteValue(val, []) do |result|
|
50
|
+
result
|
51
|
+
end
|
52
|
+
end
|
43
53
|
end
|
44
54
|
|
45
55
|
def read(raw: false)
|
@@ -47,6 +57,15 @@ module BLE
|
|
47
57
|
val= _deserialize_value(val, raw: raw)
|
48
58
|
end
|
49
59
|
|
60
|
+
def async_read(raw: false)
|
61
|
+
return Concurrent::Promise.execute do
|
62
|
+
@dbus_obj[I_GATT_CHARACTERISTIC].ReadValue() do |result|
|
63
|
+
val= result.first
|
64
|
+
val= _deserialize_value(val, raw: raw)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
50
69
|
# Register to this characteristic for notifications when
|
51
70
|
# its value changes.
|
52
71
|
def notify!
|
data/lib/ble/db_ollie.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module BLE
|
2
|
+
module Service
|
3
|
+
add '22bb746f-2bb0-7554-2d6f-726568705327',
|
4
|
+
name: 'Sphero Ollie (BB-8) BLE Service',
|
5
|
+
nick: :ollie_ble_service
|
6
|
+
add '22bb746f-2ba0-7554-2d6f-726568705327',
|
7
|
+
name: 'Sphero Ollie (BB-8) Robot Control Service',
|
8
|
+
nick: :ollie_robot_control_service
|
9
|
+
end
|
10
|
+
class Characteristic
|
11
|
+
add '22bb746f-2bbf-7554-2d6f-726568705327',
|
12
|
+
name: 'Ollie Wake',
|
13
|
+
nick: :ollie_wake
|
14
|
+
|
15
|
+
add '22bb746f-2bb2-7554-2d6f-726568705327',
|
16
|
+
name: 'Ollie TX Power',
|
17
|
+
nick: :ollie_tx_power
|
18
|
+
|
19
|
+
add '22bb746f-2bbd-7554-2d6f-726568705327',
|
20
|
+
name: 'Ollie AntiDOS',
|
21
|
+
nick: :ollie_antidos
|
22
|
+
|
23
|
+
add '22bb746f-2ba1-7554-2d6f-726568705327',
|
24
|
+
name: 'Ollie Commands',
|
25
|
+
nick: :ollie_commands
|
26
|
+
|
27
|
+
add '22bb746f-2ba6-7554-2d6f-726568705327',
|
28
|
+
name: 'Ollie Response',
|
29
|
+
nick: :ollie_response
|
30
|
+
end
|
31
|
+
end
|
data/lib/ble/device.rb
CHANGED
@@ -12,6 +12,8 @@ module BLE
|
|
12
12
|
# Notify that you need to have the device in a connected state
|
13
13
|
class NotConnected < Error ; end
|
14
14
|
|
15
|
+
attr_accessor :requires_connection
|
16
|
+
|
15
17
|
# @param adapter [String] adapter unix device name
|
16
18
|
# @param dev [String] device MAC address
|
17
19
|
# @param auto_refresh [Boolean] gather information about device
|
@@ -49,7 +51,8 @@ module BLE
|
|
49
51
|
# It will remove also the pairing information.
|
50
52
|
# @return [Boolean]
|
51
53
|
def remove
|
52
|
-
@o_adapter[I_ADAPTER].RemoveDevice(@p_dev)
|
54
|
+
block_given? ? @o_adapter[I_ADAPTER].RemoveDevice(@p_dev, &Proc.new) :
|
55
|
+
@o_adapter[I_ADAPTER].RemoveDevice(@p_dev)
|
53
56
|
true
|
54
57
|
rescue DBus::Error => e
|
55
58
|
case e.name
|
@@ -75,18 +78,19 @@ module BLE
|
|
75
78
|
#
|
76
79
|
# @return [Boolean]
|
77
80
|
def pair
|
78
|
-
@o_dev[I_DEVICE].Pair
|
81
|
+
block_given? ? @o_dev[I_DEVICE].Pair(&Proc.new) :
|
82
|
+
@o_dev[I_DEVICE].Pair()
|
79
83
|
true
|
80
84
|
rescue DBus::Error => e
|
81
85
|
case e.name
|
82
86
|
when E_INVALID_ARGUMENTS then false
|
83
87
|
when E_FAILED then false
|
84
88
|
when E_ALREADY_EXISTS then true
|
85
|
-
when E_AUTH_CANCELED then raise
|
86
|
-
when E_AUTH_FAILED then raise
|
87
|
-
when E_AUTH_REJECTED then raise
|
88
|
-
when E_AUTH_TIMEOUT then raise
|
89
|
-
when E_AUTH_ATTEMPT_FAILED then raise
|
89
|
+
when E_AUTH_CANCELED then raise NotAuthorized
|
90
|
+
when E_AUTH_FAILED then raise NotAuthorized
|
91
|
+
when E_AUTH_REJECTED then raise NotAuthorized
|
92
|
+
when E_AUTH_TIMEOUT then raise NotAuthorized
|
93
|
+
when E_AUTH_ATTEMPT_FAILED then raise NotAuthorized
|
90
94
|
else raise ScriptError
|
91
95
|
end
|
92
96
|
end
|
@@ -95,7 +99,8 @@ module BLE
|
|
95
99
|
# operation initiated by the Pair method.
|
96
100
|
# @return [Boolean]
|
97
101
|
def cancel_pairing
|
98
|
-
@o_dev[I_DEVICE].CancelPairing
|
102
|
+
block_given? ? @o_dev[I_DEVICE].CancelPairing(&Proc.new) :
|
103
|
+
@o_dev[I_DEVICE].CancelPairing()
|
99
104
|
true
|
100
105
|
rescue DBus::Error => e
|
101
106
|
case e.name
|
@@ -116,9 +121,11 @@ module BLE
|
|
116
121
|
def connect(profile=:all)
|
117
122
|
case profile
|
118
123
|
when UUID::REGEX
|
119
|
-
@o_dev[I_DEVICE].ConnectProfile(profile)
|
124
|
+
block_given? ? @o_dev[I_DEVICE].ConnectProfile(profile, &Proc.new) :
|
125
|
+
@o_dev[I_DEVICE].ConnectProfile(profile)
|
120
126
|
when :all
|
121
|
-
@o_dev[I_DEVICE].Connect()
|
127
|
+
block_given? ? @o_dev[I_DEVICE].Connect(&Proc.new) :
|
128
|
+
@o_dev[I_DEVICE].Connect()
|
122
129
|
else raise ArgumentError, "profile uuid or :all expected"
|
123
130
|
end
|
124
131
|
true
|
@@ -149,9 +156,11 @@ module BLE
|
|
149
156
|
def disconnect(profile=:all)
|
150
157
|
case profile
|
151
158
|
when UUID::REGEX
|
152
|
-
@o_dev[I_DEVICE].DisconnectProfile(profile)
|
159
|
+
block_given? ? @o_dev[I_DEVICE].DisconnectProfile(profile, &Proc.new) :
|
160
|
+
@o_dev[I_DEVICE].DisconnectProfile(profile)
|
153
161
|
when :all
|
154
|
-
@o_dev[I_DEVICE].Disconnect()
|
162
|
+
block_given? ? @o_dev[I_DEVICE].Disconnect(&Proc.new) :
|
163
|
+
@o_dev[I_DEVICE].Disconnect()
|
155
164
|
else raise ArgumentError, "profile uuid or :all expected"
|
156
165
|
end
|
157
166
|
true
|
@@ -185,7 +194,7 @@ module BLE
|
|
185
194
|
|
186
195
|
# Indicates if the remote device is currently connected.
|
187
196
|
def is_connected?
|
188
|
-
@o_dev[I_DEVICE]['Connected']
|
197
|
+
!@requires_connection || @o_dev[I_DEVICE]['Connected']
|
189
198
|
rescue DBus::Error => e
|
190
199
|
case e.name
|
191
200
|
when E_UNKNOWN_OBJECT
|
@@ -343,12 +352,14 @@ module BLE
|
|
343
352
|
# @return [self]
|
344
353
|
def refresh!
|
345
354
|
_require_connection!
|
346
|
-
max_wait ||= 1.5
|
347
|
-
@services = Hash[@o_dev
|
355
|
+
max_wait ||= 1.5 # Use ||= due to the retry
|
356
|
+
@services = Hash[@o_dev.subnodes.map {|p_srv|
|
357
|
+
p_srv = [@o_dev.path, p_srv].join '/'
|
348
358
|
o_srv = BLUEZ.object(p_srv)
|
349
359
|
o_srv.introspect
|
350
|
-
srv = o_srv
|
351
|
-
char = Hash[
|
360
|
+
srv = o_srv.GetAll(I_GATT_SERVICE).first
|
361
|
+
char = Hash[o_srv.subnodes.map {|char|
|
362
|
+
p_char = [o_srv.path, char].join '/'
|
352
363
|
o_char = BLUEZ.object(p_char)
|
353
364
|
o_char.introspect
|
354
365
|
uuid = o_char[I_GATT_CHARACTERISTIC]['UUID' ].downcase
|
@@ -387,7 +398,7 @@ module BLE
|
|
387
398
|
# @raise [Service::NotFound, Characteristic::NotFound] if service/characteristic doesn't exist on this device
|
388
399
|
# @raise [AccessUnavailable] if not available for reading
|
389
400
|
# @return [Object]
|
390
|
-
def [](service, characteristic, raw: false)
|
401
|
+
def [](service, characteristic, raw: false, async: false)
|
391
402
|
_require_connection!
|
392
403
|
uuid = _uuid_characteristic(characteristic)
|
393
404
|
chars = _characteristics(service)
|
@@ -396,7 +407,7 @@ module BLE
|
|
396
407
|
raise Characteristic::NotFound, characteristic if char.nil?
|
397
408
|
|
398
409
|
if char.flag?('read')
|
399
|
-
|
410
|
+
async ? char.async_read(raw: raw) : char.read(raw: raw)
|
400
411
|
elsif char.flag?('encrypt-read') ||
|
401
412
|
char.flag?('encrypt-authenticated-read')
|
402
413
|
raise NotYetImplemented
|
@@ -404,6 +415,10 @@ module BLE
|
|
404
415
|
raise AccessUnavailable
|
405
416
|
end
|
406
417
|
end
|
418
|
+
alias read []
|
419
|
+
def async_read service, characteristic, raw: false
|
420
|
+
read service, characteristic, raw: raw, async: true
|
421
|
+
end
|
407
422
|
|
408
423
|
# Set value for a service/characteristic
|
409
424
|
#
|
@@ -416,24 +431,43 @@ module BLE
|
|
416
431
|
# @raise [Service::NotFound, Characteristic::NotFound] if service/characteristic doesn't exist on this device
|
417
432
|
# @raise [AccessUnavailable] if not available for writing
|
418
433
|
# @return [void]
|
419
|
-
def []=(service, characteristic, val, raw: false)
|
434
|
+
def []=(service, characteristic, val, raw: false, async: false)
|
420
435
|
_require_connection!
|
421
436
|
uuid = _uuid_characteristic(characteristic)
|
422
437
|
chars = _characteristics(service)
|
423
|
-
raise
|
438
|
+
raise Service::NotFound, service if chars.nil?
|
424
439
|
char = chars[uuid]
|
425
|
-
raise
|
440
|
+
raise Characteristic::NotFound, characteristic if char.nil?
|
426
441
|
|
427
442
|
if char.flag?('write') ||
|
428
|
-
|
429
|
-
char.write(val, raw: raw)
|
443
|
+
char.flag?('write-without-response')
|
444
|
+
async ? char.async_write(val, raw: raw) : char.write(val, raw: raw)
|
430
445
|
elsif char.flag?('encrypt-write') ||
|
431
446
|
char.flag?('encrypt-authenticated-write')
|
432
447
|
raise NotYetImplemented
|
433
448
|
else
|
434
449
|
raise AccessUnavailable
|
435
450
|
end
|
436
|
-
|
451
|
+
end
|
452
|
+
alias write []=
|
453
|
+
def async_write(service, characteristic, val, raw: false)
|
454
|
+
write service, characteristic, val, raw: raw, async: true
|
455
|
+
end
|
456
|
+
|
457
|
+
def subscribe(service, characteristic, raw: false, &block)
|
458
|
+
_require_connection!
|
459
|
+
uuid = _uuid_characteristic(characteristic)
|
460
|
+
chars = _characteristics(service)
|
461
|
+
raise Service::NotFound, service if chars.nil?
|
462
|
+
char = chars[uuid]
|
463
|
+
raise Characteristic::NotFound, characteristic if char.nil?
|
464
|
+
|
465
|
+
if char.flag? 'notify'
|
466
|
+
char.notify!
|
467
|
+
char.on_change(raw: raw, &block)
|
468
|
+
else
|
469
|
+
raise OperationNotSupportedError.new("No notifications available for characteristic #{characteristic}")
|
470
|
+
end
|
437
471
|
end
|
438
472
|
|
439
473
|
#---------------------------------
|
@@ -480,7 +514,7 @@ module BLE
|
|
480
514
|
characteristic.downcase
|
481
515
|
else
|
482
516
|
if char = Characteristic[characteristic]
|
483
|
-
char
|
517
|
+
char[:uuid]
|
484
518
|
end
|
485
519
|
end
|
486
520
|
if uuid.nil?
|
data/lib/ble/version.rb
CHANGED
@@ -4,6 +4,9 @@ module BLE
|
|
4
4
|
# Anything may change at any time.
|
5
5
|
# The public API should not be considered stable.
|
6
6
|
|
7
|
+
# 1.0.0 MAJOR
|
8
|
+
# - [FEATURE] Support BLUEZ 5.36 (breaking change in DBus API)
|
9
|
+
|
7
10
|
# 0.1.0 MINOR
|
8
11
|
# - [FEATURE] BLE notifications functionality.
|
9
12
|
# - [REFACTOR] Breaks Characteristic API
|
@@ -11,5 +14,5 @@ module BLE
|
|
11
14
|
# 0.0.3 PATCH
|
12
15
|
# - [FIX] NotSupported exeption was not declared.
|
13
16
|
#
|
14
|
-
VERSION = '
|
17
|
+
VERSION = '1.0.0'
|
15
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ble
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephane D'Alu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-dbus
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: concurrent-ruby
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: yard
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,6 +112,7 @@ files:
|
|
98
112
|
- lib/ble/characteristic.rb
|
99
113
|
- lib/ble/db_eddystone.rb
|
100
114
|
- lib/ble/db_nordic.rb
|
115
|
+
- lib/ble/db_ollie.rb
|
101
116
|
- lib/ble/db_sig_characteristic.rb
|
102
117
|
- lib/ble/db_sig_service.rb
|
103
118
|
- lib/ble/device.rb
|
@@ -105,7 +120,7 @@ files:
|
|
105
120
|
- lib/ble/service.rb
|
106
121
|
- lib/ble/uuid.rb
|
107
122
|
- lib/ble/version.rb
|
108
|
-
homepage: http://
|
123
|
+
homepage: http://gitlab.com/sdalu/ruby-ble
|
109
124
|
licenses:
|
110
125
|
- MIT
|
111
126
|
metadata: {}
|
@@ -125,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
140
|
version: '0'
|
126
141
|
requirements: []
|
127
142
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.7.7
|
129
144
|
signing_key:
|
130
145
|
specification_version: 4
|
131
146
|
summary: Bluetooth Low Energy (BLE) API
|