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

Fixed the android app crash #190

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# These are supported funding model platforms

github: [martinohanlon]
github: avipars
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like a reasonable change to this PR

ko_fi: aviparshan
patreon: amcantech
buy_me_a_coffee: aviparshan
custom: ["https://paypal.me/AParshan", "https://cash.app/$AviPars"]
14 changes: 10 additions & 4 deletions clients/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stuffaboutcode.bluedot">

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />/>
android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

<!-- https://developer.android.com/develop/connectivity/bluetooth/bt-permissions#declare-android11-or-lower -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" />

<!-- the app doesn't seem relevant for hardware without bluetooth -->
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import androidx.core.app.ActivityCompat;
import androidx.preference.PreferenceManager;

import android.content.SharedPreferences;
import android.Manifest;

Expand All @@ -39,16 +40,14 @@ public class Devices
public static String EXTRA_ADDRESS = "device_address";
public static String EXTRA_NAME = "device_name";

private static String[] PERMISSIONS = {
private static final String[] PERMISSIONS = {
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,

};

private void checkPermissions(){
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
private void checkPermissions() {
if (!approvedPermissions()) {
ActivityCompat.requestPermissions(
this,
PERMISSIONS,
Expand All @@ -57,19 +56,27 @@ private void checkPermissions(){
}
}

private boolean approvedPermissions() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED;
}
return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devices);

devicelist = (ListView)findViewById(R.id.listView);
devicelist = (ListView) findViewById(R.id.listView);

checkPermissions();

//if the device has bluetooth
myBluetooth = BluetoothAdapter.getDefaultAdapter();

if(myBluetooth == null) {
if (myBluetooth == null) {
Toast.makeText(
getApplicationContext(),
"Bluetooth Device Not Available",
Expand All @@ -79,10 +86,10 @@ protected void onCreate(Bundle savedInstanceState) {
this.finish();
System.exit(0);

} else if(!myBluetooth.isEnabled()) {
} else if (!myBluetooth.isEnabled()) {
//Ask to the user turn the bluetooth on
Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnBTon,1);
startActivityForResult(turnBTon, 1);
}

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Expand Down Expand Up @@ -146,25 +153,34 @@ private void setConnectMsg(SharedPreferences sharedPreferences) {
}

private void pairedDevicesList() {
pairedDevices = myBluetooth.getBondedDevices();
ArrayList<String> list = new ArrayList<String>();

if (pairedDevices.size()>0) {
// create a list of paired bluetooth devices
for(BluetoothDevice bt : pairedDevices)
{
list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
}
} else {
if (!approvedPermissions()) {
Toast.makeText(
getApplicationContext(),
"No Paired Bluetooth Devices Found.",
"Bluetooth Permissions Not Granted",
Toast.LENGTH_LONG).show();
checkPermissions(); // Ask for permissions again
} else {
pairedDevices = myBluetooth.getBondedDevices();
ArrayList<String> list = new ArrayList<String>();

if (pairedDevices.size()>0) {
// create a list of paired bluetooth devices
for(BluetoothDevice bt : pairedDevices)
{
list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
}
} else {
Toast.makeText(
getApplicationContext(),
"No Paired Bluetooth Devices Found.",
Toast.LENGTH_LONG).show();
}

final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
devicelist.setAdapter(adapter);
devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked
}

final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
devicelist.setAdapter(adapter);
devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked
}

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener()
Expand Down