-
Notifications
You must be signed in to change notification settings - Fork 71
/
Help_Fragment.java
159 lines (135 loc) · 5.12 KB
/
Help_Fragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package edu.cs4730.blescannerdemo;
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class Help_Fragment extends Fragment {
String TAG = "HelpFragment";
/**
* This is the callback variable, for the button to launch the server or client fragment from the mainActivity.
*/
private OnFragmentInteractionListener mListener;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface OnFragmentInteractionListener {
/**
* Callback for when an item has been selected.
*/
public void onButtonSelected(int id);
}
//bluetooth device and code to turn the device on if needed.
BluetoothAdapter mBluetoothAdapter = null;
private static final int REQUEST_ENABLE_BT = 2;
Button btn_client, btn_server;
TextView logger;
public Help_Fragment() {
// Required empty public constructor
}
//This code will check to see if there is a bluetooth device and
//turn it on if is it turned off.
public void startbt() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
logthis("This device does not support bluetooth");
return;
}
//make sure bluetooth is enabled.
if (!mBluetoothAdapter.isEnabled()) {
logthis("There is bluetooth, but turned off");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
logthis("The bluetooth is ready to use.");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myView = inflater.inflate(R.layout.fragment_help, container, false);
logger = (TextView) myView.findViewById(R.id.logger1);
btn_client = (Button) myView.findViewById(R.id.button2);
btn_client.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) //don't call if null, duh...
mListener.onButtonSelected(2);
}
});
startbt();
checkpermissions();
return myView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
//bluetooth result code.
if (resultCode == Activity.RESULT_OK) {
logthis("Bluetooth is on.");
} else {
logthis("Please turn the bluetooth on.");
}
}
}
@Override
public void onResume() {
super.onResume();
checkpermissions();
}
void checkpermissions() {
//first check to see if I have permissions (marshmallow) if I don't then ask, otherwise start up the demo.
if ((ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) ) {
//I'm on not explaining why, just asking for permission.
logthis( "asking for permissions");
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
MainActivity.REQUEST_ACCESS_COURSE_LOCATION);
logthis("We don't have permission to course location");
} else {
logthis("We have permission to course location");
}
}
/*
* This is all for the callbacks.
*
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnFragmentInteractionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void logthis(String msg) {
logger.append(msg + "\n");
Log.d(TAG, msg);
}
}