The android application is based on the tutorial by Dave Smith from NewCircle, except the bulk of the program is run in a service, instead of an activity.
https://github.com/devunwired/accessory-samples/tree/master/BluetoothGatt/src/com/example
The app is separated into three main classes:
- kDriveButton.java
- This class defines possible types of buttons. I was unsure at the time of development what kind of buttons I wanted on the hardware, so this class will make it easy to add different input elements with different characteristics. It will also make it possible for future devices to be created with different types of input elements. Possible types of input elements are:
- Navigation buttons, similar to arrow keys on a keyboard
- General buttons, can be used for general input, such as to launch an app, initiate a phone call, select a menu option, etc.
- Rotary wheel, a spinnable element similar to a scrolling wheel on a mouse.
- Rotary wheel clicker, spinnable element that can also click
- Slider, capacitive touch slider
- Joystick, directional joystick
- Each button has characteristics that describe how the button can operate. The characteristics are:
- --Clickable, double-clickable, long-clickable, scrollable, slideable
- kDrive.java
- This class provides methods to start/stop/bind/unbind the kdrive service, connect/disconnect BLE, initializes all of the buttons on the hardware (using the kDriveButton class), and defines the callbacks for when a button is pressed. In order to make the kDrive system easy to use for app developers, the idea is to have almost all of the work done in these three classes. An app developer who would like to incorporate kDrive actions in there app would need to write methods for the callbacks defined in kDrive.java. These callbacks include “onKDriveSliderChanged”, “onKDriveButtonPushed”, etc, which are automatically called when the service detects an event. These functions are implemented in an activity that wants to interact with the kDrive.
- kDriveService.java
- The main functionality of the kDrive system occurs in a service so that it can run in the background and hopefully be accessible by any app. This allows app developers to include kDrive functionality in their own apps. The service is based on the example from NewCircle (see link above).
The service progress through the following cases:
- kDrive.java initiates Bluetooth scan
- onLeScan callback is called, searches broadcasted devices found for name “kDrive”. Attemps to connect, if found
- If successful, attempt to discover GATT services
- If services discovered, enable GATT notifications
- Once notifications are enabled, the setup is complete.
- o Once the BLE connection is setup, the service will wait for the BLE device to send notifications to the mobile device. If a notification is detected, the callback “onCharacteristicChanged” will be run. Inside this method, we will process the type of input that was detected (button pushed, slider scrolled, etc). We will then pass this information back to kDrive.java, which will in turn launch the callback that the application developer has (hopefully) implemented.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.