与反射的createRfcommSocket问题 [英] Problems with Reflection for createRfcommSocket

查看:476
本文介绍了与反射的createRfcommSocket问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是奇怪的。我试图建立蓝牙SPP在Droid X的的Andr​​oid 2.3.4。我已经包括了一些不同的方法,从试图打开一个RFCOMM插座,一个是反射(需要支持某些手机)

This is odd. I am trying to set up Bluetooth SPP on a Droid X Android 2.3.4. I have included a few different methods from attempting to open an rfcomm Socket, one being reflection (needed for support of certain phones)

不过,与Droid X的一个奇怪的事情发生了。当我第一次连接蓝牙设备我的应用程序打开并尝试由最高人民检察院第一:

However, with the Droid X a strange thing happens. When I connect the first time to a Bluetooth device my app opens up and attempts SPP by first:

Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, Integer.valueOf(1));

如果它找不到方法,它会尝试正常

And if it can't find the method, it tries the normal

mmSocket = mmDevice.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);

这正常工作第一次通过。但是,如果我关闭的程序,然后重新打开它由于某种原因,反射法后来居然找到了方法,尝试连接没有任何错误,但它实际上并没有连接。

This works correctly the first time through. But if I close my program then reopen it for some reason the reflection method then actually finds the method, attempts to connect doesn't have any errors, but it doesn't actually connect.

如果我关闭应用程序,电源周期的蓝牙适配器的手机上,然后再试一次它会正常工作。

If I close the app, power cycle the bluetooth adapter on the phone and try again it will work correctly.

我在上这是怎么回事亏损,所以任何帮助将是很大的AP preciated。

I am at a loss on what's going on, so any help would be greatly appreciated.

推荐答案

嗯,我想出了一个创可贴式的修复,我不开心,但会工作,直到我能找到问题的根源或更好的解决方案。我只是做一个包含级别的方法来尝试共享preference对象。

Well I came up with a bandaid type fix that I am not happy with, but will work until I can find the root cause or better solution. I simply make a shared preference object that contains a "level" of which methods to try.

水平标记1-4,并且如果电平被设置为一个数字&其中; =到目前的水平,将尝试该方法,如果没有连接已经建立。

The levels are labeled 1-4, and if the level is set to a number <= to the current level it will try that method if no connection has been established.

if(level<=1){
success set level = 1
fail set level = 0
}

if(level<=2){
success set level = 2
fail set level = 0
}

...


if(level<=4){
success set level = 4
fail set level = 0
}

这篇关于与反射的createRfcommSocket问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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