简体   繁体   English

在Android上连接Xamarin的配对蓝牙设备

[英]Connect to paired bluetooth device from Xamarin on Android

We need our application to be able to connect to a paired bluetooth device automatically when an application starts via A2DP or Hands Free Profile. 当应用程序通过A2DP或Hands Free Profile启动时,我们需要我们的应用程序能够自动连接到配对的蓝牙设备。

We are working in Xamarin (monodroid), for Android platform. 我们正在使用Xamarin(monodroid),用于Android平台。

I've found this stackoverflow question: Programmatically connect to paired Bluetooth device 我发现了这个stackoverflow问题:以编程方式连接到配对的蓝牙设备

But it relates to native ways of achieving this (see answer by kcoppock). 但它与本土方法有关(见kcoppock的回答)。 I'd like to know if there is a way to achieve this via Xamarin. 我想知道是否有办法通过Xamarin实现这一目标。 We can connect to SPP endpoint since it's an RFCOMM based connection, but we need that and the audio connection, so we are loking for a way to connect to A2DP. 我们可以连接到SPP端点,因为它是基于RFCOMM的连接,但我们需要它和音频连接,所以我们想要一种连接到A2DP的方法。

Update 1: 更新1:

We have tried to connect using CreateInsecureRfcommSocketToServiceRecord method like this: 我们尝试使用CreateInsecureRfcommSocketToServiceRecord方法进行连接,如下所示:

mmSocket = device.CreateInsecureRfcommSocketToServiceRecord(0000110A-0000-1000-8000-00805F9B34FB); mmSocket.Connect();

Upon a call to Connect, we are getting an error: 在致电Connect时,我们收到错误消息:

read failed, socket might closed or timeout, read ret: -1

Stack trace begins with: 堆栈跟踪始于:

Java.IO.IOException at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00062] in /Users/buil…

Update 2: 更新2:

By the way, whene we try to connect via the native java test app using the approach by kcoppock , the connection code seems to work without an error, although the device isn't being connected as an A2DP headset. 顺便说一句,当我们尝试使用kcoppock的方法通过本机java测试应用程序连接时,连接代码似乎没有错误,但设备没有作为A2DP耳机连接。

The only programming way we have seen to be able to do it was this Google Play app , which proves that it is possible. 我们看到能够做到的唯一编程方式Google Play应用 ,它证明了这是可能的。

Remember that Xamarin binds to native api so don't worry that something "relates to native ways" ;) Based on the anwser You referenced I wrote and tested the code below. 请记住,Xamarin绑定到本机api所以不要担心某些“与本机方式有关”;)基于你引用的anwser我编写并测试了下面的代码。 I hope it will help You. 我希望它会对你有所帮助。

class btListener : Java.Lang.Object, IBluetoothProfileServiceListener
{
    public void OnServiceConnected([GeneratedEnum] ProfileType profile, IBluetoothProfile proxy)
    {
        String deviceName = "JABRA WAVE+";

        BluetoothDevice result = null;

        var devices = BluetoothAdapter.DefaultAdapter.BondedDevices;
        if (devices != null)
        {
            foreach (BluetoothDevice device in devices)
            {
                if (deviceName == device.Name)
                {
                    result = device;
                    break;
                }
            }
        }
        var connect = Java.Lang.Class.FromType(typeof(BluetoothA2dp)).GetDeclaredMethod("connect", Java.Lang.Class.FromType(typeof(BluetoothDevice)));
        connect.Invoke((Java.Lang.Object)proxy, result);
    }

    public void OnServiceDisconnected([GeneratedEnum] ProfileType profile)
    {
    }
}

Following code in eg OnCreate function: 以下代码在OnCreate函数中:

btListener btReceiver = new btListener();
BluetoothAdapter.DefaultAdapter.GetProfileProxy(this, btReceiver, ProfileType.A2dp);

Just looked at the date.. but I'm posting answer anyway - maybe it's still going to help somebody 只是看了一下日期..但我还是张贴了答案 - 也许它仍然会帮助某人

You may check this blog post . 你可以查看这篇博文 adapter.BondedDevices property in this link will return a list of paired devices. 此链接中的adapter.BondedDevices属性将返回已配对设备的列表。

在Xamarin示例中有一个本机java示例程序及其模拟程序: http//docs.xamarin.com/samples/BluetoothChat/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM