android - Xamarin.Andorid 的 Ble Api 示例
问题描述
我正在尝试使用 Ble 客户端https://developers.google.com/fit/android/ble-sensors将Polar H10连接到我的 xamarin android 应用程序 我已经在网上搜索了 xamarin android 的任何示例但徒劳无功。有人知道吗?
我通过在片段中单击按钮来初始化 API 客户端;因此,当单击 ScanButton 时,我会调用 ScanButton_Click 函数并初始化已成功连接的 Google Api 客户端。
当我开始扫描 ble 设备时,我不确定如何获得结果。现在,我相信我只是打印在控制台中看起来不像列表但在执行com.google.android.gms.internal.zzbsz@8bb6de0
该行时看起来像“”的列表Console.WriteLine(list);
。
我的 GoogleAPI 客户端代码:
private void ScanButton_Click(object sender, EventArgs e)
{
InitializeGoogleAPIClient();
mClient.Connect();
}
private void InitializeGoogleAPIClient()
{
var clientConnectionCallback = new ClientConnectionCallback();
mClient = new GoogleApiClient.Builder(mActivity)
.AddApi(FitnessClass.BLE_API)
.AddApi(FitnessClass.SENSORS_API)
.AddApi(FitnessClass.RECORDING_API)
.AddScope(new Scope(Scopes.FitnessActivityRead))
.AddScope(new Scope(Scopes.FitnessBodyReadWrite))
.AddScope(new Scope(Scopes.FitnessLocationReadWrite))
.AddConnectionCallbacks(clientConnectionCallback)
.AddOnConnectionFailedListener((ConnectionResult result) => {
Console.WriteLine("Connection failed. Cause: " + result);
if (!result.HasResolution)
{
// Show the localized error dialog
GooglePlayServicesUtil.GetErrorDialog(result.ErrorCode, mActivity, 0).Show();
return;
}
if (!authInProgress)
{
try
{
Console.WriteLine("Attempting to resolve failed connection");
authInProgress = true;
result.StartResolutionForResult(mActivity, REQUEST_OAUTH);
}
catch (IntentSender.SendIntentException e)
{
Console.WriteLine("Exception while starting resolution activity", e);
}
}
}).Build();
//OnConnected? start calling Fit APIs
clientConnectionCallback.OnConnectedImpl = () => InvokeBLEApi();
}
private async void InvokeBLEApi()
{
var bleScanCallback = new BleConnectionCallBack(mClient);
bleScanCallback.OnDevice = (device) => ClaimBle(device);
StartBleScanRequest request = new StartBleScanRequest.Builder()
.SetDataTypes(Android.Gms.Fitness.Data.DataType.TypeHeartRateBpm)
.SetBleScanCallback(bleScanCallback)
.Build();
PendingResult pendingResult = FitnessClass.BleApi.StartBleScan(mClient, request);
}
private void ClaimBle(BleDevice device)
{
PendingResult pending_result = FitnessClass.BleApi.ClaimBleDevice(mClient, device);
PendingResult list = FitnessClass.BleApi.ListClaimedBleDevices(mClient);
Console.WriteLine(list);
}
我在上面使用的类:
class BleConnectionCallBack : BleScanCallback
{
private GoogleApiClient mClient;
private BleDevice mdevice;
public Action<BleDevice> OnDevice { get; set; }
public BleConnectionCallBack(GoogleApiClient Client)
{
mClient = Client;
}
public override void OnDeviceFound(BleDevice device)
{
var hr_sensor = device.Name;
OnDevice(device);
//PendingResult pendingResult = FitnessClass.BleApi.ClaimBleDevice(mClient, device);
}
public override void OnScanStopped()
{
}
}
class ClientConnectionCallback : Java.Lang.Object, GoogleApiClient.IConnectionCallbacks
{
public Action OnConnectedImpl { get; set; }
public void OnConnected(Bundle connectionHint)
{
//once we get the callback we can start calling the Fit APIs
Console.WriteLine("Connected!!!");
OnConnectedImpl();
}
public void OnConnectionSuspended(int cause)
{
if (cause == GoogleApiClient.ConnectionCallbacksConsts.CauseNetworkLost)
{
Console.WriteLine("Connection lost. Cause: Network Lost.");
}
else if (cause == GoogleApiClient.ConnectionCallbacksConsts.CauseServiceDisconnected)
{
Console.WriteLine("Connection lost. Reason: Service Disconnected");
}
}
}
解决方案
我在RxAndroidBle之前使用过这个库,它有代码示例和所有东西。它使用 RxJava2,所以它有点学习曲线,但很好用。
这是他们使用的代码示例:
Disposable scanSubscription = rxBleClient.scanBleDevices(
new ScanSettings.Builder()
// .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
// .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
.build()
// add filters if needed
)
.subscribe(
scanResult -> {
// Process scan result here.
},
throwable -> {
// Handle an error here.
}
);
// When done, just dispose.
scanSubscription.dispose();
推荐阅读
- javascript - 使用 ES6 创建 HTML 元素
- angular - 角 httpClientModule
- java - 在随机位置具有指定数字的 Arraylist
- javascript - 带 python 和烧瓶的装载机
- integer - 在没有双精度的情况下有效地确定一个点是在三角形内部还是在边缘上
- haskell - 更适合阅读 Haskell 中的科学记数法
- python - 如何从字典转换列表
- html - 缩小浏览器时在部分内容上显示响应式页脚
- c - 我如何使用指针在这个数组(函数)内部移动而不是数组订阅
- ruby-on-rails - 如何在 Rails 中每隔几秒运行 10 分钟?