Why my bluetooth app does not work on my mobile phone and works in another phone

Viewed 24

I am developing a bluetooth chat following a Youtube tutorial, saveral days ago the function of search bluetooth devices availables around me, worked but suddenly to the next day, it did not nothing. I tried search information and videos, maybe i changed something but nothing, after this for last chance i tried my app in a friend´s mobile phone, and it worked well. So i do not know how can I proceed or how can i fix this.

I am using a Honor 9X (STK-LX1) with Android 10 and EMUI 10.0.0, the API is 30. Now I test the app in another friend´s mobile, and it worked.

public class DeviceListActivity extends AppCompatActivity {

private Context context;
private ListView listPairedDevices, listAvailableDevices;
private ArrayAdapter<String> adapterPairedDevices, adapterAvailableDevices;

private BluetoothAdapter bluetoothAdapter;
private ProgressBar progressScanDevices;

private Set<BluetoothDevice> foundDevices = new HashSet<>();


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

    context = this;
    init();
}

private void init(){
    listPairedDevices = findViewById(R.id.list_paired_devices);
    listAvailableDevices = findViewById(R.id.list_available_devices);
    progressScanDevices = findViewById(R.id.progress_scan_devices);

    adapterPairedDevices = new ArrayAdapter<String>(context,R.layout.device_list_item);
    adapterAvailableDevices = new ArrayAdapter<String>(context, R.layout.device_list_item);

    listPairedDevices.setAdapter(adapterPairedDevices);
    listAvailableDevices.setAdapter(adapterAvailableDevices);

   listAvailableDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String info = ((TextView)view).getText().toString();
            String mac = info.substring(info.length()-17);

            Intent intent = new Intent();
            intent.putExtra("deviceAddress",mac);
            setResult(RESULT_OK,intent);
            finish();
        }
    });

    listPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String info = ((TextView)view).getText().toString();
            String address = info.substring(info.length()-17);

            Intent intent = new Intent();
            intent.putExtra("deviceAddress",address);
            setResult(RESULT_OK,intent);
            finish();
        }
    });

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

    if(pairedDevices != null && pairedDevices.size()>0){
        for( BluetoothDevice device: pairedDevices){
            adapterPairedDevices.add(device.getName() + "\n"+ device.getAddress());
        }

    }

    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(bluetoothDeviceListener,intentFilter);

    IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(bluetoothDeviceListener,intentFilter1);

    IntentFilter intentFilter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(bluetoothDeviceListener,intentFilter2);

}



private BroadcastReceiver bluetoothDeviceListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();


        if(BluetoothDevice.ACTION_FOUND.equals(action)) {

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                String deviceName = device.getName();


                if(deviceName!=null){

                    if(foundDevices.contains(device))
                        return;
                    foundDevices.add(device);
                    adapterAvailableDevices.add(device.getName() + "\n" + device.getAddress());
                }

            }
        }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
            progressScanDevices.setVisibility(View.GONE);
            if(adapterAvailableDevices.getCount() == 0){
                Toast.makeText(context, "No new devices found",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "Click on the device to start the chat",Toast.LENGTH_SHORT).show();
            }
        }else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){


        }
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_device_list, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.menu_scan_devices:
            scanDevices();
            return true;
        default:
            return super.onOptionsItemSelected(item);

    }

}

private void scanDevices(){
    progressScanDevices.setVisibility(View.VISIBLE);
    adapterAvailableDevices.clear();

    //Toast.makeText(context, "Scan Devices",Toast.LENGTH_SHORT).show();
    if(bluetoothAdapter.isDiscovering()){
        bluetoothAdapter.cancelDiscovery();
    }
    bluetoothAdapter.startDiscovery();
}

}

Edit 1: I put Log.e("Tag 1", "Start search") at the end of the function scanDevices and another Log.e("Tag2","BroadcastReceiver") inside the onReceive function of BroadcastReceiver When i use the Honor 9x, only appears the first Log.e, but when i use the anothers mobile phones, it appears the first and second.

Edit 2: I have the uses permission on my manifest, BLUETOOTH_ADMIN, BLUETOOTH, ACCESS_FINE_LOCATION.

I would appreciate any help.

0 Answers
Related