The sample code below demonstrates how to retrieve the current values of the user settings:
#include <nfc/nfc.h>
bool ceAllowedWhenBacklightOffOrLocked = false;
bool ceAllowedWhenPoweredOff = false;
if (NFC_RESULT_SUCCESS ==
nfc_get_setting(NFC_SETTING_CE_BACKLIGHT_OFF,
&ceAllowedWhenBacklightOffOrLocked) &&
NFC_RESULT_SUCCESS ==
nfc_get_setting(NFC_SETTING_CE_POWERED_OFF,
&ceAllowedWhenPoweredOff)) {
printf("CE allowed when locked or backlight is off: %s\n",
ceAllowedWhenBacklightOffOrLocked ? "true" : "false");
printf("CE allowed when device is turned off: %s\n",
ceAllowedWhenPoweredOff ? "true" : "false");
} else {
// Handle error
}
The
sample code below uses nfc_set_setting() to
turn on the When device is turned off
setting. You can also use this function to turn the setting off, or to modify
the When locked or backlight is off
setting.
#include <nfc/nfc.h>
nfc_result_t res =
nfc_set_setting(NFC_SETTING_CE_POWERED_OFF, true);
if(res == NFC_RESULT_SUCCESS) {
// The dialog is currently on the screen; we need
// to wait for a BlackBerry Platform Services
// message with the actual result.
} else if(res == NFC_RESULT_RESOURCE_BUSY) {
// The user is already being prompted about a setting
// change (by this or another application). We should
// try again later.
} else {
// Handle error
}
Receive
the response to your request in a BlackBerry Platform Services event loop such as the following. An NFC_RESULT_SUCCESS event means the setting has been
successfully changed. In this code sample, it indicates that card transaction is
now enabled when the device is powered off.
#include <nfc/nfc_bps.h>
#include <nfc/nfc.h>
#include <bps/bps.h>
bps_initialize();
nfc_request_events();
for (;;) {
bps_event_t* event;
nfc_event_t* nfc_event;
nfc_event_type_t event_type;
int event_value;
bps_get_event(&event, -1);
if (bps_event_get_domain(event) == nfc_get_domain()
&& BPS_SUCCESS ==
nfc_get_nfc_event(event, &nfc_event)
&& NFC_RESULT_SUCCESS ==
nfc_get_event_type(nfc_event, &event_type)
&& NFC_CE_ENABLE_POWERED_OFF_EVENT == event_type
&& NFC_RESULT_SUCCESS ==
nfc_get_notification_value(nfc_event,
&event_value)) {
switch (event_value) {
case NFC_RESULT_SUCCESS:
// Handle the case where the setting
// was successfully changed
case NFC_RESULT_OPERATION_REJECTED:
// Handle the case where the user
// declined the setting change
default:
// Default error handling
}
} else {
// Handle uninteresting or invalid events
}
}