Create an XBee Android application from scratch
Follow these steps to create an XBee Android application:
- If not installed, download and install Android Studio. You can get it at https://developer.android.com/studio.
- Open Android Studio.
- In the Welcome screen, click Start a new Android Studio project and use the Empty Activity template. For instructions, see the Android Developers Guide.
- Open the project build.gradle and add the Digi Maven repository:
allprojects { repositories { google() jcenter() maven { url "http://ftp1.digi.com/support/m-repo" } } }
- Open the module build.gradle and add the XBee Library for Android as dependency:
dependencies { [...] implementation 'com.digi.xbee:xbee-android-library:1.0.0' }
- Replace the application's main Activity code with the following code:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.digi.xbee.api.android.XBeeBLEDevice; import com.digi.xbee.api.exceptions.BluetoothAuthenticationException; import com.digi.xbee.api.exceptions.XBeeException; public class MyActivity extends AppCompatActivity { // Constants. // TODO: replace with the Bluetooth MAC address of your XBee device. private static final String BLE_MAC_ADDR = "08:6B:D7:52:B3:7B"; // TODO: replace with the Bluetooth password of your XBee device. private static final String BLE_PASSWORD = "1234"; // Variables. private XBeeBLEDevice myXBeeDevice = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Instantiate the XBeeDevice object and open the connection. // This process blocks the UI, so it must be done in a different thread. new Thread(new Runnable() { @Override public void run() { // Instantiate an XBee BLE device with the Bluetooth MAC and password. myXBeeDevice = new XBeeBLEDevice(MyActivity.this, BLE_MAC_ADDR, BLE_PASSWORD); try { // Open the connection with the device. myXBeeDevice.open(); showToastMessage("Device open: " + myXBeeDevice.toString()); } catch (BluetoothAuthenticationException e) { // Error authenticating the device, check password. showToastMessage("Invalid password: " + e.getMessage()); } catch (XBeeException e) { // Error opening the connection with the device. showToastMessage("Could not open device: " + e.getMessage()); } } }).start(); } /** * Displays the given message. * * @param message The message to show. */ private void showToastMessage(final String message) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MyActivity.this, message, Toast.LENGTH_LONG).show(); } }); } }
This code tries to open the communication with an XBee device over Bluetooth Low Energy. If it succeeds, the application displays a toast message with the information of the XBee device. Otherwise, it displays a Could not open device message followed by the error that occurred.
- To build the Android application, select Build > Make Project from the main menu bar.
- To launch the application, select Run > Run 'app'.
- Verify the device can be found and the application displays its information in a toast message.