Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marchbold/92dd6e31121cab01f37e to your computer and use it in GitHub Desktop.
Save marchbold/92dd6e31121cab01f37e to your computer and use it in GitHub Desktop.
Bluetooth LE: Responding to read requests
var characteristic:Characteristic = ...; // Some characteristic you have created and added to a published service
BluetoothLE.service.peripheralManager.addEventListener( RequestEvent.READ, peripheral_readRequestHandler );
...
private function peripheral_readRequestHandler( event:RequestEvent ):void
{
//
// Read requests will only have one object in the requests event
var request:Request = event.requests[0];
trace( "peripheral manager: read request: " + request.characteristic.uuid );
//
// Handle the read request by first checking the UUID and then responding with the requested data.
// You need to make sure you correctly handle the offset variable as illustrated below
if (request.characteristic.uuid == characteristic.uuid)
{
if (request.offset > characteristic.value.length)
{
BluetoothLE.service.peripheralManager.respondToRequest( request, Request.INVALID_OFFSET );
}
else
{
characteristic.value.position = 0;
var data:ByteArray = new ByteArray();
data.writeBytes( characteristic.value, request.offset, (characteristic.value.length - request.offset) );
BluetoothLE.service.peripheralManager.respondToRequest( request, Request.SUCCESS, data );
}
}
}
// com.distriqt.BluetoothLE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment