0

I am building an Android app that advertises small packets of information as well as scan for information using BLE. The code for advertising is:

   AdvertiseSettings advSettings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
                .setConnectable(false)
                .build();


        AdvertiseData advData = new AdvertiseData.Builder()
                .setIncludeTxPowerLevel(true)
                .addServiceUuid(uuid)
                .build();

        AdvertiseData advScanResponse = new AdvertiseData.Builder()
                .setIncludeDeviceName(false)
                .addServiceData(uuid,"1234567890".getBytes( Charset.forName( "UTF-8" ) ))
                .build();

        AdvertiseCallback advCallback = new AdvertiseCallback() {
            String TAG="status";

            @Override
            public void onStartFailure(int errorCode) {
                super.onStartFailure(errorCode);
                Log.e(TAG, "Not broadcasting: " + errorCode);
                int statusText;
                switch (errorCode) {
                    case ADVERTISE_FAILED_ALREADY_STARTED:
                        Log.w(TAG, "ADVERTISE_FAILED_ALREADY_STARTED");
                        break;
                    case ADVERTISE_FAILED_DATA_TOO_LARGE:
                        Log.w(TAG, "ADVERTISE_FAILED_DATA_TOO_LARGE");
                        break;
                    case ADVERTISE_FAILED_FEATURE_UNSUPPORTED:
                        Log.w(TAG, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED");
                        break;
                    case ADVERTISE_FAILED_INTERNAL_ERROR:
                        Log.w(TAG, "ADVERTISE_FAILED_INTERNAL_ERROR");
                        break;
                    case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS:
                        Log.w(TAG, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS");
                        break;
                    default:
                        Log.wtf(TAG, "Unhandled error: " + errorCode);
                }
                Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                super.onStartSuccess(settingsInEffect);
                Log.v(TAG, "Advertising started");
                mText.setText(uuid.toString());
                Toast.makeText(MainActivity.this, "Started", Toast.LENGTH_SHORT).show();
            }
        };

        mBluetoothAdapter.getBluetoothLeAdvertiser()
                .startAdvertising(advSettings, advData, advScanResponse, advCallback);

and the code for listening is as follows:

mBluetoothLeScanner=mBluetoothAdapter.getBluetoothLeScanner();


    ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            if( result == null
                    || result.getDevice() == null
                     )
                return;

            if(result.getScanRecord().getServiceUuids()!=null) {

                ParcelUuid pp=result.getScanRecord().getServiceUuids().get(0);

                StringBuilder builder = new StringBuilder((pp.toString()+"\n"));

                builder.append(new String (result.getScanRecord().getServiceUuids().size()+""));

                mText.setText(builder.toString());
            }

I have tested it on multiple phones and it works fine. However, Android version 9.0 on Galaxy S8+ can't scan unless the location is turned on. I also tried in on Android version 9.0 on Redmi Note 7 and it works perfectly without the need of turning on the location. What is causing my problem?

  • Same with Pixel phones. I guess this is by design. Otherwise you can ask Google for an answer. – Emil Apr 16 '20 at 00:21

1 Answers1

0

Not sure about the Android settings on the Redmi Note 7, but for Android 9 devices ACCESS_COARSE_LOCATION needs to be turned on in order for Bluetooth to function. Maybe this is somehow turned on on the Redmi Note 7 regardless of the location settings, whereas on the S8+ this is tied to the location settings.

You can find more information here:-

https://stackoverflow.com/a/58232014/2215147

I hope this helps.

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72