How to create a core nativescript plugin to access native android bluetooth api's in classic way

Viewed 13

I'm trying to create a plugin to use the classic native android bluetooth functionalities. I am creating core nativescript plugin to access native android bluetooth api's in classic way. I tries to use the available plugin i.e 'nativescript-community/ble' nativescript plugin. This plugin is not supporting classic way of identifying the available bluetooth devices and not getting paired.

I followed the https://v6.docs.nativescript.org/ guide to create a plugin by cloning the plugin template structure and called some of the classic android native api's but it is not working . can you please help me in how to create plugin to call native android bluetooth api's also I want to
know how to build and use the .aar file.

Only isEnabled() is working fine with the BluetoothAdapter.ACTION_REQUEST_ENABLE intent parameter. Others like

  1. discovering device through registerBroadcastReceiver call back methods with ACTION_FOUND
  2. getBondedDevices() are not working as expected. I have attached some of the native android methods called in the plugin typescript/javasript.

I have introduced these methods in .android.ts


    public constructor(input) {
    super();
    this.createNativeView();
  }

  /**
   * Creates new native controls.
   */
  public createNativeView(): Object {
    
    const context = android.content.Context;
    startBluetooth(context);
    this._layout = new android.widget.LinearLayout(app.android.context);
    return this._layout;
    
  }
export function startBluetooth(context:any) {
 
  /*const BTMgr = context.getSystemService(android.bluetooth.BluetoothManager.class);
  
  const adaptermgr = BTMgr.getAdapter();
  if(adaptermgr === null) {
    alert("device doesn't support bluetooth" );

  }
  else {
    alert("device supports bluetooth");
  }*/
  const adapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
  
    if(!(adapter.isEnabled())) {
         android.startActivityForResult(
            android.content.Intent(adapter.ACTION_REQUEST_ENABLE),adapter.REQUEST_ENABLE_BT);
     }
     /* android.registerBroadcastReceiver(
        android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED,
        (context, intent) => {
            const state = intent.getIntExtra(android.bluetooth.BluetoothAdapter.EXTRA_STATE, android.bluetooth.BluetoothAdapter.ERROR);
      if(android.bluetooth.BluetoothAdapter.STATE_ON) {
        const BTMgr = context.getSystemService(context.BLUETOOTH_SERVICE);
        const adapter = BTMgr.getAdapter();
      //    const adapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
      //    let  bluetoothdevice = android.bluetooth.BluetoothDevice;
      //    let BTPairSet = new Set<android.bluetooth.BluetoothDevice>();
        let BTPairSet = adapter.getBondedDevices();
      //    alert(JSON.parse(BTPairSet));
        alert(JSON.stringify(BTPairSet));
      }
  }); */
  android.onActivityResult()
//     GetPairedDevices();
}

0 Answers
Related