Android蓝牙插座连接状态

时间:2013-09-18 09:11:48

标签: android bluetooth android-bluetooth

在我的Android应用程序中,我需要执行一个操作,以检查bluetooth socket是否已连接,然后再进入下一个状态。

在清单文件中,我定义了android:minSdkVersion="7" And android:targetSdkVersion="17"

所以我可以使用蓝牙套接字类中提供的 isConnected()方法来检查这个。但是这个方法已添加到API level 14中,因为我的android:targetSdkVersion="17"编译得很好并且正确安装在我的设备中。

但问题是在运行时它给我提供了以下错误。

java.lang.NoSuchMethodError: android.bluetooth.BluetoothSocket.isConnected

运行Android OS 4.0(API级别14)或更高版本的设备也不会出现此错误。

主要是我相信这些POST关于 java.lang.NoSuchMethodError 的探索。

问题

1)。问题是如何识别蓝牙套接字是否与运行Android操作系统版本低于4.0(API级别14)的设备连接或不连接。

如果某人有一个解决方案可以在运行低于Android 4.0版本的设备运行的设备中实现这一点。谢谢inadvance ... !!!

1 个答案:

答案 0 :(得分:2)

试试这个:

getApplicationContext().registerReceiver(receiver,
                new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));

getApplicationContext().registerReceiver(receiver,
                new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));

private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
        {
            // LOG - Connected
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
        {
            // LOG - Disconnected
        }
    }
}

当您连接到另一台设备时,这将启动BroadcastReceiver。 我希望这就是你要找的东西。