Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMPROVED Bluetooth Service #734

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ android {
outputFileName = new File("remote-" + android.defaultConfig.versionCode + ".apk")
}
}
multiDexEnabled true

}
buildTypes {
release {
Expand All @@ -40,31 +42,34 @@ dependencies {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'
implementation 'com.mikepenz:materialdrawer:6.1.1'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.annotation:annotation:1.0.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.mikepenz:google-material-typeface:3.0.1.3.original@aar'
implementation 'com.mikepenz:fontawesome-typeface:5.3.1.1@aar'
implementation 'com.mikepenz:octicons-typeface:3.2.0.5@aar'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.mikepenz:itemanimators:1.1.0'
implementation 'com.mikepenz:crossfader:1.5.1@aar'
implementation 'com.mikepenz:crossfadedrawerlayout:1.0.1@aar'
implementation 'com.google.android:flexbox:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.preference:preference:1.1.0"
implementation "androidx.preference:preference:1.1.1"
testImplementation 'junit:junit:4.12'
implementation 'com.github.parse-community:ParseLiveQuery-Android:1.1.0'
implementation 'com.google.code.gson:gson:2.8.6'
androidTestImplementation 'androidx.test:runner:1.1.1'
implementation 'me.aflak.libraries:bluetooth:1.3.4'
androidTestImplementation 'androidx.test:runner:1.2.0'
implementation 'com.github.ivbaranov:rxbluetooth2:2.1.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.caverock:androidsvg-aar:1.4'
implementation 'com.android.support:multidex:1.0.3'



}
20 changes: 12 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACTION_PICK_WIFI_NETWORK" />
<uses-permission android:name="android.Manifest.permission.OVERRIDE_WIFI_CONFIG" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<!--<uses-permission android:name="android.permission.READ_PHONE_STATE" />-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Expand All @@ -37,15 +38,18 @@
<application
android:name=".MainApplication"
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:logo="@mipmap/ic_launcher_round"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:theme="@style/MyMaterialTheme.Base">
<activity android:name=".SplashScreenActivity"
android:screenOrientation="portrait">
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -56,9 +60,9 @@
android:name=".InitialActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/MyMaterialTheme.Base"
android:windowSoftInputMode="stateHidden|adjustResize">
</activity>
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize"
tools:ignore="LockedOrientationActivity" />

<service android:name=".utils.GPSService" />

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/io/treehouses/remote/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class Constants {
public static final int STATE_LISTEN = 1; // now listening for incoming connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
public static final int STATE_CONNECTED = 3; // now connected to a remote device
public static final int STATE_FAILED = -1;

// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,20 @@
import io.treehouses.remote.R;
import io.treehouses.remote.adapter.RPIListAdapter;
import io.treehouses.remote.bases.BaseDialogFragment;
import io.treehouses.remote.callback.BluetoothDeviceCallback;
import io.treehouses.remote.callback.SetDisconnect;
import io.treehouses.remote.pojo.DeviceInfo;

import static android.widget.Toast.LENGTH_LONG;

public class RPIDialogFragment extends BaseDialogFragment {

private static BluetoothChatService mChatService = null;
public class RPIDialogFragment extends BaseDialogFragment implements BluetoothDeviceCallback {

private static RPIDialogFragment instance = null;
private List<BluetoothDevice> raspberry_devices = new ArrayList<BluetoothDevice>(), all_devices = new ArrayList<BluetoothDevice>();
private Set<BluetoothDevice> pairedDevices;
private static BluetoothDevice mainDevice = null;
private ListView listView;
private ArrayAdapter mArrayAdapter;
private BluetoothAdapter mBluetoothAdapter;
private SetDisconnect checkConnectionState;
private Context context;
private Switch mDiscoverRaspberry;
Expand All @@ -77,25 +75,19 @@ public static androidx.fragment.app.DialogFragment newInstance(int num) {
public Dialog onCreateDialog(Bundle savedInstanceState) {
instance = this;
context = getContext();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothCheck();
if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); }
mBluetoothAdapter.startDiscovery();
LayoutInflater inflater = Objects.requireNonNull(getActivity()).getLayoutInflater();
LayoutInflater inflater = getActivity().getLayoutInflater();
final View mView = inflater.inflate(R.layout.activity_rpi_dialog_fragment, null);
initDialog(mView);

if (mChatService == null) { mChatService = new BluetoothChatService(mHandler, getActivity().getApplicationContext()); }

pairedDevices = mBluetoothAdapter.getBondedDevices();
mChatService.updateHandler(mHandler);
mChatService.startDiscovery(this);
pairedDevices = mChatService.getPairedDevices();
setAdapterNotNull(raspberryDevicesText);
for (BluetoothDevice d : pairedDevices) {
if (checkPiAddress(d.getAddress())) {
addToDialog(d, raspberryDevicesText, raspberry_devices, false);
progressBar.setVisibility(View.INVISIBLE); }
}
intentFilter();

return mDialog;
}

Expand All @@ -106,33 +98,29 @@ private void initDialog(View mView) {
mDialog.setTitle(R.string.select_device);
listViewOnClickListener(mView);
Button mCloseButton = mView.findViewById(R.id.rpi_close_button);
mCloseButton.setOnClickListener(v -> {
bluetoothCheck("unregister");
dismiss();
mCloseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkConnectionState.checkConnectionState();
mChatService.disconnect();
dismiss();
}
});
mDiscoverRaspberry = mView.findViewById(R.id.rpi_switch);
mDiscoverRaspberry.setChecked(true);
switchViewOnClickListener();
progressBar = mView.findViewById(R.id.progressBar);
}

private void intentFilter() {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
getActivity().registerReceiver(mReceiver, filter);
}

private void listViewOnClickListener(View mView) {
listView.setOnItemClickListener((parent, view, position, id) -> {
mChatService = new BluetoothChatService(mHandler, getActivity().getApplicationContext());
mChatService.updateHandler(mHandler);
List<BluetoothDevice> deviceList;
if (mDiscoverRaspberry.isChecked()) deviceList = raspberry_devices;
else deviceList = all_devices;
if (checkPiAddress(deviceList.get(position).getAddress())) {
mainDevice = deviceList.get(position);
mChatService.connect(deviceList.get(position),true);
mChatService.connectToDevice(deviceList.get(position));
int status = mChatService.getState();
mDialog.cancel();
finish(status, mView);
Expand Down Expand Up @@ -179,48 +167,29 @@ private void finish(int status, View mView) {
@Override
public void onDestroy() {
super.onDestroy();
try { if (mBluetoothAdapter == null) context.unregisterReceiver(mReceiver); } catch (Exception e) { e.printStackTrace(); }
}

private AlertDialog getAlertDialog(View mView, Context context, Boolean wifi) {
return new AlertDialog.Builder(context).setView(mView).setIcon(R.drawable.dialog_icon).create();
}

public void bluetoothCheck(String... args) {
if (mBluetoothAdapter == null) {
public void bluetoothCheck() {
if (!mChatService.isBluetoothSupported()) {
Toast.makeText(getActivity(), "Your Bluetooth Is Not Enabled or Not Supported", LENGTH_LONG).show();
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
context.unregisterReceiver(mReceiver);
// getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
dismiss();
}
if (args.length >= 1) {
if (args[0].equals("unregister")) {
context.unregisterReceiver(mReceiver);
Intent intent = new Intent();
intent.putExtra("mChatService", mChatService);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
}
else if (!mChatService.isBluetoothEnabled()) {
Toast.makeText(context, "Please enable bluetooth", Toast.LENGTH_SHORT).show();
}

}

private void setAdapterNotNull(List<DeviceInfo> listVal) {
mArrayAdapter = new RPIListAdapter(getContext(), listVal);
listView.setAdapter(mArrayAdapter);
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (checkPiAddress(device.getAddress())) {
addToDialog(device, raspberryDevicesText, raspberry_devices, true);
progressBar.setVisibility(View.INVISIBLE);
}
addToDialog(device, allDevicesText, all_devices, true);
Log.e("Broadcast BT", device.getName() + "\n" + device.getAddress());
}
}
};

private void addToDialog(BluetoothDevice device, List<DeviceInfo> textList, List<BluetoothDevice> mDevices, Boolean inRange) {
if (!mDevices.contains(device)){
mDevices.add(device);
Expand Down Expand Up @@ -248,17 +217,17 @@ public void handleMessage(Message msg) {

switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
checkConnectionState.checkConnectionState();
switch (msg.arg1) {
case Constants.STATE_CONNECTED:
Log.e("RPIDialogFragment", "Bluetooth Connection Status Change: State Listen");
Log.e("RPIDialogFragment", "Bluetooth Connection Status Change: State Connected");
pDialog.dismiss();
listener.setChatService(mChatService);
checkConnectionState.checkConnectionState();
mBluetoothAdapter.cancelDiscovery();
dismiss();
Toast.makeText(context, "Bluetooth Connected", LENGTH_LONG).show();
break;
case Constants.STATE_NONE:
case Constants.STATE_FAILED:
pDialog.dismiss();
dismiss();
Toast.makeText(context, "Connection Failed: Please Try Again", LENGTH_LONG).show();
Log.e("RPIDialogFragment", "Bluetooth Connection Status Change: State None");
break;
Expand All @@ -272,4 +241,14 @@ public void handleMessage(Message msg) {
};

public Handler getmHandler() { return mHandler; }

@Override
public void onDeviceFound(BluetoothDevice device) {
Log.e("RPIDIALOG", "onDeviceFound: "+ device.getName());
if (checkPiAddress(device.getAddress())) {
addToDialog(device, raspberryDevicesText, raspberry_devices, true);
progressBar.setVisibility(View.INVISIBLE);
}
addToDialog(device, allDevicesText, all_devices, true);
}
}
Loading