21
21
import java .util .Set ;
22
22
import java .util .UUID ;
23
23
24
+ import io .github .controlwear .virtual .joystick .android .JoystickView ;
25
+
24
26
public class ControlActivity extends AppCompatActivity implements View .OnClickListener {
25
- boolean statusLed =false , statusBuzzer =false ;
27
+ private boolean statusLed =false , statusBuzzer =false ;
26
28
public static BluetoothManager bluetoothManager ;
27
- ImageView ibUp , ibLeft , ibRight , ibDown , ibBuzzer , ibLeds , ibStop , ibConnect ;
29
+ private ImageView ibBuzzer , ibLeds , ibConnect ;
30
+ private JoystickView joystick ;
28
31
29
32
30
33
@ Override
31
34
protected void onCreate (Bundle savedInstanceState ) {
32
35
super .onCreate (savedInstanceState );
33
36
setContentView (R .layout .activity_control );
37
+ joystick =findViewById (R .id .joystick );
34
38
ibConnect =findViewById (R .id .ibConnect );
35
39
ibBuzzer =findViewById (R .id .ibBuzzer );
36
40
ibLeds =findViewById (R .id .ibLeds );
37
- ibStop =findViewById (R .id .ibStop );
38
- ibUp =findViewById (R .id .ibUp );
39
- ibDown =findViewById (R .id .ibDown );
40
- ibLeft =findViewById (R .id .ibLeft );
41
- ibRight =findViewById (R .id .ibRight );
42
41
43
42
bluetoothManager =new BluetoothManager (this );
44
43
if (bluetoothManager .getmBluetoothAdapter () == null ) {
45
44
// Device does not support Bluetooth
46
45
Toast .makeText (this , "The devicie doesn't support bluetooth" , Toast .LENGTH_SHORT ).show ();
47
46
finish ();
48
47
}
48
+
49
+ joystick .setOnMoveListener (new JoystickView .OnMoveListener () {
50
+ @ Override
51
+ public void onMove (int angle , int strength ) {
52
+ if (bluetoothManager .isDeviceConnected ()) {
53
+ if (strength > 10 ) {
54
+ if ((angle <= 45 && angle >= 0 ) || (angle >= 315 && angle < 360 ))
55
+ //right
56
+ bluetoothManager .sendMessage ("b" );
57
+ else if ((angle <= 135 && angle >= 90 ) || (angle > 45 && angle <= 89 ))
58
+ //up
59
+ bluetoothManager .sendMessage ("a" );
60
+ else if ((angle <= 225 && angle >= 180 ) || (angle > 135 && angle <= 179 ))
61
+ //left
62
+ bluetoothManager .sendMessage ("d" );
63
+ else if ((angle <= 315 && angle >= 270 ) || (angle > 225 && angle <= 269 ))
64
+ //down
65
+ bluetoothManager .sendMessage ("e" );
66
+ } else {
67
+ bluetoothManager .sendMessage ("c" );
68
+ }
69
+ }else {
70
+ Toast .makeText (getApplicationContext (), "First connect the bluetooth" , Toast .LENGTH_SHORT ).show ();
71
+ }
72
+
73
+ }
74
+ });
49
75
ibConnect .setOnClickListener (this );
50
76
ibBuzzer .setOnClickListener (this );
51
77
ibLeds .setOnClickListener (this );
52
- ibStop .setOnClickListener (this );
53
- ibUp .setOnClickListener (this );
54
- ibLeft .setOnClickListener (this );
55
- ibRight .setOnClickListener (this );
56
- ibDown .setOnClickListener (this );
57
78
}
58
79
59
80
@ Override
@@ -65,19 +86,25 @@ protected void onResume() {
65
86
@ Override
66
87
public void onClick (View view ) {
67
88
if (view .getId ()==R .id .ibConnect ){
68
- if (!bluetoothManager .DEVICE_ADDRESS .equals ("" ) && bluetoothManager .DEVICE_ADDRESS !=null ) {
69
- if (bluetoothManager .isDeviceConnected ()){
70
- if (bluetoothManager .connectDisconnect ()){
71
- ibConnect .setImageResource (R .drawable .connect );
72
- ibConnect .setBackgroundResource (R .color .colorAccent );
73
- Toast .makeText (getApplicationContext (), "Disconnected" , Toast .LENGTH_SHORT ).show ();
74
- }else Toast .makeText (getApplicationContext (), "The device could not disconnect" , Toast .LENGTH_SHORT ).show ();
89
+ if (bluetoothManager .DEVICE_ADDRESS !=null ) {
90
+ if (!bluetoothManager .DEVICE_ADDRESS .equals ("" )){
91
+ if (bluetoothManager .isDeviceConnected ()){
92
+ if (bluetoothManager .connectDisconnect ()){
93
+ ibConnect .setImageResource (R .drawable .connect );
94
+ ibConnect .setBackgroundResource (R .color .colorAccent );
95
+ Toast .makeText (getApplicationContext (), "Disconnected" , Toast .LENGTH_SHORT ).show ();
96
+ }else Toast .makeText (getApplicationContext (), "The device could not disconnect" , Toast .LENGTH_SHORT ).show ();
97
+ }else {
98
+ if (bluetoothManager .connectDisconnect ()){
99
+ ibConnect .setImageResource (R .drawable .disconnect );
100
+ ibConnect .setBackgroundColor (Color .RED );
101
+ Toast .makeText (getApplicationContext (), "Connected" , Toast .LENGTH_SHORT ).show ();
102
+ }else Toast .makeText (getApplicationContext (), "The device could not connect" , Toast .LENGTH_SHORT ).show ();
103
+ }
75
104
}else {
76
- if (bluetoothManager .connectDisconnect ()){
77
- ibConnect .setImageResource (R .drawable .disconnect );
78
- ibConnect .setBackgroundColor (Color .RED );
79
- Toast .makeText (getApplicationContext (), "Connected" , Toast .LENGTH_SHORT ).show ();
80
- }else Toast .makeText (getApplicationContext (), "The device could not connect" , Toast .LENGTH_SHORT ).show ();
105
+ Toast .makeText (getApplicationContext (), "Select a device" , Toast .LENGTH_SHORT ).show ();
106
+ Intent intent = new Intent (this , PairedDevices .class );
107
+ startActivity (intent );
81
108
}
82
109
} else {
83
110
Toast .makeText (getApplicationContext (), "Select a device" , Toast .LENGTH_SHORT ).show ();
@@ -87,21 +114,6 @@ public void onClick(View view) {
87
114
}
88
115
if (bluetoothManager .isDeviceConnected ()) {
89
116
switch (view .getId ()) {
90
- case R .id .ibUp :
91
- bluetoothManager .sendMessage ("a" );
92
- break ;
93
- case R .id .ibRight :
94
- bluetoothManager .sendMessage ("b" );
95
- break ;
96
- case R .id .ibStop :
97
- bluetoothManager .sendMessage ("c" );
98
- break ;
99
- case R .id .ibLeft :
100
- bluetoothManager .sendMessage ("d" );
101
- break ;
102
- case R .id .ibDown :
103
- bluetoothManager .sendMessage ("e" );
104
- break ;
105
117
case R .id .ibBuzzer :
106
118
if (statusBuzzer ) {
107
119
bluetoothManager .sendMessage ("z" );
0 commit comments