Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marchbold/ca04781574881dbca7ec to your computer and use it in GitHub Desktop.
Save marchbold/ca04781574881dbca7ec to your computer and use it in GitHub Desktop.
Bluetooth LE: Discover services and characteristics of a connected peripheral
var peripheral:Peripheral = ...; // This snippet assumes you have a connected 'peripheral' object.
var service:Service = null;
peripheral.addEventListener( PeripheralEvent.DISCOVER_SERVICES, peripheral_discoverServicesHandler );
peripheral.addEventListener( PeripheralEvent.DISCOVER_CHARACTERISTICS, peripheral_discoverCharacteristicsHandler );
peripheral.discoverServices();
...
private function peripheral_discoverServicesHandler( event:PeripheralEvent ):void
{
trace( "peripheral discover services: " + event.peripheral.name );
if (event.peripheral.services.length > 0)
{
for each (var service:Service in event.peripheral.services)
{
trace( "service: "+ service.uuid );
}
// As an example discover the characteristics of the first available service
peripheral.discoverCharacteristics( peripheral.services[0] );
}
}
private function peripheral_discoverCharacteristicsHandler( event:PeripheralEvent ):void
{
trace( "peripheral discover characteristics: " + event.peripheral.name );
for each (var service:Service in event.peripheral.services)
{
trace( "service: "+ service.uuid );
for each (var ch:Characteristic in service.characteristics)
{
trace( "characteristic: "+ch.uuid );
}
}
}
// com.distriqt.BluetoothLE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment