如何使用 android 2.1 sdk 取消配对蓝牙设备 [英] How to unpair bluetooth device using android 2.1 sdk
问题描述
在 Android 2.1 中,要取消配对蓝牙设备,您可以转到蓝牙设置,长按设备并选择取消配对以取消配对该设备.我希望能够从我的应用程序中做到这一点.我可以使用 BluetoothAdapter.getBondedDevices 检索配对/绑定设备列表(),但我找不到如何取消配对.我已经探索了 BluetoothChat 示例,我已经搜索了 sdk,但仍然找不到允许这样做的 API.
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?
推荐答案
以下是取消配对/移除绑定设备的方法,调用此方法,其中 macAddress 是设备的 mac 地址字符串..i.e.00:02:00:A3:03:05"
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);
要获得 IBluetooth 对象,您需要经过几个步骤
To get the IBluetooth Object you need to go through a couple of steps
- 在您的项目中创建一个名为 android.bluetooth 的包
- 创建两个文件,IBluetooth.aidl 和 IBluetoothCallback.aidl
在您的文件中创建名为 getBluetooth() 的方法
- create a package in your project called android.bluetooth
- create two files, IBluetooth.aidl and IBluetoothCallback.aidl
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 ************/
/************ 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 ************/
/************ IBluetoothCallback.aidl ************/
package android.bluetooth;
/**
* System private API for Bluetooth service callbacks.
*
* {@hide}
*/
interface IBluetoothCallback
{
void onRfcommChannelFound(int channel);
}
这篇关于如何使用 android 2.1 sdk 取消配对蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!