【问题标题】:Monitoring the connection of a paired bluetooth device监控配对蓝牙设备的连接
【发布时间】:2012-11-06 22:23:00
【问题描述】:

我已经编写了代码,它返回了一个 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");
        }
    }
}

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    您使用的是哪个安卓版本?

    您必须拥有您的 AndroidManifest.xml 的权限:

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

    您可以尝试下面的代码,但请确保在单独的线程上或从AsyncTask 运行它,否则您的 UI 可能会挂起。 这将检查您配对的设备并尝试连接到每个设备。如果成功,则在 logcat 上打印 Connected,否则打印 Disconnected

    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");
                }
            }
    
    }
    

    【讨论】:

    • 感谢您的帮助,我已经在我的 AndroidManifest 文件中获得了权限。在我的情况下,连接将在移动设备之间我如何在我的代码中使用这种方法?我必须在哪里调用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多