简体   繁体   中英

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

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 );

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);

            

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