如何检测机器人的蓝牙耳机或有线耳机 [英] How to detect the BT headset or wired headset in android

查看:369
本文介绍了如何检测机器人的蓝牙耳机或有线耳机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想检测的Andr​​oid的蓝牙耳机或有线耳机。
我指 http://developer.android.com/guide/topics/connectivity/bluetooth html的 code从这个链接。
他们提到,只有3.0及以上版本的Andr​​oid可以支持BluetoothHeadset和BluetoothProfile类。这有利于检测蓝牙耳机。现在,我想在2.2(Froyo的)相同的功能。所以请任何人能告诉我该怎么做?

In my application i want to detect the BT headset or wired headset in android. I refer http://developer.android.com/guide/topics/connectivity/bluetooth.html code from this link. They mention that only 3.0 and above version of android can support the BluetoothHeadset and BluetoothProfile class. Which is helpful to detect the BT headset. Now i want the same functionality in 2.2(froyo).So please any one can tell me how to do the same?

在此先感谢。

推荐答案

您可以通过反射使用2.2 3.0 API的。但是,这可能会导致您的应用程序在某些情况下无法正常工作,表现未predictably对于某些设备等。

You can use the 3.0 API's in 2.2 via Reflection. But this might cause your app to work incorrectly in certain cases, behave unpredictably for some devices etc.

如果你还是想尝试,在这里看到:

If you still wanna try, see here:

<一个href=\"http://stackoverflow.com/questions/7739644/how-to-use-bluetoothheadset-class-via-reflection\">How通过反射使用BluetoothHeadset类

How to use BluetoothHeadset class via reflection

的简要说明:

名称反射用于描述code,其能够检查其他code在相同的系统(或自身)。

The name reflection is used to describe code which is able to inspect other code in the same system (or itself).

举例来说,假设你有Java中的未知类型的对象,你想调用就可以了DoSomething的方法(如果存在)。 Java的静态类型系统是不是真的设计为支持这一点,除非对象符合已知的接口,但使用反射,你的code可以看看对象,看看它有一个被称为DoSomething的方法,然后,称之为如果你想。

For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething', and then, call it if you want to.

所以,给你这个在Java中的code为例(假设有问题的对象是foo):

So, to give you a code example of this in Java (imagine the object in question is foo) :

Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);

在上面的例子中,作者试图检测某些蓝牙类和方法是否在顺序存在的检测工作。如果它们存在,则它们被使用。如果没有,那么你的code必须通知用户或适合您的应用程序的一些其它合适的机制处理这种情况。

In the example above, the author is trying to detect whether certain bluetooth classes and methods exist in order for the detection to work. If they exist, then they are used. If not, then your code must handle the situation by notifying the user or by some other appropriate mechanism suitable for your app.

这篇关于如何检测机器人的蓝牙耳机或有线耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆