如何使用android 2.1 sdk解除蓝牙设备的配对

如何使用android 2.1 sdk解除蓝牙设备的配对,android,bluetooth,Android,Bluetooth,在Android 2.1中,要解除蓝牙设备的锁定,您可以进入蓝牙设置,长时间单击设备并选择“解除锁定以解除锁定该设备”。我希望能够从我的应用程序中做到这一点。我可以使用检索配对/绑定设备的列表,但找不到如何取消配对。我已经研究了BluetoothChat示例,并且搜索了sdk,但仍然找不到允许此操作的API 如何解锁蓝牙设备?可以通过droid java手动解锁设备 您可以调用隐藏方法来删除绑定。以下是取消绑定/删除绑定设备的方法调用此方法,其中macAddress是设备mac地址的字符串。“0

在Android 2.1中,要解除蓝牙设备的锁定,您可以进入蓝牙设置,长时间单击设备并选择“解除锁定以解除锁定该设备”。我希望能够从我的应用程序中做到这一点。我可以使用检索配对/绑定设备的列表,但找不到如何取消配对。我已经研究了BluetoothChat示例,并且搜索了sdk,但仍然找不到允许此操作的API


如何解锁蓝牙设备?

可以通过droid java手动解锁设备


您可以调用隐藏方法来删除绑定。

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

要获取IBluetooth对象,需要经过几个步骤

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

    /***************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);
    }
    
        package android.bluetooth;
    
        /**
         * System private API for Bluetooth service callbacks.
         *
         * {@hide}
         */
        interface IBluetoothCallback
        {
            void onRfcommChannelFound(int channel);
        }
    
  • /***********IBluetoothCallback.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);
    }
    
        package android.bluetooth;
    
        /**
         * System private API for Bluetooth service callbacks.
         *
         * {@hide}
         */
        interface IBluetoothCallback
        {
            void onRfcommChannelFound(int channel);
        }
    
    另一种方式:

    public void clear(View v) {
        Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
        try {
            Class<?> btDeviceInstance =  Class.forName(BluetoothDevice.class.getCanonicalName());
            Method removeBondMethod = btDeviceInstance.getMethod("removeBond");
            String currentMac = getCurrentMAC();
            boolean cleared = false;
                    for (BluetoothDevice bluetoothDevice : bondedDevices) {
                String mac = bluetoothDevice.getAddress();
                if(mac.equals(currentMac)) {
                    removeBondMethod.invoke(bluetoothDevice);
                    Log.i(TAG,"Cleared Pairing");
                    cleared = true;
                    break;
                }
            }
    
                    if(!cleared) {
                Log.i(TAG,"Not Paired");
                    }
        } catch (Throwable th) {
            Log.e(TAG, "Error pairing", th);
        }
    }
    
    公共空白清除(视图五){
    Set bondedDevices=adapter.getBondedDevices();
    试一试{
    类btDeviceInstance=Class.forName(BluetoothDevice.Class.getCanonicalName());
    方法removeBondMethod=btDeviceInstance.getMethod(“removeBond”);
    字符串currentMac=getCurrentMAC();
    布尔清除=假;
    用于(蓝牙设备蓝牙设备:bondedDevices){
    字符串mac=bluetoothDevice.getAddress();
    如果(mac等于(当前mac)){
    removeBondMethod.invoke(蓝牙设备);
    Log.i(标签“清除配对”);
    清除=真;
    打破
    }
    }
    如果(!清除){
    Log.i(标签“未配对”);
    }
    }捕获(可丢弃){
    Log.e(标签“错误配对”,th);
    }
    }
    
    您可以将用户发送到蓝牙设置,通过此设置,您可以取消配对设备的配对 Intent Intent TENTOPENBLUETOHTSETTINGS=新Intent();intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION\u蓝牙设置);
    StartTactivity(IntentTopenBluetooth设置)`

    我可能想问这个问题或者更多地搜索这个场景,但是我上面的建议是我发现的以编程方式取消绑定的方法。要手动解除配对,请使用手机中已有的BlueToth程序。谢谢!!!这对我来说太棒了!在哪里定义了“removeBond(BluetoothDevice)”方法?为什么我们必须调用反射来调用它?嘿,d3n13d1,我尝试过这个,但在API级别24及以上的版本上不起作用,你能建议其他方法吗,谢谢,我只使用了16版和17版的apk,但它可以在我的像素上工作。请将此包共享在一个zip文件中好吗?很抱歉,我不理解第3点)“在文件中创建名为getBluetooth()的方法”?那在哪里?为什么我会得到一个6年前我写的“无法解析符号IBluetooth”的Lol…我已经记不起更好的要点了。很可能您的IBlueTooth.aidl不在正确的位置,或者未在生成脚本中导入。请解释您的代码,以便其他用户了解其功能。谢谢@IgnacioAra,当最终用户点击“扫描设备”按钮时,我们将发送给他蓝牙设置意图。他可以从那里配对设备,然后返回应用程序。