├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── BT_test
└── BT_test.ino
├── BluetoothKeyboard.apk
├── README.md
├── bin
├── AndroidManifest.xml
├── BluetoothKeyboard.apk
├── classes.dex
├── classes
│ └── com
│ │ └── damas
│ │ ├── blejoystickapp
│ │ ├── DeviceListActivity$1.class
│ │ ├── DeviceListActivity$2.class
│ │ ├── DeviceListActivity$3.class
│ │ ├── DeviceListActivity$4.class
│ │ ├── DeviceListActivity.class
│ │ ├── JoystickIME$1.class
│ │ ├── JoystickIME$AdbReceiver$1.class
│ │ ├── JoystickIME$AdbReceiver.class
│ │ ├── JoystickIME.class
│ │ ├── SettingsActivity$1.class
│ │ ├── SettingsActivity.class
│ │ └── bluetooth
│ │ │ ├── CommService$ServiceHandler.class
│ │ │ ├── CommService.class
│ │ │ └── CustomizedBluetoothDevice.class
│ │ └── bluetoothkeyboard
│ │ ├── BuildConfig.class
│ │ ├── R$attr.class
│ │ ├── R$dimen.class
│ │ ├── R$drawable.class
│ │ ├── R$id.class
│ │ ├── R$layout.class
│ │ ├── R$menu.class
│ │ ├── R$string.class
│ │ ├── R$style.class
│ │ ├── R$xml.class
│ │ └── R.class
├── dexedLibs
│ └── android-support-v4-12a753c2552b0bfb3afb57f13fac49a0.jar
├── jarlist.cache
├── res
│ └── crunch
│ │ ├── drawable-hdpi
│ │ ├── bluetooth.png
│ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ │ └── drawable
│ │ ├── bluetoothoff.png
│ │ └── bluetoothon.png
└── resources.ap_
├── gen
└── com
│ └── damas
│ └── bluetoothkeyboard
│ ├── BuildConfig.java
│ └── R.java
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ ├── bluetooth.png
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── drawable
│ ├── bluetoothoff.png
│ ├── bluetoothon.png
│ ├── dotted.xml
│ ├── gradient.xml
│ ├── gradient2.xml
│ ├── gradient_blue.xml
│ ├── gradient_red.xml
│ └── listview_divider.xml
├── layout
│ ├── activity_device_list.xml
│ ├── keyboardtimeout.xml
│ ├── macaddr1.xml
│ ├── macaddr2.xml
│ ├── settings.xml
│ └── view.xml
├── menu
│ └── main.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
├── values-w820dp
│ └── dimens.xml
├── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── xml
│ └── methods.xml
└── src
└── com
└── damas
└── blejoystickapp
├── DeviceListActivity.java
├── JoystickIME.java
├── SettingsActivity.java
└── bluetooth
├── CommService.java
└── CustomizedBluetoothDevice.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | BluetoothKeyboard
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
12 |
13 |
14 |
15 |
20 |
21 |
22 |
23 |
26 |
27 |
28 |
29 |
30 |
33 |
34 |
35 |
38 |
39 |
40 |
41 |
42 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/BT_test/BT_test.ino:
--------------------------------------------------------------------------------
1 |
2 | #include
3 | #include
4 |
5 | void setup()
6 | {
7 | // initialize serial communication at 9600 bits per second:
8 | Serial.begin(9600);
9 |
10 | }
11 |
12 |
13 | void loop()
14 | {
15 |
16 |
17 | Serial.print("1x6t;");
18 | delay(15000);
19 | Serial.print("1x8u;");
20 | delay(15000);
21 | Serial.print("1x8p;");
22 | delay(30000);
23 | Serial.print("1x8t;");
24 | delay(15000);
25 | Serial.print("4x8p;");
26 | delay(15000);
27 | Serial.print("8x8t;");
28 | delay(15000);
29 | Serial.print("1x8u;");
30 | delay(30000);
31 | Serial.print("9x8u;");
32 | delay(15000);
33 | Serial.print("9x8p;");
34 | delay(15000);
35 | Serial.print("1x8t;");
36 | delay(30000);
37 | Serial.print("8x8p;");
38 | delay(15000);
39 | Serial.print("4x10t;");
40 | delay(15000);
41 | Serial.print("4x11p;");
42 | delay(60000);
43 | }
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/BluetoothKeyboard.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/BluetoothKeyboard.apk
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Bluetooth-Keyboard-Android
2 | Android Custom IME with Arduino physical Keyboard.
3 | This project is demo for physical keyboard with Android as a input method (IME).
4 |
5 |
6 | USAGE:
7 |
8 | Burn Arduino sketch in BT_Test directory to arduino controller.
9 |
10 | Install apk to android phone.
11 |
12 | Go to settings->Language & Input, Turn ON 'Bluetooth Keyboard' in 'Keyboard & Input Methods'.
13 |
14 | Click 'Bluetooth Keyboard' and select your device, make Bluetooth connection.
15 |
16 |
--------------------------------------------------------------------------------
/bin/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
12 |
13 |
14 |
15 |
20 |
21 |
22 |
23 |
26 |
27 |
28 |
29 |
30 |
33 |
34 |
35 |
38 |
39 |
40 |
41 |
42 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/bin/BluetoothKeyboard.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/BluetoothKeyboard.apk
--------------------------------------------------------------------------------
/bin/classes.dex:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes.dex
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/DeviceListActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/DeviceListActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/DeviceListActivity$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/DeviceListActivity$2.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/DeviceListActivity$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/DeviceListActivity$3.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/DeviceListActivity$4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/DeviceListActivity$4.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/DeviceListActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/DeviceListActivity.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/JoystickIME$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/JoystickIME$1.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/JoystickIME$AdbReceiver$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/JoystickIME$AdbReceiver$1.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/JoystickIME$AdbReceiver.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/JoystickIME$AdbReceiver.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/JoystickIME.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/JoystickIME.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/SettingsActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/SettingsActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/SettingsActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/SettingsActivity.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/bluetooth/CommService$ServiceHandler.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/bluetooth/CommService$ServiceHandler.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/bluetooth/CommService.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/bluetooth/CommService.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/blejoystickapp/bluetooth/CustomizedBluetoothDevice.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/blejoystickapp/bluetooth/CustomizedBluetoothDevice.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/BuildConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/BuildConfig.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$attr.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$attr.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$dimen.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$dimen.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$drawable.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$drawable.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$id.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$id.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$layout.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$layout.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$menu.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$menu.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$string.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$string.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$style.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$style.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R$xml.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R$xml.class
--------------------------------------------------------------------------------
/bin/classes/com/damas/bluetoothkeyboard/R.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/classes/com/damas/bluetoothkeyboard/R.class
--------------------------------------------------------------------------------
/bin/dexedLibs/android-support-v4-12a753c2552b0bfb3afb57f13fac49a0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/dexedLibs/android-support-v4-12a753c2552b0bfb3afb57f13fac49a0.jar
--------------------------------------------------------------------------------
/bin/jarlist.cache:
--------------------------------------------------------------------------------
1 | # cache for current jar dependency. DO NOT EDIT.
2 | # format is
3 | # Encoding is UTF-8
4 |
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-hdpi/bluetooth.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable-hdpi/bluetooth.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable/bluetoothoff.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable/bluetoothoff.png
--------------------------------------------------------------------------------
/bin/res/crunch/drawable/bluetoothon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/res/crunch/drawable/bluetoothon.png
--------------------------------------------------------------------------------
/bin/resources.ap_:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/bin/resources.ap_
--------------------------------------------------------------------------------
/gen/com/damas/bluetoothkeyboard/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.damas.bluetoothkeyboard;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/com/damas/bluetoothkeyboard/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.damas.bluetoothkeyboard;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class dimen {
14 | /** Default screen margins, per the Android Design guidelines.
15 |
16 | Example customization of dimensions originally defined in res/values/dimens.xml
17 | (such as screen margins) for screens with more than 820dp of available width. This
18 | would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
19 |
20 | */
21 | public static final int activity_horizontal_margin=0x7f050000;
22 | public static final int activity_vertical_margin=0x7f050001;
23 | }
24 | public static final class drawable {
25 | public static final int bluetooth=0x7f020000;
26 | public static final int bluetoothoff=0x7f020001;
27 | public static final int bluetoothon=0x7f020002;
28 | public static final int dotted=0x7f020003;
29 | public static final int gradient=0x7f020004;
30 | public static final int gradient2=0x7f020005;
31 | public static final int gradient_blue=0x7f020006;
32 | public static final int gradient_red=0x7f020007;
33 | public static final int ic_launcher=0x7f020008;
34 | public static final int listview_divider=0x7f020009;
35 | }
36 | public static final class id {
37 | public static final int TableLayout01=0x7f090010;
38 | public static final int TableRow01=0x7f090011;
39 | public static final int action_keyboard=0x7f090019;
40 | public static final int action_settings=0x7f090018;
41 | public static final int btnDown=0x7f090015;
42 | public static final int btnLeft=0x7f090013;
43 | public static final int btnOk=0x7f090017;
44 | public static final int btnRight=0x7f090014;
45 | public static final int btnUp=0x7f090012;
46 | public static final int btn_startScan=0x7f090001;
47 | public static final int dialogButtonOK=0x7f090006;
48 | public static final int imageviewBluetooth=0x7f09000e;
49 | public static final int imgView=0x7f09000a;
50 | public static final int listview_devicelist=0x7f090000;
51 | public static final int npicker_hours=0x7f090003;
52 | public static final int npicker_minutes=0x7f090002;
53 | public static final int npicker_seconds=0x7f090004;
54 | public static final int rlBleData=0x7f09000c;
55 | public static final int rlJoystickKeys=0x7f09000f;
56 | public static final int rlJoystickOK=0x7f090016;
57 | public static final int tv_addr_ID=0x7f090008;
58 | public static final int tv_addr_Name=0x7f090007;
59 | public static final int tv_minutes=0x7f090005;
60 | public static final int txtBleData=0x7f09000d;
61 | public static final int txtConnectBluetooth=0x7f090009;
62 | public static final int txtKeyboardTimeout=0x7f09000b;
63 | }
64 | public static final class layout {
65 | public static final int activity_device_list=0x7f030000;
66 | public static final int keyboardtimeout=0x7f030001;
67 | public static final int macaddr1=0x7f030002;
68 | public static final int macaddr2=0x7f030003;
69 | public static final int settings=0x7f030004;
70 | public static final int view=0x7f030005;
71 | }
72 | public static final class menu {
73 | public static final int main=0x7f080000;
74 | }
75 | public static final class string {
76 | public static final int action_settings=0x7f060001;
77 | public static final int app_name=0x7f060000;
78 | public static final int bt_not_enabled_leaving=0x7f060004;
79 | public static final int bt_scan=0x7f060005;
80 | public static final int devicelist_scan=0x7f060003;
81 | public static final int hello_world=0x7f060002;
82 | public static final int messegelist_send=0x7f06000a;
83 | public static final int not_connected=0x7f060006;
84 | public static final int title_connected_to=0x7f060008;
85 | public static final int title_connecting=0x7f060007;
86 | public static final int title_not_connected=0x7f060009;
87 | }
88 | public static final class style {
89 | /**
90 | Base application theme, dependent on API level. This theme is replaced
91 | by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
92 |
93 |
94 | Theme customizations available in newer API levels can go in
95 | res/values-vXX/styles.xml, while customizations related to
96 | backward-compatibility can go here.
97 |
98 |
99 | Base application theme for API 11+. This theme completely replaces
100 | AppBaseTheme from res/values/styles.xml on API 11+ devices.
101 |
102 | API 11 theme customizations can go here.
103 |
104 | Base application theme for API 14+. This theme completely replaces
105 | AppBaseTheme from BOTH res/values/styles.xml and
106 | res/values-v11/styles.xml on API 14+ devices.
107 |
108 | API 14 theme customizations can go here.
109 | */
110 | public static final int AppBaseTheme=0x7f070000;
111 | /** Application theme.
112 | All customizations that are NOT specific to a particular API-level can go here.
113 | */
114 | public static final int AppTheme=0x7f070001;
115 | }
116 | public static final class xml {
117 | public static final int methods=0x7f040000;
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/bluetooth.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable-hdpi/bluetooth.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable/bluetoothoff.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable/bluetoothoff.png
--------------------------------------------------------------------------------
/res/drawable/bluetoothon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/asimiqbalmalik/Bluetooth-Keyboard-Android/c763a934e10288461777586cf27785254e2d10b1/res/drawable/bluetoothon.png
--------------------------------------------------------------------------------
/res/drawable/dotted.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/res/drawable/gradient.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/res/drawable/gradient2.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | -
5 |
6 |
7 |
8 |
9 |
10 | -
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/res/drawable/gradient_blue.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
--------------------------------------------------------------------------------
/res/drawable/gradient_red.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
--------------------------------------------------------------------------------
/res/drawable/listview_divider.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/res/layout/activity_device_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
17 |
21 |
22 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/res/layout/keyboardtimeout.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
23 |
24 |
25 |
26 |
32 |
33 |
44 |
45 |
55 |
56 |
66 |
67 |
75 |
76 |
--------------------------------------------------------------------------------
/res/layout/macaddr1.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
20 |
21 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/res/layout/macaddr2.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
11 |
12 |
20 |
21 |
24 |
25 |
26 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/res/layout/settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
21 |
22 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/res/layout/view.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
12 |
13 |
21 |
22 |
28 |
29 |
30 |
36 |
37 |
41 |
42 |
47 |
48 |
55 |
56 |
67 |
68 |
75 |
76 |
77 |
81 |
82 |
93 |
94 |
101 |
102 |
113 |
114 |
115 |
119 |
120 |
127 |
128 |
139 |
140 |
147 |
148 |
149 |
150 |
151 |
157 |
158 |
161 |
162 |
166 |
167 |
175 |
176 |
184 |
185 |
186 |
190 |
191 |
199 |
200 |
209 |
210 |
211 |
215 |
216 |
224 |
225 |
233 |
234 |
235 |
236 |
237 |
--------------------------------------------------------------------------------
/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
17 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 | 64dp
9 |
10 |
11 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Bluetooth Keyboard
5 | Settings
6 | Hello world!
7 | Scan Device
8 | Bluetooth was not enabled. Leaving …
9 | SCAN
10 | You are not connected to a device
11 | Status: connecting
12 | Status: connected
13 | Status: not connected
14 | Send
15 |
16 |
17 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/res/xml/methods.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
--------------------------------------------------------------------------------
/src/com/damas/blejoystickapp/DeviceListActivity.java:
--------------------------------------------------------------------------------
1 | package com.damas.blejoystickapp;
2 |
3 | import java.lang.reflect.Method;
4 | import java.util.ArrayList;
5 | import java.util.Set;
6 |
7 | import com.damas.blejoystickapp.bluetooth.CommService;
8 | import com.damas.blejoystickapp.bluetooth.CustomizedBluetoothDevice;
9 | import com.damas.bluetoothkeyboard.R;
10 |
11 | import android.app.Activity;
12 | import android.bluetooth.BluetoothAdapter;
13 | import android.bluetooth.BluetoothDevice;
14 | import android.content.BroadcastReceiver;
15 | import android.content.Context;
16 | import android.content.Intent;
17 | import android.content.IntentFilter;
18 | import android.content.SharedPreferences;
19 | import android.os.Bundle;
20 | import android.util.Log;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.widget.AdapterView;
25 | import android.widget.BaseAdapter;
26 | import android.widget.Button;
27 | import android.widget.ListView;
28 | import android.widget.TextView;
29 | import android.widget.Toast;
30 | import android.widget.AdapterView.OnItemClickListener;
31 |
32 | public class DeviceListActivity extends Activity {
33 |
34 | private static final String TAG = "DeviceListActivity";
35 | private static final boolean D = true;
36 |
37 | private static final int REQUEST_ENABLE_BT = 2;
38 | protected static final String PREFS_NAME = "Antonio081014 Bluetooth Android";
39 | protected static final String PREFS_DEVICE_ADDR = "Antonio081014 Bluetooth Address";
40 |
41 | private ArrayList mDeviceList;
42 |
43 | private BluetoothAdapter mBluetoothAdapter;
44 | private ListView mListViewDeviceList;
45 | private Button mButtonStartScan;
46 | private int currentPosition;
47 |
48 | private boolean isDeviceinRange=false;
49 |
50 | private SharedPreferences settings;
51 |
52 | BaseAdapter mBaseAdapter = new BaseAdapter() {
53 |
54 | @Override
55 | public View getView(int position, View convertView, ViewGroup parent) {
56 | LayoutInflater mInflater = (LayoutInflater) getApplicationContext()
57 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
58 |
59 | CustomizedBluetoothDevice device = mDeviceList.get(position);
60 |
61 | View rowView = mInflater.inflate(R.layout.macaddr1, parent, false);
62 | if (device.isStatusPaired() == false)
63 | rowView = mInflater.inflate(R.layout.macaddr2, parent, false);
64 |
65 | TextView name = (TextView) rowView.findViewById(R.id.tv_addr_Name);
66 | TextView addr = (TextView) rowView.findViewById(R.id.tv_addr_ID);
67 | String status = device.isStatusPaired() ? "Paired" : "Not Paired";
68 |
69 | name.setText(status + ": " + device.getName() + ".");
70 | addr.setText(device.getAddress());
71 | return rowView;
72 | }
73 |
74 | @Override
75 | public long getItemId(int position) {
76 | return 0;
77 | }
78 |
79 | @Override
80 | public Object getItem(int position) {
81 | return mDeviceList.get(position);
82 | }
83 |
84 | @Override
85 | public int getCount() {
86 | if (mDeviceList != null)
87 | return mDeviceList.size();
88 | return 0;
89 | }
90 | };
91 |
92 | private void updateUI() {
93 | mBaseAdapter.notifyDataSetChanged();
94 | }
95 |
96 | private void getPairedDevice() {
97 | Set pairedDevices = mBluetoothAdapter
98 | .getBondedDevices();
99 | if (pairedDevices.size() > 0) {
100 | for (BluetoothDevice device : pairedDevices) {
101 | CustomizedBluetoothDevice customizedDevice = new CustomizedBluetoothDevice(
102 | device);
103 | mDeviceList.add(customizedDevice);
104 | }
105 | }
106 | }
107 |
108 | private void initialization() {
109 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
110 | if (mBluetoothAdapter == null) {
111 | finish();
112 | return;
113 | }
114 |
115 | mListViewDeviceList = (ListView) findViewById(R.id.listview_devicelist);
116 | mListViewDeviceList.setAdapter(mBaseAdapter);
117 | mListViewDeviceList.setOnItemClickListener(mDeviceClickListener);
118 |
119 | mButtonStartScan = (Button) findViewById(R.id.btn_startScan);
120 | mButtonStartScan.setOnClickListener(new View.OnClickListener() {
121 | @Override
122 | public void onClick(View v) {
123 | setup();
124 | doDiscovery();
125 | }
126 | });
127 |
128 | settings = getSharedPreferences(DeviceListActivity.PREFS_NAME, 0);
129 |
130 | // Register the BroadcastReceiver
131 | IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
132 | registerReceiver(mReceiver, filter);
133 |
134 | // Register for broadcasts when discovery has finished
135 | filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
136 | this.registerReceiver(mReceiver, filter);
137 |
138 | filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
139 | this.registerReceiver(mReceiver, filter);
140 |
141 | filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
142 | this.registerReceiver(mReceiver, filter);
143 |
144 | filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
145 | this.registerReceiver(mReceiver,filter);
146 |
147 | }
148 |
149 | private void startConnect(CustomizedBluetoothDevice device) {
150 | SharedPreferences.Editor editor = settings.edit();
151 | editor.putString(DeviceListActivity.PREFS_DEVICE_ADDR,
152 | device.getAddress());
153 | editor.commit();
154 |
155 | }
156 |
157 | // The on-click listener for all devices in the ListViews
158 | // It will auto-connect with the device.
159 | private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
160 | public void onItemClick(AdapterView> av, View v, int position,
161 | long arg3) {
162 | // Cancel discovery because it's costly and we're about to connect
163 | Intent resultIntent = new Intent();
164 | // TODO Add extras or a data URI to this intent as appropriate.
165 |
166 | mBluetoothAdapter.cancelDiscovery();
167 | BluetoothDevice device = mBluetoothAdapter
168 | .getRemoteDevice(mDeviceList.get(position).getAddress());
169 | resultIntent.putExtra("MacAddress", device.getAddress());
170 | resultIntent.putExtra("DeviceName", device.getName());
171 | // check if device is still active or not
172 |
173 | currentPosition = position;
174 | if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
175 | // unpairDevice(device);
176 | //setResult(Activity.RESULT_OK, resultIntent);
177 | //finish();
178 | connectBT(device.getAddress(), device.getName());
179 | } else if (device.getBondState() == BluetoothDevice.BOND_NONE) {
180 | if (D)
181 | Log.d(TAG, "device not bond");
182 | pairDevice(device);
183 | if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
184 | //setResult(Activity.RESULT_OK, resultIntent);
185 | //finish();
186 | connectBT(device.getAddress(), device.getName());
187 | } else {
188 | Toast.makeText(getApplicationContext(),
189 | "Please pair your device!", Toast.LENGTH_LONG)
190 | .show();
191 | }
192 |
193 | }
194 | }
195 | };
196 |
197 | public void connectBT(String Mac, String deviceNameStr) {
198 | try {
199 | Intent intent = new Intent(getApplicationContext(),
200 | CommService.class);
201 | intent.putExtra("Mac", Mac);
202 | intent.putExtra("deviceNameStr", deviceNameStr);
203 | //Toast.makeText(this, "service started! ", Toast.LENGTH_SHORT).show();
204 | this.startService(intent);
205 | } catch (Exception e) {
206 | e.printStackTrace();
207 | }
208 |
209 | }
210 |
211 | /**
212 | * Start device discover with the BluetoothAdapter
213 | */
214 | private void doDiscovery() {
215 | // If we're already discovering, stop it
216 | if (mBluetoothAdapter.isDiscovering()) {
217 | mBluetoothAdapter.cancelDiscovery();
218 | }
219 |
220 | // Request discover from BluetoothAdapter
221 | mBluetoothAdapter.startDiscovery();
222 | }
223 |
224 | private void pairDevice(BluetoothDevice device) {
225 | try {
226 | if (D)
227 | Log.d(TAG, "Start Pairing...");
228 |
229 | Method m = device.getClass()
230 | .getMethod("createBond", (Class[]) null);
231 | m.invoke(device, (Object[]) null);
232 | } catch (Exception e) {
233 | Log.e(TAG, e.getMessage());
234 | }
235 | }
236 |
237 | private void unpairDevice(BluetoothDevice device) {
238 | try {
239 | Method m = device.getClass()
240 | .getMethod("removeBond", (Class[]) null);
241 | m.invoke(device, (Object[]) null);
242 | } catch (Exception e) {
243 | Log.e(TAG, e.getMessage());
244 | }
245 | }
246 |
247 | // The BroadcastReceiver that listens for discovered devices and
248 | // changes the title when discovery is finished
249 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
250 | @Override
251 | public void onReceive(Context context, Intent intent) {
252 | String action = intent.getAction();
253 |
254 | // When discovery finds a device
255 | if (BluetoothDevice.ACTION_FOUND.equals(action)) {
256 | // Get the BluetoothDevice object from the Intent
257 | BluetoothDevice device = intent
258 | .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
259 | // If it's already paired, skip it, because it's been listed
260 | // already
261 | CustomizedBluetoothDevice mDevice = new CustomizedBluetoothDevice(
262 | device);
263 | if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
264 |
265 | if (mDeviceList.contains(mDevice) == false) {
266 | mDeviceList.add(mDevice);
267 | updateUI();
268 | }
269 | }
270 | }else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
271 | // Device is now connected
272 | isDeviceinRange=true;
273 | Toast.makeText(getApplicationContext(), "Service Started Successfully", Toast.LENGTH_LONG).show();
274 | finish();
275 | }else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
276 | // Device is now connected
277 | Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();
278 | //finish();
279 | }
280 |
281 | // When the device bond state changed.
282 | else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
283 | int prevBondState = intent.getIntExtra(
284 | BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
285 | int bondState = intent.getIntExtra(
286 | BluetoothDevice.EXTRA_BOND_STATE, -1);
287 |
288 |
289 | if (prevBondState == BluetoothDevice.BOND_BONDED
290 | && bondState == BluetoothDevice.BOND_NONE) {
291 | BluetoothDevice device = intent
292 | .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
293 | if (currentPosition != -1
294 | && currentPosition < mDeviceList.size()) {
295 | CustomizedBluetoothDevice mDevice = mDeviceList
296 | .get(currentPosition);
297 | if (device.getAddress().compareTo(mDevice.getAddress()) == 0) {
298 | mDevice.setStatusPaired(false);
299 | updateUI();
300 | pairDevice(device);
301 | }
302 | }
303 | } else if (prevBondState == BluetoothDevice.BOND_BONDING
304 | && bondState == BluetoothDevice.BOND_BONDED) {
305 | BluetoothDevice device = intent
306 | .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
307 | if (currentPosition != -1
308 | && currentPosition < mDeviceList.size()) {
309 | CustomizedBluetoothDevice mDevice = mDeviceList
310 | .get(currentPosition);
311 | if (device.getAddress().compareTo(mDevice.getAddress()) == 0) {
312 | mDevice.setStatusPaired(true);
313 | updateUI();
314 | startConnect(mDevice);
315 | }
316 | }
317 | }
318 | }
319 | }
320 | };
321 |
322 | @Override
323 | protected void onCreate(Bundle savedInstanceState) {
324 | super.onCreate(savedInstanceState);
325 | setContentView(R.layout.activity_device_list);
326 | initialization();
327 | }
328 |
329 | @Override
330 | protected void onStart() {
331 | super.onStart();
332 |
333 | if (!mBluetoothAdapter.isEnabled()) {
334 | Intent enableIntent = new Intent(
335 | BluetoothAdapter.ACTION_REQUEST_ENABLE);
336 | startActivityForResult(enableIntent,
337 | DeviceListActivity.REQUEST_ENABLE_BT);
338 | // Otherwise, setup the chat session
339 | } else {
340 | setup();
341 | }
342 | }
343 |
344 | private void setup() {
345 | mDeviceList = new ArrayList();
346 | currentPosition = -1;
347 | getPairedDevice();
348 | updateUI();
349 | }
350 |
351 | /*
352 | * @Override public boolean onCreateOptionsMenu(Menu menu) {
353 | * getMenuInflater().inflate(R.menu., menu); return true; }
354 | */
355 |
356 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
357 | switch (requestCode) {
358 | case DeviceListActivity.REQUEST_ENABLE_BT:
359 | // When the request to enable Bluetooth returns
360 | if (resultCode == Activity.RESULT_OK) {
361 | // Bluetooth is now enabled, so set up a chat session
362 | setup();
363 | } else {
364 | // User did not enable Bluetooth or an error occured
365 | if (D)
366 | Log.d(TAG, "BT not enabled");
367 | Toast.makeText(this, R.string.bt_not_enabled_leaving,
368 | Toast.LENGTH_SHORT).show();
369 | finish();
370 | }
371 | }
372 | super.onActivityResult(requestCode, resultCode, data);
373 | }
374 |
375 | @Override
376 | protected void onDestroy() {
377 | super.onDestroy();
378 |
379 | // Make sure we're not doing discovery anymore
380 | if (mBluetoothAdapter != null) {
381 | mBluetoothAdapter.cancelDiscovery();
382 | }
383 |
384 | // Unregister broadcast listeners
385 | this.unregisterReceiver(mReceiver);
386 | }
387 | }
388 |
--------------------------------------------------------------------------------
/src/com/damas/blejoystickapp/JoystickIME.java:
--------------------------------------------------------------------------------
1 | package com.damas.blejoystickapp;
2 |
3 | import android.bluetooth.BluetoothAdapter;
4 | import android.bluetooth.BluetoothDevice;
5 | import android.content.BroadcastReceiver;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.IntentFilter;
9 | import android.content.SharedPreferences;
10 | import android.graphics.drawable.Drawable;
11 | import android.inputmethodservice.InputMethodService;
12 | import android.os.CountDownTimer;
13 | import android.os.Handler;
14 | import android.support.v4.content.LocalBroadcastManager;
15 | import android.util.Log;
16 | import android.view.View;
17 | import android.view.inputmethod.InputConnection;
18 | import android.widget.Button;
19 | import android.widget.EditText;
20 | import android.widget.ImageView;
21 |
22 | import com.damas.bluetoothkeyboard.R;
23 |
24 | public class JoystickIME extends InputMethodService {
25 |
26 | private boolean isBluetoothConnected = false;
27 |
28 | private Button mbtnLeft;
29 | private Button mbtnRight;
30 | private Button mbtnUp;
31 | private Button mbtnDown;
32 | private EditText mtxtBleData;
33 | private String mFirstCharacter;
34 | private String mSecondCharacter;
35 | private long mKeyboardTimeout;
36 | private String mKeyboardTime = "";
37 | private boolean isKeyboardCountend;
38 | private BroadcastReceiver mReceiver = null;
39 | private ImageView mImgViewBluetooth;
40 |
41 | Handler handler = new Handler();
42 | Runnable runnable = new Runnable() {
43 | public void run() {
44 | // afficher();
45 | changeImageBluetooth();
46 | }
47 | };
48 |
49 | @Override
50 | public View onCreateInputView() {
51 | Log.d("JoystickIME", "onCreateInputView CALLED!");
52 | View mInputView = (View) getLayoutInflater().inflate(R.layout.view,
53 | null);
54 | mtxtBleData = (EditText) mInputView.findViewById(R.id.txtBleData);
55 | mbtnLeft = (Button) mInputView.findViewById(R.id.btnLeft);
56 | mbtnRight = (Button) mInputView.findViewById(R.id.btnRight);
57 | mbtnUp = (Button) mInputView.findViewById(R.id.btnUp);
58 | mbtnDown = (Button) mInputView.findViewById(R.id.btnDown);
59 | mImgViewBluetooth = (ImageView) mInputView
60 | .findViewById(R.id.imageviewBluetooth);
61 |
62 | changeImageBluetooth();
63 | // handler.postDelayed(runnable, 1000);
64 |
65 | if (mReceiver == null) {
66 | IntentFilter filter = new IntentFilter("speedExceeded");
67 | IntentFilter filter11 = new IntentFilter("Ext");
68 |
69 | mReceiver = new AdbReceiver();
70 | registerReceiver(mReceiver, filter11);
71 | LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
72 | filter);
73 |
74 | IntentFilter filter1 = new IntentFilter(
75 | BluetoothDevice.ACTION_ACL_CONNECTED);
76 | IntentFilter filter2 = new IntentFilter(
77 | BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
78 | IntentFilter filter3 = new IntentFilter(
79 | BluetoothDevice.ACTION_ACL_DISCONNECTED);
80 | this.registerReceiver(mReceiver, filter1);
81 | this.registerReceiver(mReceiver, filter2);
82 | this.registerReceiver(mReceiver, filter3);
83 | }
84 |
85 | return mInputView;
86 | }
87 |
88 | public void changeImageBluetooth() {
89 | Log.d("changeImageBluetooth", "" + isBluetoothConnected);
90 | if (isBluetoothConnected) {
91 | mImgViewBluetooth.setBackgroundDrawable(this.getResources()
92 | .getDrawable(R.drawable.bluetoothoff)); // on
93 | } else {
94 | mImgViewBluetooth.setBackgroundDrawable(this.getResources()
95 | .getDrawable(R.drawable.bluetoothon)); // off
96 |
97 | }
98 | }
99 |
100 | @Override
101 | public void onCreate() {
102 | // TODO Auto-generated method stub
103 | super.onCreate();
104 | Log.d("JoystickIME", "onCreate CALLED!");
105 | }
106 |
107 | @Override
108 | public void onDestroy() {
109 | if (mReceiver != null)
110 | unregisterReceiver(mReceiver);
111 | super.onDestroy();
112 | Log.d("JoystickIME", "onDestroy CALLED!");
113 | }
114 |
115 | @Override
116 | public void onFinishInput() {
117 | // TODO Auto-generated method stub
118 | super.onFinishInput();
119 | Log.d("JoystickIME", "onFinishInput CALLED!");
120 | }
121 |
122 | @Override
123 | public void onInitializeInterface() {
124 | // TODO Auto-generated method stub
125 | super.onInitializeInterface();
126 | Log.d("JoystickIME", "onInitializeInterface CALLED!");
127 |
128 | }
129 |
130 | public void takeAction(String message) {
131 | if (message.length() == 2) {
132 | mFirstCharacter = message.substring(0, 1);
133 | mSecondCharacter = message.substring(1, 2);
134 | // mtxtBleData.setText(intent.getStringExtra("Message"));
135 | InputConnection ic = getCurrentInputConnection();
136 |
137 | if (mSecondCharacter.equalsIgnoreCase("T")) {
138 | // TOUCHED BLUE
139 | if (mFirstCharacter.equalsIgnoreCase("L")) {
140 | Drawable d = getResources().getDrawable(
141 | R.drawable.gradient_blue);
142 | Drawable r = getResources()
143 | .getDrawable(R.drawable.gradient);
144 | mbtnLeft.setBackgroundDrawable(d);
145 | mbtnRight.setBackgroundDrawable(r);
146 | mbtnUp.setBackgroundDrawable(r);
147 | mbtnDown.setBackgroundDrawable(r);
148 | mtxtBleData.setText("Left Touched");
149 | // send text to edit text
150 |
151 | } else if (mFirstCharacter.equalsIgnoreCase("R")) {
152 | Drawable d = getResources().getDrawable(
153 | R.drawable.gradient_blue);
154 | Drawable r = getResources()
155 | .getDrawable(R.drawable.gradient);
156 | mbtnLeft.setBackgroundDrawable(r);
157 | mbtnRight.setBackgroundDrawable(d);
158 | mbtnUp.setBackgroundDrawable(r);
159 | mbtnDown.setBackgroundDrawable(r);
160 | mtxtBleData.setText("Right Touched");
161 | } else if (mFirstCharacter.equalsIgnoreCase("U")) {
162 | Drawable d = getResources().getDrawable(
163 | R.drawable.gradient_blue);
164 | Drawable r = getResources()
165 | .getDrawable(R.drawable.gradient);
166 | mbtnLeft.setBackgroundDrawable(r);
167 | mbtnRight.setBackgroundDrawable(r);
168 | mbtnUp.setBackgroundDrawable(d);
169 | mbtnDown.setBackgroundDrawable(r);
170 | mtxtBleData.setText("Up Touched");
171 | } else if (mFirstCharacter.equalsIgnoreCase("D")) {
172 | Drawable d = getResources().getDrawable(
173 | R.drawable.gradient_blue);
174 | Drawable r = getResources()
175 | .getDrawable(R.drawable.gradient);
176 | mbtnLeft.setBackgroundDrawable(r);
177 | mbtnRight.setBackgroundDrawable(r);
178 | mbtnUp.setBackgroundDrawable(r);
179 | mbtnDown.setBackgroundDrawable(d);
180 | // mtxtBleData.setText("Down Touched");
181 | }
182 |
183 | } else if (mSecondCharacter.equalsIgnoreCase("U")) {
184 | // UnPressed ORIGINAL
185 | if (mFirstCharacter.equalsIgnoreCase("L")) {
186 | Drawable d = getResources()
187 | .getDrawable(R.drawable.gradient);
188 | mbtnLeft.setBackgroundDrawable(d);
189 |
190 | // mtxtBleData.setText("Left UnPressed");
191 | } else if (mFirstCharacter.equalsIgnoreCase("R")) {
192 | Drawable d = getResources()
193 | .getDrawable(R.drawable.gradient);
194 | mbtnRight.setBackgroundDrawable(d);
195 | // mtxtBleData.setText("Right UnPressed");
196 | } else if (mFirstCharacter.equalsIgnoreCase("U")) {
197 | Drawable d = getResources()
198 | .getDrawable(R.drawable.gradient);
199 | mbtnUp.setBackgroundDrawable(d);
200 | // mtxtBleData.setText("Up UnPressed");
201 | } else if (mFirstCharacter.equalsIgnoreCase("D")) {
202 | Drawable d = getResources()
203 | .getDrawable(R.drawable.gradient);
204 | mbtnDown.setBackgroundDrawable(d);
205 | // mtxtBleData.setText("Down UnPressed");
206 | }
207 |
208 | } else if (mSecondCharacter.equalsIgnoreCase("P")) {
209 | // PRESSED RED
210 | if (mFirstCharacter.equalsIgnoreCase("L")) {
211 | Drawable d = getResources().getDrawable(
212 | R.drawable.gradient_red);
213 | Drawable r = getResources()
214 | .getDrawable(R.drawable.gradient);
215 | mbtnLeft.setBackgroundDrawable(d);
216 | mbtnRight.setBackgroundDrawable(r);
217 | mbtnUp.setBackgroundDrawable(r);
218 | mbtnDown.setBackgroundDrawable(r);
219 | if (mtxtBleData != null)
220 | mtxtBleData.setText("Left Pressed");
221 | // SEND VALUE TO THAT EDIT TEXT
222 | if (ic != null)
223 | ic.commitText("L", 1);
224 | } else if (mFirstCharacter.equalsIgnoreCase("R")) {
225 | Drawable d = getResources().getDrawable(
226 | R.drawable.gradient_red);
227 | Drawable r = getResources()
228 | .getDrawable(R.drawable.gradient);
229 | mbtnLeft.setBackgroundDrawable(r);
230 | mbtnRight.setBackgroundDrawable(d);
231 | mbtnUp.setBackgroundDrawable(r);
232 | mbtnDown.setBackgroundDrawable(r);
233 | if (mtxtBleData != null)
234 | mtxtBleData.setText("Right Pressed");
235 | // SEND VALUE TO THAT EDIT TEXT
236 | if (ic != null)
237 | ic.commitText("R", 1);
238 | } else if (mFirstCharacter.equalsIgnoreCase("U")) {
239 | Drawable d = getResources().getDrawable(
240 | R.drawable.gradient_red);
241 | Drawable r = getResources()
242 | .getDrawable(R.drawable.gradient);
243 | mbtnLeft.setBackgroundDrawable(r);
244 | mbtnRight.setBackgroundDrawable(r);
245 | mbtnUp.setBackgroundDrawable(d);
246 | mbtnDown.setBackgroundDrawable(r);
247 | if (mtxtBleData != null)
248 | mtxtBleData.setText("Up Pressed");
249 | // SEND VALUE TO THAT EDIT TEXT
250 | if (ic != null)
251 | ic.commitText("U", 1);
252 | } else if (mFirstCharacter.equalsIgnoreCase("D")) {
253 | Drawable d = getResources().getDrawable(
254 | R.drawable.gradient_red);
255 | Drawable r = getResources()
256 | .getDrawable(R.drawable.gradient);
257 | mbtnLeft.setBackgroundDrawable(r);
258 | mbtnRight.setBackgroundDrawable(r);
259 | mbtnUp.setBackgroundDrawable(r);
260 | mbtnDown.setBackgroundDrawable(d);
261 | if (mtxtBleData != null)
262 | mtxtBleData.setText("Down Pressed");
263 | // SEND VALUE TO THAT EDIT TEXT
264 | if (ic != null)
265 | ic.commitText("D", 1);
266 | }
267 | }
268 |
269 | }
270 |
271 | }
272 |
273 | class AdbReceiver extends BroadcastReceiver {
274 |
275 | @Override
276 | public void onReceive(Context context, Intent intent) {
277 | String action = intent.getAction();
278 | BluetoothDevice device = intent
279 | .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
280 |
281 | if (BluetoothDevice.ACTION_FOUND.equals(action)) {
282 | // Device found
283 |
284 | } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
285 | // Device is now connected
286 | isBluetoothConnected = true;
287 |
288 | changeImageBluetooth();
289 |
290 | } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
291 | .equals(action)) {
292 | // Done searching
293 | } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
294 | // Device has disconnected
295 | // mImgViewBluetooth.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bluetoothon));
296 | isBluetoothConnected = false;
297 | changeImageBluetooth();
298 | } else if (action.equalsIgnoreCase("isBluetoothConnected")) {
299 | // Done searching
300 | isBluetoothConnected = false;
301 | changeImageBluetooth();
302 | } else if (action.equalsIgnoreCase("speedExceeded")) {
303 | /*
304 | * if(isKeyboardActive){ isKeyboardActive=false; }else
305 | * if(!isKeyboardActive)isKeyboardActive=true; showKeyboard();
306 | */
307 | // values equal to textview
308 | // showKeyboard();
309 | SharedPreferences settings = getApplication()
310 | .getSharedPreferences("BTJOYSTICK1", 0);
311 | mKeyboardTime = settings.getString("KeyboardTimeout",
312 | "30 Seconds");
313 | if (mKeyboardTime.equalsIgnoreCase("Never")) {
314 |
315 | } else if (mKeyboardTime.equalsIgnoreCase("30 Seconds")) {
316 | mKeyboardTimeout = 30000;
317 |
318 | } else if (mKeyboardTime.equalsIgnoreCase("1 Minutes")) {
319 | mKeyboardTimeout = 60000;
320 |
321 | } else if (mKeyboardTime.equalsIgnoreCase("5 Minutes")) {
322 | mKeyboardTimeout = 300000;
323 |
324 | } else if (mKeyboardTime.equalsIgnoreCase("15 Minutes")) {
325 | mKeyboardTimeout = 900000;
326 |
327 | } else if (mKeyboardTime.equalsIgnoreCase("30 Minutes")) {
328 | mKeyboardTimeout = 1800000;
329 |
330 | } else if (mKeyboardTime.equalsIgnoreCase("1 Hour")) {
331 | mKeyboardTimeout = 3600000;
332 |
333 |
334 | }
335 | isKeyboardCountend = true;
336 |
337 | if (!intent.getStringExtra("Message").equalsIgnoreCase("")) {
338 | isBluetoothConnected = true;
339 | changeImageBluetooth();
340 | Log.d("JoystickIME", "JoystickIme message"+intent.getStringExtra("Message"));
341 | takeAction(intent.getStringExtra("Message"));
342 | isKeyboardCountend = false;
343 | } else if (intent.getStringExtra("Message")
344 |
345 | .equalsIgnoreCase("")) {
346 | new CountDownTimer(mKeyboardTimeout, 1000) {
347 |
348 | public void onTick(long millisUntilFinished) {
349 | }
350 |
351 | public void onFinish() {
352 | // mTextField.setText("done!");
353 | if (isKeyboardCountend) {
354 |
355 | }
356 | }
357 | }.start();
358 |
359 | }
360 | }
361 |
362 | }
363 | }
364 | }
--------------------------------------------------------------------------------
/src/com/damas/blejoystickapp/SettingsActivity.java:
--------------------------------------------------------------------------------
1 | package com.damas.blejoystickapp;
2 |
3 | import com.damas.blejoystickapp.bluetooth.CommService;
4 | import com.damas.bluetoothkeyboard.R;
5 |
6 | import android.app.Activity;
7 | import android.app.Dialog;
8 | import android.content.Intent;
9 | import android.content.SharedPreferences;
10 | import android.os.Bundle;
11 | import android.util.Log;
12 | import android.view.View;
13 | import android.view.View.OnClickListener;
14 | import android.widget.Button;
15 | import android.widget.NumberPicker;
16 | import android.widget.TextView;
17 | import android.widget.Toast;
18 |
19 | public class SettingsActivity extends Activity implements OnClickListener {
20 |
21 | private static final int MY_CHILD_ACTIVITY = 1;
22 | private static final String TAG = "SettingsActivity";
23 |
24 | private TextView txtConnectBluetooth;
25 | private TextView txtKeyboardTimeout;
26 | private String mBleDeviceMac;
27 | private String mBleDeviceName;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | // TODO Auto-generated method stub
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.settings);
34 | txtConnectBluetooth = (TextView) findViewById(R.id.txtConnectBluetooth);
35 | txtKeyboardTimeout = (TextView) findViewById(R.id.txtKeyboardTimeout);
36 | txtConnectBluetooth.setOnClickListener(this);
37 | txtKeyboardTimeout.setOnClickListener(this);
38 |
39 | }
40 |
41 | public void connectBT(String Mac, String deviceNameStr) {
42 | try {
43 | Intent intent = new Intent(getApplicationContext(),
44 | CommService.class);
45 | intent.putExtra("Mac", Mac);
46 | intent.putExtra("deviceNameStr", deviceNameStr);
47 | Toast.makeText(this, "service started! ", Toast.LENGTH_SHORT)
48 | .show();
49 | this.startService(intent);
50 | } catch (Exception e) {
51 | e.printStackTrace();
52 | }
53 |
54 | }
55 |
56 | @Override
57 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
58 | // TODO Auto-generated method stub
59 | super.onActivityResult(requestCode, resultCode, data);
60 | switch (requestCode) {
61 | case (MY_CHILD_ACTIVITY): {
62 | if (resultCode == Activity.RESULT_OK) {
63 | // TODO Extract the data returned from the child Activity.
64 | mBleDeviceName = data.getStringExtra("MacAddress");
65 | mBleDeviceMac = data.getStringExtra("DeviceName");
66 | Log.d("MainActivity", "deviceMac + deviceName " + mBleDeviceMac
67 | + " + " + mBleDeviceName);
68 | connectBT(mBleDeviceName, mBleDeviceMac);
69 | }
70 | break;
71 | }
72 | }
73 | }
74 |
75 | public void showCustomDialog() {
76 | // custom dialog
77 | final Dialog dialog = new Dialog(this);
78 | dialog.setContentView(R.layout.keyboardtimeout);
79 | dialog.setTitle("Keyboard Timeout");
80 |
81 | // set the custom dialog components - text, image and button
82 | final NumberPicker mhoursNpick = (NumberPicker) dialog
83 | .findViewById(R.id.npicker_hours);
84 | final NumberPicker mminutesNpick = (NumberPicker) dialog
85 | .findViewById(R.id.npicker_minutes);
86 | final NumberPicker msecondsNpick = (NumberPicker) dialog
87 | .findViewById(R.id.npicker_seconds);
88 | mhoursNpick.setMinValue(0);
89 | mhoursNpick.setMaxValue(10);
90 | mhoursNpick.setWrapSelectorWheel(false);
91 | mminutesNpick.setMinValue(0);
92 | mminutesNpick.setMaxValue(10);
93 | mminutesNpick.setWrapSelectorWheel(false);
94 | msecondsNpick.setMinValue(0);
95 | msecondsNpick.setMaxValue(10);
96 | msecondsNpick.setWrapSelectorWheel(false);
97 | Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
98 | dialogButton.setOnClickListener(new OnClickListener() {
99 |
100 | @Override
101 | public void onClick(View arg0) {
102 | // TODO Auto-generated method stub
103 | SharedPreferences Settings = getApplication()
104 | .getSharedPreferences("BTJOYSTICK1", 0);
105 | SharedPreferences.Editor editor = Settings.edit();
106 | editor.putString("Hours", mhoursNpick.toString());
107 | editor.putString("Minutes", mminutesNpick.toString());
108 |
109 | editor.putString("Seconds", msecondsNpick.toString());
110 |
111 | editor.commit();
112 | dialog.dismiss();
113 |
114 | }
115 | });
116 |
117 | dialog.show();
118 | }
119 |
120 | @Override
121 | public void onClick(View arg0) {
122 | // TODO Auto-generated method stub
123 |
124 | switch (arg0.getId()) {
125 | case R.id.txtConnectBluetooth:
126 | // connectbluetooth
127 | Intent intent = new Intent(this, DeviceListActivity.class);
128 | startActivityForResult(intent, MY_CHILD_ACTIVITY);
129 |
130 | break;
131 | case R.id.txtKeyboardTimeout:
132 | // connectbluetooth
133 | showCustomDialog();
134 | break;
135 | default:
136 | break;
137 | }
138 | }
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/src/com/damas/blejoystickapp/bluetooth/CommService.java:
--------------------------------------------------------------------------------
1 | package com.damas.blejoystickapp.bluetooth;
2 |
3 | import java.io.BufferedInputStream;
4 | import java.io.File;
5 | import java.io.FileInputStream;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.io.OutputStream;
9 | import java.util.Set;
10 | import java.util.UUID;
11 |
12 |
13 |
14 |
15 | import android.app.Service;
16 | import android.bluetooth.BluetoothAdapter;
17 | import android.bluetooth.BluetoothDevice;
18 | import android.bluetooth.BluetoothSocket;
19 | import android.content.Intent;
20 | import android.content.SharedPreferences;
21 | import android.os.Handler;
22 | import android.os.HandlerThread;
23 | import android.os.IBinder;
24 | import android.os.Looper;
25 | import android.os.Message;
26 | import android.os.Process;
27 | import android.support.v4.content.LocalBroadcastManager;
28 | import android.util.Log;
29 | import android.widget.Toast;
30 |
31 | public class CommService extends Service {
32 |
33 | private final static String TAG = "CommService";
34 | //final Handler handler = new Handler();
35 | private Looper mServiceLooper;
36 | private ServiceHandler mServiceHandler;
37 |
38 |
39 | private BluetoothSocket mSocket;
40 | private InputStream mIstream;
41 | private OutputStream mOstream;
42 | // private Handler mHandler;
43 | private String mMac;
44 | private String mDeviceName;
45 | private BluetoothAdapter mBluetoothAdapter;
46 |
47 | private StringBuffer sb;
48 | private boolean isAppSyncCompleted;
49 |
50 | private final static int THREAD_RECEIVE_DATA=1;
51 | private final static int THREAD_SEND_DATA=2;
52 |
53 |
54 | // Handler that receives messages from the thread
55 | private final class ServiceHandler extends Handler {
56 | public ServiceHandler(Looper looper) {
57 | super(looper);
58 | }
59 |
60 | @Override
61 | public void handleMessage(Message msg) {
62 | Log.d(TAG, "msg.what in handleMessage "+msg.what);
63 | switch(msg.what){
64 | case THREAD_RECEIVE_DATA:
65 | getData(msg);
66 | break;
67 | case THREAD_SEND_DATA:
68 | while(sb.length()>0){
69 | sendAppSyncBroadcast();
70 | }
71 | if(!(sb.length()>0))
72 | isAppSyncCompleted=true;
73 | break;
74 | }
75 |
76 | }
77 | }
78 |
79 | @Override
80 | public void onCreate() {
81 | // TODO Auto-generated method stub
82 | super.onCreate();
83 | //Toast.makeText(getApplicationContext(), "Service is started!",Toast.LENGTH_SHORT).show();
84 |
85 | }
86 |
87 | @Override
88 | public int onStartCommand(Intent intent, int flags, int startId) {
89 | // TODO Auto-generated method stub
90 | if (intent != null) {
91 | SharedPreferences Settings = getApplication().getSharedPreferences(
92 | "BTJOYSTICK0", 0);
93 | SharedPreferences.Editor editor = Settings.edit();
94 | editor.putString("Mac", intent.getStringExtra("Mac"));
95 | editor.putString("deviceNameStr",
96 | intent.getStringExtra("deviceNameStr"));
97 | editor.commit();
98 | }
99 | //Toast.makeText(getApplicationContext(), "Service is running!",Toast.LENGTH_SHORT).show();
100 |
101 | this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
102 | HandlerThread thread = new HandlerThread("ServiceStartArguments",
103 | Process.THREAD_PRIORITY_BACKGROUND);
104 | thread.start();
105 |
106 | // Get the HandlerThread's Looper and use it for our Handler
107 | mServiceLooper = thread.getLooper();
108 | mServiceHandler = new ServiceHandler(mServiceLooper);
109 |
110 | Message msg = mServiceHandler.obtainMessage();
111 | msg.what = THREAD_RECEIVE_DATA;
112 | msg.arg1=startId;
113 | mServiceHandler.sendMessage(msg);
114 |
115 | // If we get killed, after returning from here, restart
116 | return START_STICKY;
117 | }
118 |
119 | @Override
120 | public IBinder onBind(Intent arg0) {
121 | // TODO Auto-generated method stub
122 | return null;
123 | }
124 |
125 | @Override
126 | public void onDestroy() {
127 | // TODO Auto-generated method stub
128 | super.onDestroy();
129 | Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
130 | final Intent intent = new Intent("isBluetoothConnected");
131 | LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
132 | stopSelf();
133 | }
134 |
135 | public void getData(Message msg) {
136 | // get mac and devicename
137 |
138 | SharedPreferences settings = getApplication().getSharedPreferences(
139 | "BTJOYSTICK0", 0);
140 | this.mMac = settings.getString("Mac", "");
141 | this.mDeviceName = settings.getString("deviceNameStr", "");
142 |
143 | if (mBluetoothAdapter == null)
144 | return;
145 |
146 | Set devices = mBluetoothAdapter.getBondedDevices();
147 | BluetoothDevice device = null;
148 | try {
149 | for (BluetoothDevice curDevice : devices) {
150 | if (curDevice.getName().matches(mDeviceName)) {
151 |
152 | device = curDevice;
153 |
154 | break;
155 | }
156 | }
157 | } catch (Exception e) {
158 | e.printStackTrace();
159 | }
160 |
161 | if (device == null)
162 | try {
163 | device = mBluetoothAdapter.getRemoteDevice(mMac);
164 | } catch (Exception e) {
165 | e.printStackTrace();
166 | }
167 |
168 | try {
169 | mSocket = device.createRfcommSocketToServiceRecord(UUID
170 | .fromString("00001101-0000-1000-8000-00805F9B34FB"));
171 | mSocket.connect();
172 | } catch (IOException e) {
173 | Toast.makeText(getApplicationContext(), "Device not in Range!",Toast.LENGTH_SHORT).show();
174 | mSocket = null;
175 | }
176 | if (mSocket == null)
177 | return;
178 |
179 | InputStream tmpIn = null;
180 | OutputStream tmpOut = null;
181 |
182 | try {
183 | tmpIn = mSocket.getInputStream();
184 | tmpOut = mSocket.getOutputStream();
185 | } catch (IOException e) {
186 | }
187 |
188 |
189 | HandlerThread thread = new HandlerThread("AppSyncServiceStartArguments",
190 | Process.THREAD_PRIORITY_BACKGROUND);
191 | thread.start();
192 | // Get the HandlerThread's Looper and use it for our Handler
193 | mServiceLooper = thread.getLooper();
194 | mServiceHandler = new ServiceHandler(mServiceLooper);
195 |
196 | mIstream = tmpIn;
197 | mOstream = tmpOut;
198 | sb = new StringBuffer();
199 | byte[] buffer = new byte[1024]; // buffer store for the stream
200 | int bytes; // bytes returned from read()
201 | isAppSyncCompleted=true;
202 |
203 | while (true) {
204 | try {
205 |
206 | // Read from the InputStream
207 | bytes = mIstream.read(buffer);
208 | sb.append(new String(buffer, 0, bytes));
209 | Log.d("iSTREAM", "...Data from arduino Stringbuffers "+sb);
210 | int endindex=sb.indexOf(";");
211 | if(endindex!=-1){
212 | //Execute background task if not empty
213 | if(isAppSyncCompleted)
214 | startAppSyncThread();
215 | }else if(endindex<1){
216 | sb.replace(0, endindex+1, "");
217 | }
218 |
219 | } catch (IOException e) {
220 | break;
221 | }
222 | }
223 | // Stop the service using the startId, so that we don't stop
224 | // the service in the middle of handling another job
225 | stopSelf(msg.arg1);
226 | }
227 |
228 | private void startAppSyncThread() {
229 |
230 | Log.d(TAG, "startAppSyncThread() is called");
231 | Message msg = mServiceHandler.obtainMessage();
232 | msg.what = THREAD_SEND_DATA;
233 | mServiceHandler.sendMessage(msg);
234 | }
235 | private void sendAppSyncBroadcast() {
236 | isAppSyncCompleted=false;
237 | String message;
238 | int endindex=sb.indexOf(";");
239 | if(endindex!=-1){
240 | message=sb.substring(0, endindex);
241 | Log.d(TAG, "data from arduino message" + message);
242 |
243 | if (!message.equalsIgnoreCase("")) {
244 |
245 | final Intent intent = new Intent("speedExceeded");
246 | intent.putExtra("Message", message);
247 | LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(
248 | intent);
249 | sb.replace(0, endindex+1, "");
250 | }else if(endindex<1){
251 | sb.replace(0, endindex+1, "");
252 | }
253 |
254 | }
255 | }
256 |
257 | /* Call this from the main Activity to shutdown the connection */
258 | public void cancel() {
259 | try {
260 | if (mSocket != null)
261 | mSocket.close();
262 | } catch (IOException e) {
263 | }
264 | }
265 |
266 |
267 | }
268 |
--------------------------------------------------------------------------------
/src/com/damas/blejoystickapp/bluetooth/CustomizedBluetoothDevice.java:
--------------------------------------------------------------------------------
1 | package com.damas.blejoystickapp.bluetooth;
2 |
3 | import android.bluetooth.BluetoothDevice;
4 |
5 |
6 |
7 | public class CustomizedBluetoothDevice {
8 |
9 | private String name;
10 | private String address;
11 | private boolean statusPaired;
12 |
13 | public CustomizedBluetoothDevice(BluetoothDevice device) {
14 | this.name = device.getName();
15 | this.address = device.getAddress();
16 | this.statusPaired = device.getBondState() == BluetoothDevice.BOND_BONDED;
17 | }
18 |
19 | @Override
20 | public int hashCode() {
21 | final int prime = 31;
22 | int result = 1;
23 | result = prime * result + ((address == null) ? 0 : address.hashCode());
24 | return result;
25 | }
26 |
27 | @Override
28 | public boolean equals(Object obj) {
29 | if (this == obj) {
30 | return true;
31 | }
32 | if (obj == null) {
33 | return false;
34 | }
35 | if (!(obj instanceof CustomizedBluetoothDevice)) {
36 | return false;
37 | }
38 | CustomizedBluetoothDevice other = (CustomizedBluetoothDevice) obj;
39 | if (address == null) {
40 | if (other.address != null) {
41 | return false;
42 | }
43 | } else if (!address.equals(other.address)) {
44 | return false;
45 | }
46 | return true;
47 | }
48 |
49 | /**
50 | * @return the name
51 | */
52 | public String getName() {
53 | return name;
54 | }
55 |
56 | /**
57 | * @param name
58 | * the name to set
59 | */
60 | public void setName(String name) {
61 | this.name = name;
62 | }
63 |
64 | /**
65 | * @return the address
66 | */
67 | public String getAddress() {
68 | return address;
69 | }
70 |
71 | /**
72 | * @param address
73 | * the address to set
74 | */
75 | public void setAddress(String address) {
76 | this.address = address;
77 | }
78 |
79 | /**
80 | * @return the statusPaired
81 | */
82 | public boolean isStatusPaired() {
83 | return statusPaired;
84 | }
85 |
86 | /**
87 | * @param statusPaired
88 | * the statusPaired to set
89 | */
90 | public void setStatusPaired(boolean statusPaired) {
91 | this.statusPaired = statusPaired;
92 | }
93 | }
--------------------------------------------------------------------------------