package com.SalmanDev.BluetoothInfo; import android.content.Context; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; import android.app.Activity; import android.util.Log; import android.bluetooth.BluetoothAdapter; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.runtime.*; import com.google.appinventor.components.runtime.Form; import com.google.appinventor.components.runtime.EventDispatcher; import com.google.appinventor.components.runtime.OnDestroyListener; import com.google.appinventor.components.runtime.OnResumeListener; import com.google.appinventor.components.runtime.OnStopListener; import com.google.appinventor.components.common.ComponentCategory; @DesignerComponent(version = 1, description = "Created by Salman Developer", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "https://img.icons8.com/color-glass/16/000000/bluetooth.png") @UsesPermissions(permissionNames = "android.permission.BLUETOOTH, android.permission.BLUETOOTH_ADMIN, android.permission.ACCESS_FINE_LOCATION") @SimpleObject(external = true) public class BluetoothInfo extends AndroidNonvisibleComponent implements OnResumeListener, OnStopListener, OnDestroyListener { private ComponentContainer container; private Activity activity; private Form form; private Context context; private boolean listening; private int state, scanMode, connectionState; private String name; public BluetoothInfo(ComponentContainer container) { super(container.$form()); this.container = container; form = container.$form(); form.registerForOnStop(this); form.registerForOnResume(this); form.registerForOnDestroy(this); activity = (Activity) container.$context(); context = (Context) container.$context(); startListeningBluetooth(); } private void startListeningBluetooth() { if (!listening) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); intentFilter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); activity.registerReceiver(bluetoothReceiver, intentFilter); listening = true; } } private void stopListeningBluetooth() { if (listening) { activity.unregisterReceiver(bluetoothReceiver); listening = false; } } public void onResume() { startListeningBluetooth(); } public void onStop() { stopListeningBluetooth(); } public void onDestroy() { stopListeningBluetooth(); } private BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case BluetoothAdapter.ACTION_STATE_CHANGED: state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); break; case BluetoothAdapter.ACTION_SCAN_MODE_CHANGED: scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.ERROR); break; case BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED: name = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); break; case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED: connectionState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.ERROR); break; } Changed(state, name, scanMode, connectionState); } }; @SimpleEvent(description = "") public void Changed(int state, String name, int scanMode, int connectionState) { EventDispatcher.dispatchEvent(this, "Changed", state, name, scanMode, connectionState); } @SimpleFunction(description = "") public String GetAddress() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return bluetoothAdapter.getAddress(); } @SimpleFunction(description = "") public String GetName() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return bluetoothAdapter.getName(); } @SimpleFunction(description = "") public int GetState() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return bluetoothAdapter.getState(); } @SimpleFunction(description = "") public int GetScanMode() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return bluetoothAdapter.getScanMode(); } @SimpleFunction(description = "") public boolean IsAvailable() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); boolean result = (bluetoothAdapter == null) ? false : true ; return result; } @SimpleFunction(description = "") public boolean IsValidAddress(String address) { return BluetoothAdapter.checkBluetoothAddress(address); } @SimpleFunction(description = "") public boolean IsEnabled() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return bluetoothAdapter.isEnabled(); } }