private void onServiceAdded() {
    final BluetoothLeAdvertiser bluetoothLeAdvertiser =
        mBluetoothManager.getAdapter().getBluetoothLeAdvertiser();
    AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
    dataBuilder.setIncludeTxPowerLevel(false); // necessity to fit in 31 byte advertisement
    // dataBuilder.setManufacturerData(0, advertisingBytes);
    dataBuilder.addServiceUuid(new ParcelUuid(UUID.fromString(Constants.CHAT_SERVICE_UUID)));
    // dataBuilder.setServiceData(pUUID, new byte[]{});

    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    settingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
    settingsBuilder.setConnectable(true);

    bluetoothLeAdvertiser.startAdvertising(
        settingsBuilder.build(), dataBuilder.build(), mAdvertiseCallback);
    final Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(
        new Runnable() {
          @Override
          public void run() {
            bluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
          }
        },
        ADVERTISE_TIMEOUT);
  }
Exemple #2
0
  public void startAdvertising() {
    System.out.println("Start advertising");

    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.BLUETOOTH_ADMIN)
        != PackageManager.PERMISSION_GRANTED) {
      System.out.println(" doesn't have permission");

    } else {
      System.out.println(" has permission");
      advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
      AdvertiseSettings.Builder settings = new AdvertiseSettings.Builder();
      settings.setConnectable(true);
      settings.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
      settings.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
      settings.setTimeout(120000);
      AdvertiseData.Builder data = new AdvertiseData.Builder();
      data.addServiceUuid(new ParcelUuid(UUID.fromString(B4UUID)));
      data.addServiceData(
          new ParcelUuid(UUID.fromString(B4UUID)), "a".getBytes(Charset.forName("UTF-8")));
      data.setIncludeDeviceName(true);
      advertiseCallback = new AdvertiseBack();
      advertiser.startAdvertising(settings.build(), data.build(), advertiseCallback);
      isAdvertising = true;
    }
  }
 protected void setAdvertiseData() {
   AdvertiseData.Builder mBuilder = new AdvertiseData.Builder();
   ByteBuffer mManufacturerData = ByteBuffer.allocate(24);
   byte[] uuid = getIdAsByte(UUID.fromString("4d87fc72-7adc-11e5-8bcf-feff819cdc9f"));
   mManufacturerData.put(0, (byte) 0xBE); // Beacon Identifier
   mManufacturerData.put(1, (byte) 0xAC); // Beacon Identifier
   for (int i = 2; i <= 17; i++) {
     mManufacturerData.put(i, uuid[i - 2]); // adding the UUID
   }
   mManufacturerData.put(18, (byte) 0x00); // first byte of Major
   mManufacturerData.put(19, (byte) 0x09); // second byte of Major
   mManufacturerData.put(20, (byte) 0x00); // first minor
   mManufacturerData.put(21, (byte) 0x06); // second minor
   mManufacturerData.put(22, (byte) 0xB5); // txPower
   mBuilder.addManufacturerData(224, mManufacturerData.array()); // using google's company ID
   mAdvertiseData = mBuilder.build();
 }