Android无法检测蓝牙设备

时间:2014-10-09 03:57:15

标签: android broadcastreceiver android-bluetooth

我的广播接收器出现问题并找到特定的设备。在我的程序早期,我寻找当前的设备并允许用户选择。该名称和地址已保存。当应用程序处理其“onResume”时,我创建了一个广播接收器。

我要做的就是查看以前保存的设备是否在该区域内。 不配对,但至少必须看到它。我没有看到任何这些干杯消息。我甚至试图在此之前将设备置于可发现模式。

这是我的广播接收器。

mBluetoothReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                String deviceName = device.getName();
                String deviceAddress = device.getAddress();

                Toast toast = Toast.makeText(getApplicationContext(), "BT Found", Toast.LENGTH_LONG);
                toast.show();

                if (deviceAddress.equals(mDeviceAddress)) {
                    Toast toasty = Toast.makeText(getApplicationContext(), "Correct BT Device Found", Toast.LENGTH_LONG);
                    toasty.show();
                }
            }
            if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceAddress = device.getAddress();
                Toast toast = Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_LONG);
                toast.show();

                if (deviceAddress.equals(mDeviceAddress)) {
                    Toast toasty = Toast.makeText(getApplicationContext(), "Correct BT Device Connected", Toast.LENGTH_LONG);
                    toasty.show();
                }
            }
            } catch (Exception e) {
                System.out.println("Broadcast Error : " + e.toString());
            }
        }
    };

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mBluetoothReceiver, filter);

    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mBluetoothReceiver, filter);

    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    this.registerReceiver(mBluetoothReceiver, filter);

    filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    this.registerReceiver(mBluetoothReceiver, filter);

    filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    this.registerReceiver(mBluetoothReceiver, filter);

    filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mBluetoothReceiver, filter);

修改

似乎正在正确检测连接和断开连接事件。它们仅在变化时检测到。但是,ACTION_FOUND不是。我不能简单地进入设备区域。

0 个答案:

没有答案