Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

How to unpair bluetooth device using android 2.1 sdk

In Android 2.1, to unpair a Bluetooth device you can go to Bluetooth settings, long-click on a device and select Unpair to unpair that device. I want to be able to do this from my application. I can retrieve a list of paired/bonded devices using BluetoothAdapter.getBondedDevices(), but I can't find how to unpair. I've explored the BluetoothChat sample, and I've searched the sdk but still can't find an API that allows this.

How can I unpair a Bluetooth device?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's how you unpair/remove a bonded device call this method where macAddress is the string of the mac address of the device..i.e. "00:02:00:A3:03:05"

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

To get the IBluetooth Object you need to go through a couple of steps

  1. create a package in your project called android.bluetooth
  2. create two files, IBluetooth.aidl and IBluetoothCallback.aidl
  3. create method in your files called 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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...