Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

detect bluetooth headphones android programmatically

public void onCreate() {
    ...
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter);
}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           ... //Device found
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
           ... //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
           ... //Done searching
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           ... //Device is about to disconnect
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
           ... //Device has disconnected
        }
    }
};
Comment

detect bluetooth headphones android programmatically

fun isBluetoothHeadsetConnected(): Boolean {
    val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    return (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled
        && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED)
}
Comment

PREVIOUS NEXT
Code Example
Java :: Which of the below is a correct identifier for a method name in Java * 2 points 0start #stop 0_start *start# 
Java :: java get wrapper class for primitive 
Java :: what is java.io example 
Java :: how to make bidirectional fromated binding 
Java :: zoom sdk not initialized 
Java :: no main attribute in java android 
Java :: add external JARs to java vscode 
Java :: for loop optimized java 
Java :: guava-18.0.jar 
Java :: how to send int value from one actvi to another in android 
Java :: assigning value with for each 
Java :: cfgbcfdxv 
Java :: how to implement seekbar for music in java 
Java :: java mongodb document get 
Java :: interview questions on ds and algo 
Java :: success listener with Glide Android java 
Java :: android studio setbackgroundcolor drawable 
Java :: jsp multipart/form-data submition 
Java :: import classes from another project java 
Java :: lcm 
Java :: java lib reference 
Java :: has 8 digit in number 
Java :: Java Stack class search() method 
Java :: Java Change values of variables 
Java :: calculate values of array 
Java :: how to split each string of a line 
Java :: sealed interface java codegrepper 
Java :: how to read returned arraylist from another class method 
Java :: springboot getting body value 
Java :: bebra 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =