I've been working on creating a Nativescript application that can send Bluetooth low energy advertisements. Since there are no existing Nativescript plugins for this functionality, I decided to develop a Java library (with plans to add a Swift library later) that should enable advertising.
After successfully creating the plugin and integrating the Java library with Typescript, I encountered an issue where even though the startAdvertising() method was being called, the advertisements were not actually being sent (they weren't appearing on my BLE Sniffer).
Due to the fact that the advertising process is handled in the Java code, I'm struggling to find a way to debug it effectively. For instance, I can't display a success/fail message using Toast outside of an activity, or print messages to the console.
If anyone has any insights into why the advertisements might not be sending, or suggestions on how to debug the Java code more thoroughly, I would greatly appreciate it.
Ble Advertiser Library
//imports...
class Advertiser {
public String startAdvertising(int manufacturerId, byte[] payload, int duration){
//Acquire instance of the BluetoothLeAdvertiser
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothLeAdvertiser leAdvertiser = btAdapter.getBluetoothLeAdvertiser();
AdvertiseSettings settings =
new AdvertiseSettings.Builder()
.setTimeout(duration)
.setConnectable(false)
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.build();
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(false)
.setIncludeTxPowerLevel(false)
.addManufacturerData(manufacturerId, payload)
.build();
AdvertiseCallback callback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
}
};
leAdvertiser.startAdvertising(settings, data, callback);
return "Method finished.";
}
}
jsIntegrator.js
module.exports = {
startAdvertising: function(manufacturerId, payload, timeout=5000) {
var advertiser = new com.nslibs.moble.Advertiser();
console.log("Result of startAdvertising: " + advertiser.startAdvertising(
manufacturerId, payload, timeout));
//Prints Result of startAdvertising: MethodFinished
}
}
tsIntegrator.ts
export function startAdvertising(
manufacturerId: number, toSend: number[], timeout?: number);
mainClass.ts
import * as Ble from "my-ble-plugin";
export class Main{
foo(){
Ble.startAdvertising(2, [4,6,8]);
}
}