Xamarin Android BLE - BluetoothAdapter.StartLeScan返回false

时间:2015-05-28 16:03:35

标签: c# android bluetooth xamarin bluetooth-lowenergy

参考本指南:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html我尝试运行BluetoothAdapter.StartLeScan(BluetoothAdapter.ILeScanCallback)方法。这是假的,我不知道为什么。

在我的清单中,我为BLUETOOTHBLUETOOTH_ADMIN启用了权限。我通过运行Activity.ApplicationContext.CheckCallingOrSelfPermission("android.permission.BLUETOOTH")Activity.ApplicationContext.CheckCallingOrSelfPermission("android.permission.BLUETOOTH_ADMIN")确认了这一点。这些都返回Android.Content.PM.Permission.GrantedBluetoothAdapter.StartLeScan(BluetoothAdapter.ILeScanCallback)返回false。

可能导致此返回false的原因是什么?

BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;

// activity set in class constructor

var res = activity.ApplicationContext.CheckCallingOrSelfPermission("android.permission.BLUETOOTH");
// returns Android.Content.PM.Permission.Granted
var res2 = activity.ApplicationContext.CheckCallingOrSelfPermission("android.permission.BLUETOOTH_ADMIN");
// returns Android.Content.PM.Permission.Granted

Handler handler = new Handler();

handler.PostDelayed(delegate
{
    adapter.StopLeScan(this);
}, 4000);

var boolerson = adapter.StartLeScan(this); // returns false... why?

2 个答案:

答案 0 :(得分:0)

问题似乎是我在我创建的库中调用了BlutoothAdapter.StartLeScan(BluetoothAdapter.ILeScanCallback)。当我直接从我的应用程序代码中调用它时,它返回true。这似乎不应该,但确实如此。

答案 1 :(得分:0)

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

这些权限是必要的。