Peripheral Class
Representation of a remote peripheral.
You can't directly create Peripheral
objects. Knowing the name
and/or UUID of a peripheral is not sufficient to create a connection,
especially because the UUID is a locally assigned artificial identifier,
not something that globally identifies a peripheral (like for example a
MAC address would).
You can use the
retrievePeripheralsWithIdentifiers
function to retrieve Peripheral
objects that were previously
created on the device. It is important to understand, however, that this
function really just retrieves already existing objects, doesn't create new
ones. So, for example, if you execute this function right after you reboot the
phone then chances are you won't get anything back.
Item Index
Methods
Events
- discoveredCharacteristics
- discoveredDescriptionForCharacteristics
- discoveredIncludedServices
- discoveredServices
- invalidatedServices
- updatedName
- updatedNotificationStateForCharacteristics
- updatedRSSI
- updatedValueForCharacteristic
- updatedValueForDescriptor
- wroteValueForCharacteristic
- wroteValueForDescriptor
Methods
discoverCharacteristics
-
params
-
[param.UUIDs]
Discovers the specified characteristic(s) of a service.
The result of this operation is returned via the discoveredCharacteristics event.
Parameters:
-
params
ObjectAn object with the following properties:
-
service
Service
-
-
[param.UUIDs]
Array of Strings optionalAn array of UUID strings, representing the characteristic types to be discovered. If not specified all characteristics of the service will be discovered, which is considerably slower and not recommended.
Returns:
discoverDescriptorsForCharacteristic
-
characteristic
Discovers the descriptor(s) of a characteristic.
The result of this operation is returned via the Peripheral/discoveredDescriptorsForCharacteristics:event event.
Parameters:
-
characteristic
Characteristic
Returns:
discoverIncludedServices
-
params
Discovers the specified included service(s) of a service.
The result of this operation is returned via the discoveredIncludedServices event.
Parameters:
-
params
ObjectAn object with the following properties:
-
service
ServiceA GATT service.
-
[serviceUUIDs]
Array of Strings optionalAn array of UUID strings, representing the included service types to be discovered. If not specified, all included services will be discovered, which is considerably slower and not recommended.
-
Returns:
discoverServices
-
serviceUUIDStrings
Discovers available service(s) on the peripheral.
The result of this operation is returned via the discoveredServices event.
Parameters:
-
serviceUUIDStrings
Array of Strings
Returns:
readRSSI
()
Void
Retrieves the current RSSI of the link.
The result of this operation is returned via the updatedRSSI event.
Returns:
readValueForCharacteristic
-
characteristic
Reads the value of a characteristic.
The result of this operation is returned via the updatedValueForCharacteristic event.
Parameters:
-
characteristic
Characteristic
Returns:
readValueForDescriptor
-
descriptor
Reads the value of a descriptor.
The result of this operation is returned via the updatedValueForDescriptor event.
Parameters:
-
descriptor
Descriptor
Returns:
subscribeToCharacteristic
-
characteristic
Enables notifications/indications for a characteristic. If the characteristic allows both, notifications will be used.
Parameters:
-
characteristic
Characteristic
Returns:
unsubscribeFromCharacteristic
-
characteristic
Disables notifications/indications for a characteristic.
Parameters:
-
characteristic
Characteristic
Returns:
writeValueForCharacteristic
-
params
Writes a value to a characteristic.
Parameters:
-
params
ObjectAn object with the following properties:
-
characteristic
CharacteristicThe characteristic to change.
-
data
TiBufferThe new value of the characteristic.
-
[type=CHARACTERISTIC_TYPE_WRITE_WITH_RESPONSE]
Number optionalThe type of write to be executed. See CHARACTERISTIC_TYPE_WRITE_WITH_RESPONSE and CHARACTERISTIC_TYPE_WRITE_WITHOUT_RESPONSE.
-
Returns:
writeValueForDescriptor
-
params
Writes the value of a descriptor.
The result of this operation is returned via the wroteValueForCharacteristic event.
Parameters:
-
params
Object-
descriptor
Descriptor -
data
TiBuffer
-
Returns:
Properties
isConnected
Boolean
read-only
Whether or not the peripheral is currently connected.
name
String
read-only
The name of the peripheral.
RSSI
Number
read-only
The most recently read RSSI, in decibels. Note that you need to call the readRSSI method to update this value, and the result of that operation is returned via the updatedRSSI event.
services
Array of Service objects
read-only
A list of Service objects that have been discovered on the peripheral.
state
Number
read-only
The current connection state of the peripheral. Possible values are PERIPHERAL_STATE_DISCONNECTED, PERIPHERAL_STATE_CONNECTING, and PERIPHERAL_STATE_CONNECTED.
UUID
String
read-only
Once a peripheral has been connected at least once by the system, it is assigned a UUID. This UUID can be stored and later provided to the retrievePeripherals method.
Events
discoveredCharacteristics
This event returns the result of a call to the
discoverCharacteristics method. If the characteristic(s) were read successfully,
they can be retrieved via the service
property's
characteristics
property.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
service
ServiceThe service for which the characteristics were discovered.
-
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
discoveredDescriptionForCharacteristics
This event returns the result of a call to the discoverDescriptorsForCharacteristic method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
characteristic
Characteristic -
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
discoveredIncludedServices
This event returns the result of a call to the discoverIncludedServices method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
discoveredServices
This event returns the result of a call to the discoverServices method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
invalidatedServices
This event is fired when the services of the peripheral change.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
services
Array of Service objectsThe services that have been invalidated.
updatedName
This event is fired when the name of the peripheral changes.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
updatedNotificationStateForCharacteristics
This event returns the result of a call to the subscribeToCharacteristic or unsubscribeFromCharacteristic method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
characteristic
Characteristic -
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
updatedRSSI
This event returns the result of a call to the
readRSSI method. The event handler function for this event can
query the RSSI property of the peripheral object provided in the
source
property of the event object.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
updatedValueForCharacteristic
This event is fired after a call to the readValueForCharacteristic method, or upon receipt of a notification/indication.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
characteristic
Characteristic -
value
TiBufferThe characteristic's value at the time the update was received. Note that due to the asynchronous nature of the Titanium messaging facility, the value of the characteristic may have been already updated again by the time the JavaScript code receives this event and consequently
value
andcharacteristic.value
may be different! -
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
updatedValueForDescriptor
This event returns the result of a call to the readValueForDescriptor method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
descriptor
DescriptorThe Descriptor object corresponding to the descriptor parameter of the delegate method.
-
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
wroteValueForCharacteristic
This event returns the result of a call to the writeValueForCharacteristic method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
characteristic
Characteristic -
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.
wroteValueForDescriptor
This event returns the result of a call to the writeValueForDescriptor method.
Event Payload:
-
source
PeripheralThe peripheral sending the event.
-
descriptor
DescriptorThe object representing the descriptor that was changed.
-
errorCode
NumberThe error code; only present if an error occurred.
-
errorDomain
StringThe error domain; only present if an error occurred.
-
errorDescription
StringThe error description; only present if an error occurred.