Skip to content

Commit

Permalink
modified it to detect only headphones
Browse files Browse the repository at this point in the history
  • Loading branch information
SPiercer committed Sep 7, 2022
1 parent 4512a8e commit 47dd3ef
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package flutter.moum.flutter_headset_detector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
Expand All @@ -12,6 +20,8 @@
import android.content.IntentFilter;
import android.os.Build;
import androidx.annotation.NonNull;

import android.util.ArraySet;
import android.util.Log;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
Expand All @@ -36,13 +46,24 @@ public class FlutterHeadsetDetectorPlugin implements FlutterPlugin, MethodCallHa

private final HeadsetEventListener headsetEventListener = new HeadsetEventListener() {
@Override
public void onWiredHeadsetConnect() {
channel.invokeMethod("wired_connected", "true");
public void onWiredHeadsetConnect(String name,int micState) {
final HashMap args = new HashMap<>();
args.put("state", "connected");
args.put("name",name);
args.put("micState",micState==1 ? true : false);
if ((boolean) args.get("micState")){
return;
}
channel.invokeMethod("wired_connected", args);
}

@Override
public void onWiredHeadsetDisconnect() {
channel.invokeMethod("wired_disconnected", "true");
public void onWiredHeadsetDisconnect(String name,int micState) {
final HashMap args = new HashMap<>();
args.put("state", "disconnected");
args.put("name",name);
args.put("micState",micState==1 ? true : false);
channel.invokeMethod("wired_disconnected", args);
}

@Override
Expand Down Expand Up @@ -96,17 +117,63 @@ private int wiredHeadphonesConnectionState() {

for (AudioDeviceInfo deviceInfo : audioDevices) {
int deviceType = deviceInfo.getType();
if (deviceType == AudioDeviceInfo.TYPE_WIRED_HEADPHONES || deviceType == AudioDeviceInfo.TYPE_WIRED_HEADSET
System.out.println("[JAVA: device type] ");
System.out.println(deviceType);
if (deviceType == AudioDeviceInfo.TYPE_WIRED_HEADPHONES /* || deviceType == AudioDeviceInfo.TYPE_WIRED_HEADSET
|| deviceType == AudioDeviceInfo.TYPE_USB_HEADSET
|| deviceType == AudioDeviceInfo.TYPE_USB_DEVICE) {
|| deviceType == AudioDeviceInfo.TYPE_USB_DEVICE */) {
return 1;
}
}
return 0;

}

// public class BluetoothServiceListener implements BluetoothProfile.ServiceListener {
// private String TAG = "bluetoothListener";
// private static final int[] states={
// BluetoothProfile.STATE_CONNECTED};
// @Override
// public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
// List<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
// for (BluetoothDevice device:Devices){
// System.out.println("[BT JAVA: ]" + device.getBluetoothClass().getDeviceClass());
// if(device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO)
// System.out.println("NAME | " + device.getName() + device.getBluetoothClass().getDeviceClass());
// }
// }

// @Override
// public void onServiceDisconnected(int profile) {
// }

// }

private int bluetoothHeadphonesConnectionState() {
BluetoothManager myBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

BluetoothAdapter adapter = myBluetoothManager.getAdapter();
List<BluetoothDevice> connected = myBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);

System.out.println("[Connected Device Size] " + connected.isEmpty() + connected.size());

for (int i = 0; i<connected.size(); i++){
System.out.println("[Connected Device] " + connected.get(i).getName());
}

final Set set = adapter.getBondedDevices();


for (Iterator<BluetoothDevice> it = set.iterator(); it.hasNext(); ) {
BluetoothDevice f = it.next();
final int deviceClass = f.getBluetoothClass().getDeviceClass();

System.out.println("[JAVA Device Class]: "+ deviceClass);
final int conState = adapter.getProfileConnectionState(BluetoothAdapter.STATE_CONNECTED);
System.out.println("connection state headset is "+conState);
final boolean isCar = deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO;
System.out.println("[JAVA Device Class IS CAR?]: " + isCar);
}
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case Intent.ACTION_HEADSET_PLUG:
final int state = intent.getIntExtra("state", -1);

final int micState = intent.getIntExtra("microphone", -1);
final String name = intent.getStringExtra("name");
switch (state) {
case 0:
headsetEventListener.onWiredHeadsetDisconnect();
headsetEventListener.onWiredHeadsetDisconnect(name,micState);
break;
case 1:
headsetEventListener.onWiredHeadsetConnect();
headsetEventListener.onWiredHeadsetConnect(name,micState);
break;
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package flutter.moum.flutter_headset_detector;

public interface HeadsetEventListener {
void onWiredHeadsetConnect();
void onWiredHeadsetConnect(String name,int micState);
void onWirelessHeadsetConnect();

void onWiredHeadsetDisconnect();
void onWiredHeadsetDisconnect(String name,int micState);
void onWirelessHeadsetDisconnect();
}
14 changes: 7 additions & 7 deletions lib/flutter_headset_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class HeadsetDetector {

factory HeadsetDetector() {
if (_instance == null) {
final methodChannel =
const MethodChannel('flutter.moum/flutter_headset_detector');
final methodChannel = const MethodChannel('flutter.moum/flutter_headset_detector');
_instance = HeadsetDetector.private(methodChannel);
}

Expand All @@ -48,12 +47,10 @@ class HeadsetDetector {
//Reads asynchronously the current state of the headset with type [HeadsetState]
Future<Map<HeadsetType, HeadsetState>> get getCurrentState async {
final state = await _channel!.invokeMethod('getCurrentState');

print("[AUX STATE: ]" + state.toString());
return {
HeadsetType.WIRED:
state[0] ? HeadsetState.CONNECTED : HeadsetState.DISCONNECTED,
HeadsetType.WIRELESS:
state[1] ? HeadsetState.CONNECTED : HeadsetState.DISCONNECTED,
HeadsetType.WIRED: state[0] ? HeadsetState.CONNECTED : HeadsetState.DISCONNECTED,
HeadsetType.WIRELESS: state[1] ? HeadsetState.CONNECTED : HeadsetState.DISCONNECTED,
};
}

Expand All @@ -70,6 +67,9 @@ class HeadsetDetector {
return;
}

print("[HEADSET LOG] " + call.method);
print("[HEADSET LOG] " + call.arguments.toString());

switch (call.method) {
case 'wired_connected':
return callback(HeadsetChangedEvent.WIRED_CONNECTED);
Expand Down

0 comments on commit 47dd3ef

Please sign in to comment.