如何使用Android 2.1 sdk解开蓝牙设备

前端之家收集整理的这篇文章主要介绍了如何使用Android 2.1 sdk解开蓝牙设备前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Android 2.1中,要取消蓝牙设备的配置,您可以转到蓝牙设置,长时间点击设备,然后选择“取消配对”以取消配置该设备.我想要从我的应用程序中做到这一点.我可以使用 BluetoothAdapter.getBondedDevices()检索配对/保密设备的列表,但是我找不到如何取消配对.我已经探索了BluetoothChat示例,我搜索了sdk,但仍然找不到允许这个的API.

如何取消蓝牙装置的配对?

解决方法

这是您如何取消/删除绑定设备调用方法,其中macAddress是设备的MAC地址的字符串. “00:02:00:A3:03:05”
IBluetooth ib =getIBluetooth();
ib.removeBond(macAddress);

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

>在您的项目中创建一个名为android.bluetooth的包
>创建两个文件,IBluetooth.aidl和IBluetoothCallback.aidl
>创建方法在你的文件调用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,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相关文章