Flutter plugin to detect whether any headphones (wired or bluetooth) are connected to device. It supports only Android at the moment. Work examples:
Detect current connection state
To detect are there any connected headphones use following:
final HeadphonesDetector headphonesDetector = HeadphonesDetector();
HeadphonesConnectionState currentState = await headphonesDetector.headphonesState();
Use HeadphonesType to specify detector behavior:
final HeadphonesDetector headphonesDetector = HeadphonesDetector();
HeadphonesConnectionState currentWiredConnectionState = await headphonesDetector.headphonesState(headphonesType: HeadphonesType.wired);
HeadphonesConnectionState currentBluetoothConnectionState = await headphonesDetector.headphonesState(headphonesType: HeadphonesType.bluetooth);
Using Streams
To receive changes in connection state for wired headphones you can use following:
final HeadphonesDetector headphonesDetector = HeadphonesDetector();
headphonesDetector.wiredHeadphonesConnectionState.listen(print);
You can receive changes for Bluetooth headphones as well:
final HeadphonesDetector headphonesDetector = HeadphonesDetector();
headphonesDetector.bluetoothHeadphonesConnectionState.listen(print);
Current issues list is here.
Found a bug? Open the issue.