2121import java .util .Set ;
2222import java .util .UUID ;
2323
24+ import io .github .controlwear .virtual .joystick .android .JoystickView ;
25+
2426public class ControlActivity extends AppCompatActivity implements View .OnClickListener {
25- boolean statusLed =false , statusBuzzer =false ;
27+ private boolean statusLed =false , statusBuzzer =false ;
2628 public static BluetoothManager bluetoothManager ;
27- ImageView ibUp , ibLeft , ibRight , ibDown , ibBuzzer , ibLeds , ibStop , ibConnect ;
29+ private ImageView ibBuzzer , ibLeds , ibConnect ;
30+ private JoystickView joystick ;
2831
2932
3033 @ Override
3134 protected void onCreate (Bundle savedInstanceState ) {
3235 super .onCreate (savedInstanceState );
3336 setContentView (R .layout .activity_control );
37+ joystick =findViewById (R .id .joystick );
3438 ibConnect =findViewById (R .id .ibConnect );
3539 ibBuzzer =findViewById (R .id .ibBuzzer );
3640 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 );
4241
4342 bluetoothManager =new BluetoothManager (this );
4443 if (bluetoothManager .getmBluetoothAdapter () == null ) {
4544 // Device does not support Bluetooth
4645 Toast .makeText (this , "The devicie doesn't support bluetooth" , Toast .LENGTH_SHORT ).show ();
4746 finish ();
4847 }
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+ });
4975 ibConnect .setOnClickListener (this );
5076 ibBuzzer .setOnClickListener (this );
5177 ibLeds .setOnClickListener (this );
52- ibStop .setOnClickListener (this );
53- ibUp .setOnClickListener (this );
54- ibLeft .setOnClickListener (this );
55- ibRight .setOnClickListener (this );
56- ibDown .setOnClickListener (this );
5778 }
5879
5980 @ Override
@@ -65,19 +86,25 @@ protected void onResume() {
6586 @ Override
6687 public void onClick (View view ) {
6788 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+ }
75104 }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 );
81108 }
82109 } else {
83110 Toast .makeText (getApplicationContext (), "Select a device" , Toast .LENGTH_SHORT ).show ();
@@ -87,21 +114,6 @@ public void onClick(View view) {
87114 }
88115 if (bluetoothManager .isDeviceConnected ()) {
89116 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 ;
105117 case R .id .ibBuzzer :
106118 if (statusBuzzer ) {
107119 bluetoothManager .sendMessage ("z" );
0 commit comments