【问题标题】:How to unpair bluetooth device using android 2.1 sdk如何使用 android 2.1 sdk 取消配对蓝牙设备
【发布时间】:2010-08-11 21:24:59
【问题描述】:

在 Android 2.1 中,要取消配对蓝牙设备,您可以转到蓝牙设置,长按设备并选择取消配对以取消配对该设备。我希望能够从我的应用程序中做到这一点。我可以使用BluetoothAdapter.getBondedDevices() 检索配对/绑定设备的列表,但我找不到如何取消配对。我探索了 BluetoothChat 示例,并搜索了 sdk,但仍然找不到允许这样做的 API。

如何取消配对蓝牙设备?

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    以下是取消配对/移除绑定设备的方法,调用此方法,其中 macAddress 是设备的 MAC 地址字符串。即"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);
        }
    

    【讨论】:

    • 请把这个包分享到一个 zip 文件中好吗?
    • 抱歉无知,但我不明白第 3 点)“在您的文件中创建名为 getBluetooth() 的方法”?哪里是 ?为什么我会收到“无法解析符号 IBluetooth”
    • 哈哈,这是我 6 年前写的……我不记得细节了。很可能您的 IBlueTooth.aidl 不在正确的位置或未在构建脚本中导入。
    【解决方案2】:

    另一种方式:

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

    【讨论】:

    • 谢谢!!!这对我很有用! “removeBond(BluetoothDevice)”方法在哪里定义?为什么我们必须调用反射来调用它?
    • 嘿 d3n13d1,我试过了,但在 API 级别 24 及更高级别上不起作用,您能否建议任何替代方法,谢谢
    • 我只使用了它与 apk 的版本 16 和 17,但它适用于我的像素。
    【解决方案3】:

    您可以将用户发送到蓝牙设置,这样您可以取消配对的设备 意图意图OpenBluetoothSettings = new Intent(); intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); startActivity(intentOpenBluetoothSettings);`

    【讨论】:

    • 请解释您的代码,以便其他用户了解其功能。谢谢!
    • @IgnacioAra,当最终用户点击“扫描设备”按钮时,我们会将他发送到蓝牙设置意图。他可以从那里配对设备并返回到应用程序。
    【解决方案4】:

    可以通过 droid java 手动取消配对设备。

    你可以调用一个隐藏的方法来移除绑定。

    【讨论】:

    • 可能想问这个问题或更多地搜索这个景象,但我上面的建议是我发现以编程方式取消配对的方式。要手动取消配对,请使用手机中已有的蓝牙程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2019-05-13
    相关资源
    最近更新 更多