Android bluetooth LE advertisement callback returns successful but the device doesn't show on any nearby device scans?

329 Views Asked by At

The device is an LG LM-X410PM with Android 8.1.0. The code works good as a client, able to read characteristics from other devices. But when I use it as a server to advertise, it returns with success but doesn't appear on other device scans? Here's how I advertise:

            btAdvertiser = btAdapter.getBluetoothLeAdvertiser();
            AdvertiseSettings advertiseSettings = new AdvertiseSettings.Builder()
                    .setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
                    .setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
                    .setConnectable( false )
                    .build();

            ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );
            AdvertiseData data = new AdvertiseData.Builder()
                    .setIncludeDeviceName( true )
                    .addServiceData( pUuid, "Data".getBytes( Charset.forName( "UTF-8" ) ) )
                    .build();

            btAdvertiser.startAdvertising( advertiseSettings, data, advertisingCallback );
1

There are 1 best solutions below

0
niedev On

Probably you need to addServiceUuid instead of add uuid via addServiceData, and if you want to connect to the advertizing device you need to set connectable to true, with this code I had no problem

 AdvertiseSettings advertiseSettings = new AdvertiseSettings.Builder()
          .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) //alto
          .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)    //alto
          .setConnectable(true)
          .setTimeout(0)
          .build();
 AdvertiseData advertiseData = new AdvertiseData.Builder()
          .addServiceUuid(new ParcelUuid(UUID.fromString(getString(R.string.ble_uuid))))
          .setIncludeDeviceName(true)
          .build();

 BluetoothLeAdvertiser advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
 advertiser.startAdvertising(advertiseSettings, advertiseData, advertiseCallback);