summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoão Paulo Rechi Vita <jprvita@gmail.com>2013-12-30 19:00:43 -0300
committerJoão Paulo Rechi Vita <jprvita@gmail.com>2014-01-15 15:03:58 -0300
commit3af83d3ad7e8dd644da88e8db445c401eb9d3722 (patch)
tree38bd956e7187d40c253f94ee6dc84f4a4cd1ed35
parentca2ff03aa45511285597e20d0c968e19004f92f9 (diff)
test: Create a batch program to test BLE scanning
-rw-r--r--test/Android.mk10
-rw-r--r--test/libble-scan.c94
2 files changed, 104 insertions, 0 deletions
diff --git a/test/Android.mk b/test/Android.mk
new file mode 100644
index 0000000..898775a
--- /dev/null
+++ b/test/Android.mk
@@ -0,0 +1,10 @@
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := libble-scan.c
+LOCAL_SHARED_LIBRARIES := libble
+LOCAL_MODULE_TAGS := eng
+LOCAL_MODULE := libble-scan
+
+include $(BUILD_EXECUTABLE)
diff --git a/test/libble-scan.c b/test/libble-scan.c
new file mode 100644
index 0000000..1642f3b
--- /dev/null
+++ b/test/libble-scan.c
@@ -0,0 +1,94 @@
+/*
+ * libble-scan -- Enables BLE and scans for devices for 30s
+ *
+ * Copyright (C) 2013 João Paulo Rechi Vita
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+
+#include <libble/ble.h>
+
+static uint8_t enabled;
+
+static void enable_cb(void) {
+ printf("BLE enabled.\n");
+ enabled = 1;
+}
+
+static void adapter_state_cb(uint8_t state) {
+ printf("Adapter state changed: %u\n", state);
+}
+
+static void scan_cb(const uint8_t *address, int rssi, const uint8_t *adv_data) {
+ printf("Device found: %X:%X:%X:%X:%X:%X, RSSI %d\n", address[0], address[1],
+ address[2], address[3], address[4], address[5], rssi);
+}
+
+static ble_cbs_t ble_cbs = {
+ enable_cb,
+ adapter_state_cb,
+ scan_cb,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+int main (int argc, char *argv[]) {
+ int status;
+
+ if (getuid() != 0) {
+ printf("Permission denied\n");
+ return 1;
+ }
+
+ enabled = 0;
+ printf("Initializing libble... ");
+ status = ble_enable(ble_cbs);
+ if (status != 0) {
+ printf("failed (%d).\n", status);
+ return -1;
+ }
+ printf("\n");
+ while (!enabled) sleep(1);
+
+ printf("Starting BLE scanning for 30s: %d\n", ble_start_scan());
+ sleep(30);
+ printf("Stopping BLE scanning: %d\n", ble_stop_scan());
+
+ printf("Disabling the adapter: %d\n", ble_disable());
+ sleep(2);
+
+ return 0;
+}