我想在Android 2.0中使用Bluetoothheadset类的方法.这是我的代码:
Class bluetoothheadsetClass = Class.forname("androID.bluetooth.Bluetoothheadset"); Constructor constructor = bluetoothheadsetClass.getConstructors()[0]; Object bluetoothheadset = constructor.newInstance(this, null); Method getState = bluetoothheadsetClass.getmethod("getState", null); Object retVal = getState.invoke(bluetoothheadset, null);
执行此代码时,我总是收到日志消息:
10-12 13:29:48.360: WARN/Bluetoothheadset(3379): Proxy not attached to
service
我还试着在调用我的方法之前等待几秒钟,但它仍然是一样的.
感谢帮助!!!
解决方法:
这是我用反射做的方式.我还没有检查这段代码现在是否有效,所以它可能不是:
class Bluetoothheadset { private static final String TAG = Tags.getTag(Bluetoothheadset.class); private static final String BLUetoOTH_headSET_CLASS_name = "androID.bluetooth.IBluetoothheadset"; private static final String BLUetoOTH_headSET_STUB_CLASS_name = BLUetoOTH_headSET_CLASS_name+"$Stub"; public static final String ACTION_BLUetoOTH_headSET_SERVICE = BLUetoOTH_headSET_CLASS_name; public static final String ACTION_STATE_CHANGED = "androID.bluetooth.headset.action.STATE_CHANGED"; public static final String ACTION_AUdio_STATE_CHANGED = "androID.bluetooth.headset.action.AUdio_STATE_CHANGED"; public static final String EXTRA_AUdio_STATE = "androID.bluetooth.headset.extra.AUdio_STATE"; /** A SCO audio channel is not established */ public static final int AUdio_STATE_disCONNECTED = 0; /** A SCO audio channel is established */ public static final int AUdio_STATE_CONNECTED = 1; private static final String AS_INTERFACE_METHOD_name = "asInterface"; private static final String IS_CONNECTED_METHOD_name = "isConnected"; final Object m_service; private Bluetoothheadset(Object service) { m_service = service; } static public Bluetoothheadset getBluetoothheadset(IBinder service) { if (service == null) { return null; } Object proxy = null; try { Class<?> clazz = Class.forname(BLUetoOTH_headSET_STUB_CLASS_name); Method asInterfaceMethod = clazz.getmethod(AS_INTERFACE_METHOD_name, IBinder.class); proxy = asInterfaceMethod.invoke(null, service); } catch(ClassNotFoundException ex) { String msg = String.format("Was not able to find %s class.", BLUetoOTH_headSET_STUB_CLASS_name); Log.e(TAG, msg, ex); } catch(NoSuchMethodException ex) { String msg = String.format("Was not able to find %s method in %s class.", AS_INTERFACE_METHOD_name, BLUetoOTH_headSET_STUB_CLASS_name); Log.e(TAG, msg, ex); } catch(InvocationTargetException ex) { String msg = String.format("Was not able to invoke %s method in %s class.", AS_INTERFACE_METHOD_name, BLUetoOTH_headSET_STUB_CLASS_name); Log.e(TAG, msg, ex); } catch(illegalaccessexception ex) { String msg = String.format("Illegal access while invoking %s method in %s class.", AS_INTERFACE_METHOD_name, BLUetoOTH_headSET_STUB_CLASS_name); Log.e(TAG, msg, ex); } if(proxy == null) return null; return new Bluetoothheadset(proxy); } public Boolean isDeviceConnected(BluetoothDevice device) { //invoke: boolean isConnected(BluetoothDevice device); try { Class<?> clazz = m_service.getClass(); Method isConnectedMethod = clazz.getmethod(IS_CONNECTED_METHOD_name, BluetoothDevice.class); Object result = isConnectedMethod.invoke(m_service, device); return (Boolean)result; } catch(NoSuchMethodException ex) { String msg = String.format("Failed to find %s method in class %s.", IS_CONNECTED_METHOD_name, m_service.getClass().getname()); Log.e(TAG, msg, ex); } catch(InvocationTargetException ex) { String msg = String.format("Failed to invoke %s method in class %s.", IS_CONNECTED_METHOD_name, m_service.getClass().getname()); Log.e(TAG, msg, ex); } catch(illegalaccessexception ex) { String msg = String.format("Illegal access when invoking %s method in class %s.", IS_CONNECTED_METHOD_name, m_service.getClass().getname()); Log.e(TAG, msg, ex); } catch(Exception ex) { Log.e(TAG, "UnkNown exception was thrown.", ex); } return null; } }
总结 以上是内存溢出为你收集整理的android – 如何通过反射使用BluetoothHeadset类全部内容,希望文章能够帮你解决android – 如何通过反射使用BluetoothHeadset类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)