在配对状态下捕获蓝牙

时间:2016-08-29 05:39:17

标签: android bluetooth

我有一个listView,它会显示配对的蓝牙设备列表。我希望捕获状态,以便在接受与我的设备配对的新请求时更新paired listView

到目前为止,我一直试着听BluetoothDevice.ACTION_ACL_CONNECTED,但无济于事。

 protected void onCreate(Bundle savedInstanceState) {
        ...
        IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
        this.registerReceiver(mReceiver, filter1);

    }


//The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                getPairedDevices(); // get paired devices and update listView

            }
}

1 个答案:

答案 0 :(得分:1)

首先,要扫描配对和非配对设备,您可以使用此代码

public void scanDevices(){
    IntentFilter filter = new IntentFilter();

    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

    activity.registerReceiver(mReceiverScan, filter);
    bluetoothAdapter.startDiscovery();
}

public void pair(BluetoothDevice device){
    activity.registerReceiver(mPairReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
    devicePair=device;
    try {
        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        if(discoveryCallback!=null)
            discoveryCallback.onError(e.getMessage());
    }
}

public void unpair(BluetoothDevice device) {
    devicePair=device;
    try {
        Method method = device.getClass().getMethod("removeBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        if(discoveryCallback!=null)
            discoveryCallback.onError(e.getMessage());
    }
}

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

        switch (action) {
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (state == BluetoothAdapter.STATE_OFF) {
                    if (discoveryCallback != null)
                        discoveryCallback.onError("Bluetooth turned off");
                }
                break;
            case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                context.unregisterReceiver(mReceiverScan);
                if (discoveryCallback != null)
                    discoveryCallback.onFinish();
                break;
            case BluetoothDevice.ACTION_FOUND:
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (discoveryCallback != null)
                    discoveryCallback.onDevice(device);
                break;
        }
    }
};

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
            final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

            if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
                if(discoveryCallback!=null)
                    discoveryCallback.onPair(devicePair);
            } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
                if(discoveryCallback!=null)
                    discoveryCallback.onUnpair(devicePair);
            }
        }
    }
};

最后,您可以将这些设备添加到列表中

public List<BluetoothDevice> getPairedDevices(){
    List<BluetoothDevice> devices = new ArrayList<>();
    for (BluetoothDevice blueDevice : bluetoothAdapter.getBondedDevices()) {
        devices.add(blueDevice);
    }
    return devices;
}

不要忘记在AndroidManifet.xml文件中添加权限

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

我使用的这是一个很好的库

https://github.com/omaflak/Bluetooth-Library

这是一个使用上述lib的示例应用程序。

https://github.com/omaflak/Bluetooth-Android