我写了一个android应用程序,使用蓝牙从外部传感器获取数据。它在索尼爱立信XPERIA上运行良好,但在HTC Hero上运行不佳(它可以找到外部设备,但无法从这些设备获取任何数据)。我想知道为什么。我在网上研究了一下,还是没有发现任何线索。有人在HTC上遇到过类似的蓝牙问题吗?
发布于 2011-11-26 02:47:22
如果我没记错的话,HTC手机在某个API级别(可能是2.1或更低?)有问题。解决方案是反射。
参考
Disconnect a bluetooth socket in Android
Service discovery failed exception using Bluetooth on Android
解决方案
不是使用
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);使用
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);要在某些宏达电手机上使用特定的应用程序接口级别来获取BluetoothSocket。
扩展的的解决方案
我最近有一个应用程序,我必须考虑到这一点,我不喜欢在非HTC手机上使用它,所以我有一个条件来检查HTC,如果是真的,那么使用反射,否则不使用反射。
public BTConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the given BluetoothDevice
if (isAnHTCDevice())
{
try
{
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
}
catch (Exception e)
{
Log.e(BCTAG, "Error at HTC/createRfcommSocket: " + e);
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(MSG_BT_LOG_MESSAGE, "Exception creating htc socket: " + e));
}
}
else
{
try
{
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (Exception e)
{
Log.e(BCTAG, "Error at createRfcommSocketToServiceRecord: " + e);
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(MSG_BT_LOG_MESSAGE, "Exception creating socket: " + e));
}
}
mmSocket = tmp;
}isAnHTCDevice():
public boolean isAnHTCDevice()
{
String manufacturer = android.os.Build.MANUFACTURER;
if (manufacturer.toLowerCase().contains("htc"))
return true;
else
return false;
}发布于 2012-09-29 11:16:14
你可以这样做:
private final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb"; //standard pbap uuid
mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid())
mSocket.connect();就这么做。
发布于 2012-09-29 11:11:37
你可以这样做:
private final String PBAP_UUID = "0000112f-0000-1000-8000-00805f9b34fb"; //standard pbap uuid
mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(ParcelUuid.fromString(PBAP_UUID).getUuid());mSocket.connect();
就这么做。
https://stackoverflow.com/questions/8215108
复制相似问题