gpt4 book ai didi

android - 如何取消配对蓝牙设备使用 android 2.1 sdk

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:46 27 4
gpt4 key购买 nike

在 Android 2.1 中,要取消配对蓝牙设备,您可以转到蓝牙设置,长按设备并选择取消配对以取消配对该设备。我希望能够从我的应用程序中执行此操作。我可以使用 BluetoothAdapter.getBondedDevices() 检索配对/绑定(bind)设备列表,但我找不到取消配对的方法。我研究了 BluetoothChat 示例,搜索了 SDK,但仍然找不到允许这样做的 API。

如何取消与蓝牙设备的配对?

最佳答案

以下是取消配对/删除绑定(bind)设备的方法调用此方法,其中 macAddress 是设备 mac 地址的字符串..i.e. “00:02:00:A3:03:05”

IBluetooth ib =getIBluetooth();
ib.removeBond(macAddress);

要获得 IBluetooth 对象,您需要完成几个步骤

  1. 在您的项目中创建一个名为 android.bluetooth 的包
  2. 创建两个文件,IBluetooth.aidl 和 IBluetoothCallback.aidl
  3. 在您的文件中创建名为 getBluetooth() 的方法

    private IBluetooth getIBluetooth() {
    IBluetooth ibt = null;

    try {

    Class c2 = Class.forName("android.os.ServiceManager");

    Method m2 = c2.getDeclaredMethod("getService",String.class);
    IBinder b = (IBinder) m2.invoke(null, "bluetooth");

    Class c3 = Class.forName("android.bluetooth.IBluetooth");

    Class[] s2 = c3.getDeclaredClasses();

    Class c = s2[0];
    Method m = c.getDeclaredMethod("asInterface",IBinder.class);
    m.setAccessible(true);
    ibt = (IBluetooth) m.invoke(null, b);


    } catch (Exception e) {
    Log.e("flowlab", "Erroraco!!! " + e.getMessage());
    }

    return ibt;
    }

    /************ IBluetooth.aidl ************/

    package android.bluetooth;

    import android.bluetooth.IBluetoothCallback;
    import android.os.ParcelUuid;

    /**
    * System private API for talking with the Bluetooth service.
    *
    * {@hide}
    */
    interface IBluetooth
    {
    boolean isEnabled();
    int getBluetoothState();
    boolean enable();
    boolean disable(boolean persistSetting);

    String getAddress();
    String getName();
    boolean setName(in String name);

    int getScanMode();
    boolean setScanMode(int mode, int duration);

    int getDiscoverableTimeout();
    boolean setDiscoverableTimeout(int timeout);

    boolean startDiscovery();
    boolean cancelDiscovery();
    boolean isDiscovering();

    boolean createBond(in String address);
    boolean cancelBondProcess(in String address);
    boolean removeBond(in String address);
    String[] listBonds();
    int getBondState(in String address);

    String getRemoteName(in String address);
    int getRemoteClass(in String address);
    ParcelUuid[] getRemoteUuids(in String address);
    boolean fetchRemoteUuids(in String address, in ParcelUuid uuid, in IBluetoothCallback callback);
    int getRemoteServiceChannel(in String address, in ParcelUuid uuid);

    boolean setPin(in String address, in byte[] pin);
    boolean setPasskey(in String address, int passkey);
    boolean setPairingConfirmation(in String address, boolean confirm);
    boolean cancelPairingUserInput(in String address);

    boolean setTrust(in String address, in boolean value);
    boolean getTrustState(in String address);

    int addRfcommServiceRecord(in String serviceName, in ParcelUuid uuid, int channel, IBinder b);
    void removeServiceRecord(int handle);
    }

/************ IBluetoothCallback.aidl************/

    package android.bluetooth;

/**
* System private API for Bluetooth service callbacks.
*
* {@hide}
*/
interface IBluetoothCallback
{
void onRfcommChannelFound(int channel);
}

关于android - 如何取消配对蓝牙设备使用 android 2.1 sdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com