[英]Xamarin Wifi and Bluetooth Companion Pairing
Edit: Moved originally Answer out of the Questions section and into the Answers section so this can be more easily understood.编辑:将最初的答案从“问题”部分移到“答案”部分,以便更容易理解。
I had some issues trying to implement android companion device pairing.我在尝试实现 android 配套设备配对时遇到了一些问题。 I am still having issues with WiFi pairing/bonding.
我仍然遇到 WiFi 配对/绑定问题。 I could not find any real information on this (I assume it is not just a P2P connection).
我找不到任何关于此的真实信息(我假设它不仅仅是一个 P2P 连接)。 Source documents are shown below.
源文件如下所示。
Link to Java Pairing: Android companion device pairing链接Java配对: Android配套设备配对
Link to Microsoft Documentation: Microsoft's documentation微软文档链接: 微软的文档
The Answer I came up is below but is still missing the WiFi component for bonding.我提出的答案在下面,但仍然缺少用于绑定的 WiFi 组件。
I could not really find documentation on how to use the C# Android companion device pairing.我真的找不到关于如何使用 C# Android 配套设备配对的文档。 I wanted to share how you can implement part of it and not face some of the frustration I have faced trying to track down and piece together how the system should work.
我想分享如何实现它的一部分,而不是面对我在试图追踪和拼凑系统应该如何工作时所面临的一些挫折。 Please if you see better ways to accomplish any of these tasks or if you know how to complete the WiFi bond please post the answer.
如果您看到更好的方法来完成任何这些任务,或者如果您知道如何完成 WiFi 绑定,请发布答案。
1)First set up your Manifest (permissions are set in properties of the android project if using visual studio and the uses-feature I think needs to be typed in). 1)首先设置您的清单(如果使用 visual studio 和我认为需要输入的使用功能,则在 android 项目的属性中设置权限)。 I probably have more on this list than needed.
我可能在这个列表上有比需要更多的东西。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
<uses-permission android:name="android.permission.CALL_COMPANION_APP" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<uses-feature android:name="android.software.companion_device_setup" />
Note: I have no idea if these are the right Activity tags.注意:我不知道这些是否是正确的活动标签。 I really could not find anything on the ones to use but these do work.
我真的找不到任何可以使用的东西,但这些确实有效。
{
public class CompanionAssociationRequest : CompanionDeviceManager.Callback, IAssociationRequest
{
#region variables
public static int SELECT_DEVICE_REQUEST_CODE = 0;
AssociationRequest _request { get; set; }
AssociationRequest.Builder _requestBuilder { get; set; }
#region Bluetooth
BluetoothDeviceFilter _filter { get; set; }
BluetoothDeviceFilter.Builder _filterBuilder { get; set; }
#endregion
#region Wifi
//WifiDeviceFilter _filter { get; set; }
//WifiDeviceFilter.Builder _filterBuilder { get; set; }
#endregion
CompanionDeviceManager _companionDeviceManager { get; set; }
CompanionDeviceManager.Callback _callback { get; set; }
Context _context = Android.App.Application.Context;
#endregion
#region Assocaiation Request
public async Task MakeRequest()
{
try
{
await HasNotificationAccess();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
try
{
_companionDeviceManager = (CompanionDeviceManager)Forms.Context.GetSystemService(Context.CompanionDeviceService);
await Request();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
try
{
Console.WriteLine(_request.ToString());
_companionDeviceManager.Associate(_request, CompanionDeviceManagerCallback().Result, null);
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
}
#endregion
#region Filter and Request building
private async Task Request()
{
try
{
await RequestBuilder();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
try
{
_request = _requestBuilder.Build();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
}
private async Task RequestBuilder()
{
try
{
await Filter();
_requestBuilder = new AssociationRequest.Builder();
_requestBuilder.AddDeviceFilter(_filter);
_requestBuilder.SetSingleDevice(false);
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
}
private async Task FilterBuilder()
{
try
{
#region Bluetooth
_filterBuilder = new BluetoothDeviceFilter.Builder();
#endregion
#region Wifi
//_filterBuilder = new WifiDeviceFilter.Builder();
//_filterBuilder.SetNamePattern(Java.Util.Regex.Pattern.Compile(""));//I dont know if this works
//_filterBuilder.SetBssid();
#endregion
_filter = _filterBuilder.Build();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
}
private async Task Filter()
{
try
{
await FilterBuilder();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
}
#endregion
#region callback and overrides
private async Task<CompanionDeviceManager.Callback> CompanionDeviceManagerCallback()
{
try
{
_callback = DependencyService.Get<CompanionDeviceManager.Callback>();
}
catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
return _callback;
}
public override void OnDeviceFound(IntentSender chooserLauncher)
{
try
{
DeviceSelectionRequest(chooserLauncher);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
public override void OnFailure(Java.Lang.ICharSequence error)
{
try
{
//add notification here
Console.WriteLine("Devices not found");
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
private void DeviceSelectionRequest(IntentSender chooserLauncher)
{
try
{
#region activity result
ActivityFlags flagsMask = ActivityFlags.BroughtToFront;
ActivityFlags flagsValues = ActivityFlags.NewTask;
int extraFlags = 0;
Intent fillInIntent = null;
((Activity)Forms.Context).StartIntentSenderForResult(chooserLauncher, SELECT_DEVICE_REQUEST_CODE, fillInIntent, flagsMask, flagsValues, extraFlags);
#endregion
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
#endregion
#region notification access
public async Task HasNotificationAccess()
{
bool hasAccess = NotificationManagerCompat.From(_context).AreNotificationsEnabled();
if (!hasAccess)
{
await AskForNotificationAccess();
}
}
public async Task AskForNotificationAccess()
{
Console.WriteLine("notificaitons do not have access");
}
#endregion
}
}
[assembly: Xamarin.Forms.Dependency(typeof(Foo.Bar.Droid.SomeFileName.CompanionAssociationRequest))]
Note: I don't know JAVA so hopefully this is a good approximation of what it is suppose to be.注意:我不知道 JAVA 所以希望这是一个很好的近似值。
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
try
{
if (resultCode != Result.Ok)
{
return;
}
if (requestCode == CompanionAssociationRequest.SELECT_DEVICE_REQUEST_CODE && data != null)
{
#region Bluetooth
BluetoothDevice deviceToPair = (BluetoothDevice)data.GetParcelableExtra(CompanionDeviceManager.ExtraDevice);
#endregion
#region BluetoothLE
//Android.Bluetooth.LE.ScanResult deviceToPair = (Android.Bluetooth.LE.ScanResult)data.GetParcelableExtra(CompanionDeviceManager.ExtraDevice);
#endregion
#region Wifi
//Android.Net.Wifi.ScanResult deviceToPair = (Android.Net.Wifi.ScanResult)data.GetParcelableExtra(CompanionDeviceManager.ExtraDevice);
#endregion
if (deviceToPair != null)
{
#region Bluetooth
deviceToPair.CreateBond();
#endregion
#region BluetoothLE
//deviceToPair.Device.CreateBond();
#endregion
#region Wifi
//???
//Have been trying a few things but could not figure it out.
#endregion
//do more stuff
}
}
else
{
base.OnActivityResult(requestCode, resultCode, data);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}}
I have not really tried the BluetoothLE but I think it should work.我还没有真正尝试过 BluetoothLE,但我认为它应该可以工作。 Hopefully this helps someone banging their head on their desk and if anyone has any improvements I would appreciate input.
希望这可以帮助那些用头敲桌子的人,如果有人有任何改进,我将不胜感激。
Link to Java Pairing: Android companion device pairing链接Java配对: Android配套设备配对
Link to Microsoft Documentation: Microsoft's documentation微软文档链接: 微软的文档
These are really some of the only docs I could find and stumbled around to find some of the other ways to get the system to run.这些确实是我能找到的仅有的一些文档,并且偶然发现了一些让系统运行的其他方法。 This is almost an exact translation from the Java Pairing example but it still took me quite a while to figure out how to format and figure out how it is suppose to work together.
这几乎是 Java 配对示例的精确翻译,但我仍然花了很长时间才弄清楚如何格式化并弄清楚它应该如何协同工作。 I have not been working on the filters yet, hopefully they will be easier to setup.
我还没有研究过滤器,希望它们能更容易设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.