【问题标题】:Turning Bluetooth Tethering On in Xamarin.Android在 Xamarin.Android 中打开蓝牙网络共享
【发布时间】:2018-05-17 09:27:51
【问题描述】:

我目前正在尝试向我的应用程序添加一些蓝牙功能。我希望能够打开或关闭蓝牙网络共享,以及检查其状态。

我在 StackOverflow 上找到了 Java 代码:How to check Bluetooth tethering status programmatically in Android

我已经把它翻译成C#了,但我似乎无法得到任何结果。

无论网络共享设置如何,它总是以“Tethering:false”显示 toast,并且 setBluetoothTethering 不会改变任何内容。

知道我错过了什么吗?

这是我的代码:

[...]
        try
        {
            Class classBluetoothPan = Class.ForName("android.bluetooth.BluetoothPan");

            Method mBTPanConnect = classBluetoothPan.GetDeclaredMethod("connect", Class.FromType(typeof(BluetoothDevice)));

            Constructor BTPanCtor = classBluetoothPan.GetDeclaredConstructor(Class.FromType(typeof(Context)), Class.FromType(typeof(IBluetoothProfileServiceListener)));

            BTPanCtor.Accessible = true;

            Java.Lang.Object BTSrvInstance = BTPanCtor.NewInstance(Activity, new BTPanServiceListener(Activity));

            Method isTetheringOnMethod = classBluetoothPan.GetDeclaredMethod("isTetheringOn", null);

            var isTetheringOn = isTetheringOnMethod.Invoke(BTSrvInstance);

            Toast.MakeText(Activity, "Tethering:" + isTetheringOn, ToastLength.Short).Show();

            Method setBluetoothTetheringMethod = classBluetoothPan.GetDeclaredMethod("setBluetoothTethering", new Class[1] { Class.FromType(typeof(bool)) });

            setBluetoothTetheringMethod.Invoke(BTSrvInstance, true);

            // tether = !tether;

        }
        catch (ClassNotFoundException e)
        {
            e.PrintStackTrace();
        }
        catch (Java.Lang.Exception e)
        {
            e.PrintStackTrace();
        }
[...]


public class BTPanServiceListener : Java.Lang.Object, IBluetoothProfileServiceListener
{

    private Activity _activity;

    public BTPanServiceListener(Activity activity)
    {
        _activity = activity;
    }

    public void OnServiceConnected([GeneratedEnum] ProfileType profile, IBluetoothProfile proxy)
    {
        // throw new NotImplementedException();
    }

    public void OnServiceDisconnected([GeneratedEnum] ProfileType profile)
    {
        // throw new NotImplementedException();
    }
}

【问题讨论】:

    标签: xamarin bluetooth xamarin.android tethering


    【解决方案1】:

    我想出了如何通过setBluetoothTethering 启用蓝牙网络共享。

    我为此写了entire blog

    你可以找到最终代码here

    我假设isTetheringOn 的工作方式相同

    【讨论】:

      猜你喜欢
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多