我正在尝试使用Ble Client将Polar H10连接到我的xamarin android应用 https://developers.google.com/fit/android/ble-sensors 我已经在网上搜索了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");
}
}
}
答案 0 :(得分:0)
我在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();