Module: BLE::Service

Defined in:
lib/ble/service.rb,
lib/ble/db_ollie.rb,
lib/ble/db_nordic.rb,
lib/ble/db_eddystone.rb,
lib/ble/db_sig_service.rb

Overview

Build information about Bluetooth Services

To add a new service description:

BLE::Service.add '63a83b9c-0fa0-4d04-8ef9-23be4ed36231',
    name: 'World domination',
    type: 'net.cortex-minus.service.world_domination'

Returned service description will be a hash:

{
   name: "Bluetooth service name",
   type: "org.bluetooth.service.name",
   uuid: "128bit-uuid-string"
}

Defined Under Namespace

Classes: NotFound

Class Method Summary collapse

Class Method Details

.[](id) ⇒ Hash

Get a service description from it's id. The id could be either a uuid, a type, or a nickname

Parameters:

  • id (Symbol, String)

    uuid, type or nickname

Returns:

  • (Hash)

    service description


32
33
34
35
36
37
38
39
40
# File 'lib/ble/service.rb', line 32

def self.[](id)
    case id
    when Symbol      then DB_NICKNAME[id]
    when UUID::REGEX then DB_UUID[id]
    when String      then DB_TYPE[id]
    when Integer     then DB_UUID[BLE::UUID(id)]
    else raise ArgumentError, "invalid type for service id"
    end
end

.add(uuid, name:, **opts) ⇒ Hash

Add a service description.

Examples:

Add a service description with a 16-bit uuid

module Service
    add 0x1800,
        name: 'Generic Access',
        type: 'org.bluetooth.service.generic_access'
end

Add a service description with a 128-bit uuid

module Service
    add '63a83b9c-0fa0-4d04-8ef9-23be4ed36231',
        name: 'World domination',
        type: 'net.cortex-minus.service.world_domination'
end

Parameters:

  • uuid (Integer, String)

    16-bit, 32-bit or 128-bit uuid

  • name (String)
  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :type (String)

    type

  • :nick (Symbol)

    nickname

Returns:

  • (Hash)

    service description


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ble/service.rb', line 86

def self.add(uuid, name:, **opts)
    uuid = BLE::UUID(uuid)
    type = opts.delete :type
    nick = opts.delete :nick
    if opts.first 
        raise ArgumentError, "unknown keyword: #{opts.first[0]}" 
    end
    
    desc = DB_UUID[uuid] = {
        uuid: uuid,
        name: name,
    }

    # Add type if specified
    if type
        type = type.downcase
        desc.merge!(type: type)
        DB_TYPE[type] = desc
    end

    # Add nickname if specified or can be derived from type
    if nick.nil? && type && type =~ /\.(?<key>[^.]+)$/
        nick = $1.to_sym if type.start_with? 'org.bluetooth.service'
    end
    if nick
        if DB_NICKNAME.include?(nick)
            raise ArgumentError,
                  "nickname '#{nick}' already registered (uuid: #{uuid})"
        end
        DB_NICKNAME[nick] = desc
    end
    
    desc
end

.by_nickname(id) ⇒ Hash

Get service description from nickname.

Parameters:

  • id (Symbol)

    nickname

Returns:

  • (Hash)

    service description


46
47
48
# File 'lib/ble/service.rb', line 46

def self.by_nickname(id)
    DB_NICKNAME[id]
end

.by_type(id) ⇒ Hash

Get service description from type

Parameters:

  • id (Strig)

    type

Returns:

  • (Hash)

    service description


62
63
64
# File 'lib/ble/service.rb', line 62

def self.by_type(id)
    DB_TYPE[id]
end

.by_uuid(id) ⇒ Hash

Get service description from uuid.

Parameters:

  • id (String)

    uuid

Returns:

  • (Hash)

    service description


54
55
56
# File 'lib/ble/service.rb', line 54

def self.by_uuid(id)
    DB_UUID[id]
end