简体   繁体   中英

Android Bluetooth Low Energy: Switching from Peripheral To Central

I think I'm hitting against a wall here. I have two devices: a Mediatek Desire Plus running Android 6.0 and another, an ASUS X008DC running Android 6.0. On one device I run a ReaderActivity and on the other (the ASUS) I run a WriterActivity. The ReaderActivity is supposed to do the following:

  1. Scan for Peripheral devices with a specific service UUID, UUID_1
  2. Connect to the device, stop scanning, and start reading characteristics provided in the service description one after the other.
  3. After reading ALL the characteristics, start as a Peripheral and advertise a service with UUID, UUID_2.

The WriterActivity is supposed to do the reverse of the ReaderActivity:

  1. Advertise a given service with UUID, UUID_1
  2. When connected to a central, stop advertising and start serving characteristics one after the other (mostly in chunks on 20 bytes each)
  3. After serving ALL the characteristics, stop (close) the GATT Server and start as Central looking for a service with UUID, UUID_2.

The problem: everything works FINE on both devices UNTIL point 2. After switching roles, both devices DON'T see each other, in other words, the onScanResult of the WriterActivity IS NEVER CALLED.

I have tried ALMOST everything: from rebooting bluetooth (using an asynchronous BroadcastReceiver), to interrupting the scanning/advertising on both devices for a given idle period, to stopping the WIFI connection, to ...

I'm at THIS to give up on BLE and go back to standard Bluetooth. It's probably not a stable technology yet. What am I missing here? Thanks in advance for your time.

EDIT 1: I have installed the "BLE Central,Peripheral Checker" from Play Store on both devices and it indicates that BOTH devices support BT classic, Central and Peripheral.

The error was that one device, the MediaTek Desire, didn't require Android 23 permission request, while the other the ASUS, was more strict and required it to allow the scanning of BLE devices. Here is the function, called inside the onCreate method, that fixed the issue for good:

private void checkNetworkLocation() {
    try {
        boolean gps_enabled = false, network_enabled = false;
        LocationManager localLocationManager =
                (LocationManager) getSystemService(LOCATION_SERVICE);;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if ((checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                    || (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
                throw new Exception();
            }
        }
        try {
            gps_enabled = localLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch(Exception ex) {}

        try {
            network_enabled = localLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch(Exception ex) {}

        if(!gps_enabled && !network_enabled) {
            new AlertDialog.Builder(this).setMessage("Location service is disabled, please enable it to find BLE devices.").setCancelable(false).setNeutralButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                    DeviceScanActivity.this.startActivityForResult(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"), 0);
                }
            }).show();
        }
        return;
    } catch (Exception e) {
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM