Hardware Reference
In-Depth Information
mBleWrapper . startScanning ();
break ;
case R . id . action_stop :
mBleWrapper . stopScanning ();
break ;
default :
break ;
}
return super . onOptionsItemSelected ( item );
}
This code overrides the OptionsItemSelected() function to install your own handler.
This method is called only when processing events related to the Options menu. In the
switch statement, you decide which handler to call based on the item ID of the button
that was clicked. This item ID is based on the menu item ID of the buttons you created
previously. The R.id prefix means that it's an ID from the resource directory of the
internal Android project and not part of the wider android namespace.
The two actions in the new handler will either start the scan or stop the scanning pro‐
cedure, as you can see in the two methods from the BleWrapper . In fact, starting and
stopping the scanning procedure is probably the easiest part of the whole setup to im‐
plement the button functionality.
The next step is the action to take when a remote device is detected. A remote device
will be advertising it's there and ready to be connected to. When the program goes into
scanning mode, it turns on scanning in the BLE radio and lets the Android system know
that it wants to be informed if it get any advertisements. The Android Bluetooth library
will inform the program via callback.
To make use of the callback, you're going to revisit the code you wrote in the on
Create() method. Inside that method, you initialized your BleWrapper . Inside the
constructor of the BleWrapper , it expects you to provide a list of callbacks. You previ‐
ously left that empty, but now you're going to start implementing the callbacks. The first
one will override the uiDeviceFound() callback, which will get called by the library
whenever a device is found during the scan:
mBleWrapper = new BleWrapper ( this , new BleWrapperUiCallbacks . Null ()
{
@Override
public void uiDeviceFound ( final BluetoothDevice device ,
final int rssi ,
final byte [] record
)
{
String msg = "uiDeviceFound: " + device . getName ()+ ", " + rssi + ",
" + rssi . toString ();
Search WWH ::




Custom Search