在Android 2.1中,要取消配对蓝牙设备,您可以转至蓝牙设置,长按设备并选择取消配对以取消配对该设备。我希望能够从我的应用程序中执行此操作。我可以使用BluetoothAdapter.getBondedDevices()检索配对/绑定设备的列表,但我找不到如何配对。我已经探索了BluetoothChat示例,并且我已经搜索了sdk,但仍然找不到允许这样做的API。如何在Android 2.1中取消配对蓝牙设备sdk
如何取消配对蓝牙设备?
在Android 2.1中,要取消配对蓝牙设备,您可以转至蓝牙设置,长按设备并选择取消配对以取消配对该设备。我希望能够从我的应用程序中执行此操作。我可以使用BluetoothAdapter.getBondedDevices()检索配对/绑定设备的列表,但我找不到如何配对。我已经探索了BluetoothChat示例,并且我已经搜索了sdk,但仍然找不到允许这样做的API。如何在Android 2.1中取消配对蓝牙设备sdk
如何取消配对蓝牙设备?
可以通过droid java手动取消配对设备。
您可以调用隐藏的方法来删除键。
下面介绍如何取消配对/删除绑定设备时调用此方法,其中macAddress是设备的mac地址字符串..i.e。 “00:02:00:A3:03:05”
IBluetooth ib =getIBluetooth();
ib.removeBond(macAddress);
要获得IBluetooth对象,你需要经过几个步骤
创建文件)方法调用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文件中分享这个包? – 2014-01-14 11:24:03
对不起,但我不明白3)“在你的文件中创建方法叫做getBluetooth()”?哪里是 ?为什么我得到一个“无法解决的符号IBluetooth” – 2017-11-14 08:12:39
Lol我写了这6年前...我不记得更细的点了。很有可能您的IBlueTooth.aidl不在正确的位置或未在构建脚本中导入。 – JPM 2017-11-14 16:01:54
另一种方式:
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设备)”方法在哪里定义?为什么我们不得不调用反射来调用这个? – ossys 2013-03-12 16:55:30
可能想问这个问题或者更多地搜索这个视域,但是我上面的建议是我发现以编程方式取消配对的方式。要手动取消配对,请使用手机中已有的Bluettooth程序。 – JPM 2011-08-24 14:52:33