0

我正在使用代码进行蓝牙连接,

public void run() 
{
    try 
    {




        Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
        mBluetoothSocket = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);


       // mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
        mBluetoothAdapter.cancelDiscovery();

        mBluetoothSocket.connect();





    }
    catch (IOException eConnectException) 
    {
        Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
         closeSocket(mBluetoothSocket);
         return;
    } catch (SecurityException e) {

        Log.d(TAG, "CouldNotConnectToSocket", e);
        closeSocket(mBluetoothSocket);
    } catch (NoSuchMethodException e) {

        Log.d(TAG, "CouldNotConnectToSocket", e);
        closeSocket(mBluetoothSocket);
    } catch (IllegalArgumentException e) {
        Log.d(TAG, "CouldNotConnectToSocket", e);
        closeSocket(mBluetoothSocket);
    } catch (IllegalAccessException e) {
        Log.d(TAG, "CouldNotConnectToSocket", e);
        closeSocket(mBluetoothSocket);
    } catch (InvocationTargetException e) {
        Log.d(TAG, "CouldNotConnectToSocket", e);
        closeSocket(mBluetoothSocket);
    }
}

它在 android 2.2 的 Karbonn 设备上运行良好,当我在三星 Galaxy Y 和索尼爱立信 Xperia mini 上尝试相同的代码时,它不起作用......可能是什么问题......请协助......

4

1 回答 1

0

您是否在清单中设置了权限?

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>

你的例外是什么?

编辑:根据这个 http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_DISCONNECTED 这是一个不应该发生的低级异常。

如何以编程方式判断蓝牙设备是否已连接?(Android 2.2) 这对您有帮助吗?

于 2012-04-26T12:31:52.277 回答