1

enter image description hereI developed an application that have a bluetooth connection. For now I encountered an error (the app crashed) for user who is using Android 13 and above. The code for the checkPermission() is

private void checkPermissions() {
    String[] permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S) {
        //permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT,};
        permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH_SCAN,Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.CAMERA};
    }

    List<String> listPermissionsNeeded = new ArrayList<>();
    for (String p : permissions) {
        if (ContextCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(p);
        }
    }
    if (!listPermissionsNeeded.isEmpty())
        androidx.core.app.ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 10);
    else permissionOK();
}

Is there any error on this code ? Any idea ? Code below is where the BinFormActivity.java line 1795 and code for mLeScanCallback

public class LeDeviceListAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;

    LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<>();
    }

    public void addDevice(BluetoothDevice device) {
        if (!mLeDevices.contains(device)) {
            mLeDevices.add(device);
        }
    }

    public BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    void clear() {
        mLeDevices.clear();
    }

    //@Override
    public int getCount() {
        return mLeDevices.size();
    }

    //@Override
    Object getItem(int i) {
        return mLeDevices.get(i);
    }

    //@Override
    public long getItemId(int i) {
        return i;
    }

}

private Handler mHandler;
private static final long SCAN_PERIOD = 5000;  //5Sec

private void scanLeDevice(final boolean enable) {
    if (enable) {
        if (mHandler == null) {
            mHandler = new Handler();
        }
 
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            }
        }, SCAN_PERIOD);
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
}

private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String tmp_str = device.getName();
                        String target_name = "BCR";     
                        String target_name2 = "BT";     
                        if (tmp_str != null) {

                            if (tmp_str.length() > target_name.length()) {
                                String tmp_str2 = tmp_str.substring(0, target_name.length());
                                String tmp_str3 = tmp_str.substring( target_name.length()+3, target_name.length()+3+target_name2.length());   
                                if ((target_name.equals(tmp_str2))&&(target_name2.equals(tmp_str3))) {               
                                    mLeDeviceListAdapter.addDevice(device);
                                }
                            }
                        }

                    }
                });
            }
        };
//===
  • Posting codes and errors as images is discouraged here. Please consider adding the error message as text. Also, as far as I see in the error message, the error occurs in `BinFormActivity.java` at line 1795, after the `onLeScan` is called. So you should add all relevant code in that file as well. – Kozmotronik Aug 23 '23 at 14:39
  • Sorry with that. I've already stated the line at 1795 in BinFormActivity.java. Situation that I encountered right now is, I have two clients, both of them using Android 13. For client A, when I insert Android permission BLUETOOTH_ADMIN, the page did not crashed while client B need to remove BLUETOOTE_ADMIN to make the page not crashed. – Aqilah Mokhtarudin Aug 25 '23 at 01:30
  • Please post your implementation of `mLeScanCallback` – Ricky Mo Aug 25 '23 at 01:39
  • I updated already @RickyMo – Aqilah Mokhtarudin Aug 25 '23 at 03:35
  • 1
    The problem is `String tmp_str3 = tmp_str.substring( target_name.length()+3, target_name.length()+3+target_name2.length());`. There is no guarantee `tmp_str` is longer than `target_name.length()+3`, let alone `target_name.length()+3+target_name2.length()` – Ricky Mo Aug 25 '23 at 03:42
  • 1
    There is nothing wrong with bluetooth or android 13, any nearby device with name fewer than 8 characters can crash your app. You should change the `if` condition to `tmp_str.length() > target_name.length() + 3 + target_name2.length()` instead. – Ricky Mo Aug 25 '23 at 03:46

0 Answers0