Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): add BertyAdvertise class
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
client/react-native/android/ble/src/main/java/chat/berty/ble/BertyAdvertise.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package chat.berty.ble; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.annotation.TargetApi; | ||
import android.bluetooth.le.AdvertiseCallback; | ||
import android.bluetooth.le.AdvertiseData; | ||
import android.bluetooth.le.AdvertiseSettings; | ||
import android.bluetooth.le.BluetoothLeAdvertiser; | ||
import android.os.Build; | ||
import android.os.ParcelUuid; | ||
import android.util.Log; | ||
|
||
import static chat.berty.ble.BertyConstants.SERVICE_UUID; | ||
|
||
@SuppressLint("LongLogTag") | ||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
public class BertyAdvertise extends AdvertiseCallback { | ||
private static final String TAG = "chat.berty.ble.BertyAdvertise"; | ||
|
||
public static AdvertiseData makeAdvertiseData() { | ||
ParcelUuid pUuid = new ParcelUuid(SERVICE_UUID); | ||
|
||
AdvertiseData.Builder builder = new AdvertiseData.Builder() | ||
.setIncludeDeviceName(true) | ||
.setIncludeTxPowerLevel(false) | ||
.addServiceUuid(pUuid); | ||
|
||
return builder.build(); | ||
} | ||
|
||
public static AdvertiseSettings createAdvSettings(boolean connectible, int timeoutMillis) { | ||
AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder(); | ||
|
||
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) | ||
.setConnectable(connectible) | ||
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) | ||
.setTimeout(timeoutMillis); | ||
|
||
return builder.build(); | ||
} | ||
|
||
public BertyAdvertise() { | ||
super(); | ||
Thread.currentThread().setName("BertyAdvertise"); | ||
} | ||
|
||
/** | ||
* Callback triggered in response to {@link BluetoothLeAdvertiser#startAdvertising} indicating | ||
* that the advertising has been started successfully. | ||
* | ||
* @param settingsInEffect The actual settings used for advertising, which may be different from | ||
* what has been requested. | ||
*/ | ||
@Override | ||
public void onStartSuccess(AdvertiseSettings settingsInEffect) { | ||
Log.e(TAG, "Advertising onStartSuccess: " + settingsInEffect); | ||
super.onStartSuccess(settingsInEffect); | ||
} | ||
|
||
/** | ||
* Callback when advertising could not be started. | ||
* | ||
* @param errorCode Error code (see ADVERTISE_FAILED_* constants) for advertising start | ||
* failures. | ||
*/ | ||
@Override | ||
public void onStartFailure(int errorCode) { | ||
|
||
String errorString; | ||
switch (errorCode) { | ||
case ADVERTISE_FAILED_DATA_TOO_LARGE: errorString = "ADVERTISE_FAILED_DATA_TOO_LARGE"; | ||
break; | ||
|
||
case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS: errorString = "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS"; | ||
break; | ||
|
||
case ADVERTISE_FAILED_ALREADY_STARTED: errorString = "ADVERTISE_FAILED_ALREADY_STARTED"; | ||
break; | ||
|
||
case ADVERTISE_FAILED_INTERNAL_ERROR: errorString = "ADVERTISE_FAILED_INTERNAL_ERROR"; | ||
break; | ||
|
||
case ADVERTISE_FAILED_FEATURE_UNSUPPORTED: errorString = "ADVERTISE_FAILED_FEATURE_UNSUPPORTED"; | ||
break; | ||
|
||
default: errorString = "UNKNOWN ADVERTISE FAILURE"; | ||
break; | ||
} | ||
Log.e(TAG, "Advertising onStartFailure: " + errorString); | ||
super.onStartFailure(errorCode); | ||
} | ||
} |