Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift Support. Wireless device detection #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ coverage/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
example/.flutter-plugins-dependencies
2 changes: 2 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="flutter.moum.headset_event">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package flutter.moum.headset_event;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

public class HeadsetBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "log";
private static final String TAG = "HeadsetBroadcastReceiver";

HeadsetEventListener headsetEventListener;

public HeadsetBroadcastReceiver(HeadsetEventListener listener) {
this.headsetEventListener = listener;
}
Expand All @@ -29,15 +31,32 @@ public void onReceive(Context context, Intent intent) {
default:
Log.d(TAG, "I have no idea what the headset state is");
}
} else if (intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
int connectionState = intent.getExtras().getInt(BluetoothAdapter.EXTRA_CONNECTION_STATE);
switch (connectionState) {
case BluetoothAdapter.STATE_CONNECTED:
headsetEventListener.onHeadsetConnect();
break;
case BluetoothAdapter.STATE_DISCONNECTED:
headsetEventListener.onHeadsetDisconnect();
break;
default:
break;
}
} else if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
int connectionState = intent.getExtras().getInt(BluetoothAdapter.EXTRA_STATE);
if (connectionState == BluetoothAdapter.STATE_OFF) {
headsetEventListener.onHeadsetDisconnect();
}
} else {
abortBroadcast();

KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (key.getAction() == KeyEvent.ACTION_UP) {

int keycode = key.getKeyCode();
Log.d(TAG, "onReceive: "+keycode);
switch(keycode) {
Log.d(TAG, "onReceive: " + keycode);
switch (keycode) {
case KeyEvent.KEYCODE_MEDIA_NEXT:
headsetEventListener.onNextButtonPress();
break;
Expand All @@ -49,4 +68,4 @@ public void onReceive(Context context, Intent intent) {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,65 +1,86 @@
package flutter.moum.headset_event;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** HeadsetEventPlugin */
public class HeadsetEventPlugin implements MethodCallHandler{
/**
* HeadsetEventPlugin
*/
public class HeadsetEventPlugin implements FlutterPlugin {

public static MethodChannel headsetEventChannel;
public static int currentState = -1;
private static HeadsetBroadcastReceiver hReceiver;
private static final String TAG = "HeadsetEventPlugin";
public static MethodChannel headsetEventChannel;
public static int currentState = -1;
private static HeadsetBroadcastReceiver hReceiver;
private static final String TAG = "HeadsetEventPlugin";

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
headsetEventChannel = new MethodChannel(registrar.messenger(), "flutter.moum/headset_event");
headsetEventChannel.setMethodCallHandler(new HeadsetEventPlugin());
hReceiver = new HeadsetBroadcastReceiver(headsetEventListener);
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registrar.activeContext().registerReceiver(hReceiver, filter);
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
headsetEventChannel = new MethodChannel(registrar.messenger(), "flutter.moum/headset_event");
headsetEventChannel.setMethodCallHandler(new HeadsetEventPlugin());
hReceiver = new HeadsetBroadcastReceiver(headsetEventListener);
String actionHeadsetPlug = Intent.ACTION_HEADSET_PLUG;
String actionBluetoothConnectionState = BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED;
String actionBluetoothState = BluetoothAdapter.ACTION_STATE_CHANGED;
IntentFilter filter = new IntentFilter();
filter.addAction(actionHeadsetPlug);
filter.addAction(actionBluetoothConnectionState);
filter.addAction(actionBluetoothState);
registrar.activeContext().registerReceiver(hReceiver, filter);

}
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if (call.method.equals("register")) {

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else if(call.method.equals("register")) {
} else if (call.method.equals("getCurrentState")) {
updateHeadsetStatus();
result.success(currentState);
} else {
result.notImplemented();
}
}

} else if(call.method.equals("getCurrentState")) {
result.success(currentState);
} else {
result.notImplemented();
private void updateHeadsetStatus() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean status = (bluetoothAdapter != null && BluetoothProfile.STATE_CONNECTED ==
bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET));
if (status) {
currentState = 1;
}
}
}

static HeadsetEventListener headsetEventListener = new HeadsetEventListener() {
@Override
public void onHeadsetConnect() {
headsetEventChannel.invokeMethod("connect", "true");
}
static HeadsetEventListener headsetEventListener = new HeadsetEventListener() {
@Override
public void onHeadsetConnect() {
headsetEventChannel.invokeMethod("connect", "true");
}

@Override
public void onHeadsetDisconnect() {
headsetEventChannel.invokeMethod("disconnect", "true");
}
@Override
public void onHeadsetDisconnect() {
headsetEventChannel.invokeMethod("disconnect", "true");
}

@Override
public void onNextButtonPress() {
headsetEventChannel.invokeMethod("nextButton", "true");
}
@Override
public void onNextButtonPress() {
headsetEventChannel.invokeMethod("nextButton", "true");
}

@Override
public void onPrevButtonPress() {
headsetEventChannel.invokeMethod("prevButton", "true");
}
};
}
@Override
public void onPrevButtonPress() {
headsetEventChannel.invokeMethod("prevButton", "true");
}
};
}
6 changes: 5 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:name="${applicationName}"
android:label="headset_event_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down Expand Up @@ -36,5 +36,9 @@
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package flutter.moum.headset_event_example;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}
1 change: 1 addition & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/headset_event/ios"

SPEC CHECKSUMS:
Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
headset_event: 7e97247360e17f7a182fcd35a65ebfcbf9a8fac9

PODFILE CHECKSUM: ebd43b443038e611b86ede96e613bd6033c49497

COCOAPODS: 1.7.4
COCOAPODS: 1.8.4
14 changes: 1 addition & 13 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
7225D5D1F3F201D9E23C8B47 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 23F5D1C3FC6231DA3DC5E5BE /* Pods_Runner.framework */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; };
9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Debug.xcconfig */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
Expand All @@ -28,8 +24,6 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */,
9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -42,14 +36,12 @@
23F5D1C3FC6231DA3DC5E5BE /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
358F5E74F65F84C78718142D /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
602F24C3F95A7E584885DFE7 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -63,8 +55,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */,
3B80C3941E831B6300D905FE /* App.framework in Frameworks */,
7225D5D1F3F201D9E23C8B47 /* Pods_Runner.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -83,9 +73,7 @@
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
3B80C3931E831B6300D905FE /* App.framework */,
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
9740EEBA1CF902C7004384FC /* Flutter.framework */,
9740EEB21CF90195004384FC /* Debug.xcconfig */,
7AFA3C8E1D35360C0083082E /* Release.xcconfig */,
9740EEB31CF90195004384FC /* Generated.xcconfig */,
Expand Down Expand Up @@ -232,7 +220,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" thin";
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin";
};
488B5AFEBFD8DB9D63FD3459 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
Expand Down
Loading