监控已配对蓝牙设备的连接状态

3
我已经编写了代码,返回Android设备上所有配对蓝牙设备的列表。其中一个配对的蓝牙设备是我的笔记本电脑。
现在我的问题是:
是否有一种方法可以监视笔记本电脑和Android设备之间的蓝牙连接状态。我需要的输出是“已连接”,如果存在蓝牙连接,“未连接”如果没有蓝牙连接。
Android版本为2.1.Eclair 在继续之前,我有一些问题。
- >当我通过USB连接笔记本电脑和设备时,我应该测试这个应用程序吗? - >如何在单独的线程或从AsyncTask中运行它? - >如果我通过USB连接设备和笔记本电脑,然后启动应用程序,以上代码对我不起作用。即使笔记本电脑和手机附近并且它们已经配对,我也无法在手机上看到已连接状态。
我编写的代码如下:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_);

    out = (TextView) findViewById(R.id.textView1);

    // Getting the Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    // out.append("\nAdapter: " + adapter);

    // Check for Bluetooth support in the first place
    // Emulator doesn't support Bluetooth and will return null
    if (adapter == null) {
        out.append("\nBluetooth NOT supported. Aborting.");
        return;
    }

    // Starting the device discovery
    out.append("\nStarting discovery...");
    adapter.startDiscovery();
    out.append("\nDone with discovery...");

    // Listing paired devices
    out.append("\nDevices Pared:");
    Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter()
            .getBondedDevices();

    for (BluetoothDevice device : devices) {
        out.append("\nFound device: " + device);
        out.append("\n The name is: " + device.getName());
        out.append("\n The type is: "
                + device.getBluetoothClass().getDeviceClass());

        Method m;
        try {
            System.out.println("Trying to connect to " + device.getName());
            m = device.getClass().getMethod("createInsecureRfcommSocket",
                    new Class[] { int.class });
            BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
            socket.connect();
            out.append("Connected");
        } catch (Exception e) {
            e.printStackTrace();
            out.append("\nDisconnected");
        }
    }
}
1个回答

2
今日免费次数已满, 请开通会员/明日再来
<uses-permission android:name="android.permission.BLUETOOTH" />

您可以尝试下面的代码,但一定要在单独的线程或从AsyncTask中运行,否则您的UI可能会挂起。
此代码将检查您配对的设备并尝试连接到每一个设备。如果成功,则在logcat上打印已连接,否则打印未连接
private void checkPairedPrinters() throws IOException {
        Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        for (BluetoothDevice device : devices) {
            Method m;
            try {
                System.out.println("Trying to connect to " + device.getName());
                m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
                BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
                socket.connect();
                System.out.println("Connected");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Disconnected");
            }
        }

}

1
谢谢您的帮助,我已经在我的AndroidManifest文件中拥有了权限。在我的情况下,连接将在移动设备之间进行。我该如何在我的代码中使用这个方法?我必须在哪里调用它? - user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接