Now the online more popular Bluetooth tool class:
@SuppressLint ("Newapi") public class Clsutils {
Public Clsutils () {
TODO auto-generated Constructor stub
}
/**
*/settings/src/com/android/settings/bluetooth/cachedbluetoothdevice.java
*/
Static public boolean createbond (class<? extends Bluetoothdevice> Btclass,
Bluetoothdevice Btdevice) throws Exception {
Method Createbondmethod = Btclass.getmethod ("Createbond");
Boolean returnvalue = (Boolean) createbondmethod.invoke (Btdevice);
return Returnvalue.booleanvalue ();
}
/**
*/settings/src/com/android/settings/bluetooth/cachedbluetoothdevice.java
*/
Static public boolean removebond (class<? extends Bluetoothdevice> Btclass,
Bluetoothdevice Btdevice) throws Exception {
Method Removebondmethod = Btclass.getmethod ("Removebond");
Boolean returnvalue = (Boolean) removebondmethod.invoke (Btdevice);
return Returnvalue.booleanvalue ();
}
Static public boolean Setpin (Class btclass, Bluetoothdevice Btdevice,
Byte[] str) throws Exception {
try {
Method Removebondmethod = Btclass.getdeclaredmethod ("Setpin", new class[] {byte[].class});
Boolean returnvalue = (Boolean) removebondmethod.invoke (BTDEVICE,STR);
return Returnvalue.booleanvalue ();
} catch (SecurityException e) {
E.printstacktrace ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
}
return false;
}
Static public boolean cancelpairinguserinput (Class<?> Btclass,
Bluetoothdevice device)
Throws Exception {
Method Createbondmethod = Btclass.getmethod ("Cancelpairinguserinput");
Cancelbondprocess (btclass, device);
Boolean returnvalue = (Boolean) createbondmethod.invoke (device);
return Returnvalue.booleanvalue ();
}
Static public boolean cancelbondprocess (Class<?> Btclass,
Bluetoothdevice device)
Throws Exception {
Method Createbondmethod = Btclass.getmethod ("cancelbondprocess");
Boolean returnvalue = (Boolean) createbondmethod.invoke (device);
return Returnvalue.booleanvalue ();
}
/**
*
* @param clsshow
*/
static public void Printallinform (class<?> clsshow) {
try {
method[] Hidemethod = Clsshow.getmethods ();
int i = 0;
for (; i < hidemethod.length; i++) {
LOG.E ("Method name", Hidemethod[i].getname () + "; and the I is:"
+ i);
}
field[] AllFields = Clsshow.getfields ();
for (i = 0; i < allfields.length; i++) {
LOG.E ("Field name", Allfields[i].getname ());
}
} catch (SecurityException e) {
throw new RuntimeException (E.getmessage ());
E.printstacktrace ();
} catch (IllegalArgumentException e) {
throw new RuntimeException (E.getmessage ());
E.printstacktrace ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Static public boolean pair (String straddr, byte[] strpsw) {
Boolean result = false;
Bluetoothadapter bluetoothadapter = Bluetoothadapter.getdefaultadapter ();
Bluetoothadapter.canceldiscovery ();
if (!bluetoothadapter.isenabled ()) {
Bluetoothadapter.enable ();
}
Bluetoothdevice device = Bluetoothadapter.getremotedevice (STRADDR);
if (device.getbondstate ()! = bluetoothdevice.bond_bonded) {
try {
LOG.D ("MyLog", "not bond_bonded");
Boolean Flag1 = Clsutils.setpin (Device.getclass (), device,
STRPSW);
Boolean flag2 = Clsutils.createbond (Device.getclass (), device);
Remotedevice = device;
result = true;
} catch (Exception e) {
TODO auto-generated Catch block
LOG.D ("MyLog", "Setpin failed!");
E.printstacktrace ();
} //
} else {
LOG.D ("MyLog", "has bond_bonded");
try {
Clsutils.removebond (Device.getclass (), device);
Clsutils.createbond (Device.getclass (), device);
Boolean Flag1 = Clsutils.setpin (Device.getclass (), device,
STRPSW);
Boolean flag2 = Clsutils.createbond (Device.getclass (), device);
result = true;
} catch (Exception e) {
TODO auto-generated Catch block
LOG.D ("MyLog", "Setpin failed!");
E.printstacktrace ();
}
}
return result;
}
A2DP?? HeadSet
public static Boolean connect (Class btclass,bluetoothprofile proxy,bluetoothdevice btdevice) throws Exception {
Method Connectmethod = Btclass.getdeclaredmethod ("Connect", Bluetoothdevice.class);
Connectmethod.setaccessible (TRUE);
Boolean returnvalue = (Boolean) connectmethod.invoke (Proxy,btdevice);
return Returnvalue.booleanvalue ();
}
public static Boolean disconnect (Class btclass,bluetoothprofile proxy,bluetoothdevice btdevice) throws Exception {
Method Disconnectmethod = Btclass.getdeclaredmethod ("disconnect", bluetoothdevice.class);
Disconnectmethod.setaccessible (TRUE);
Boolean returnvalue = (Boolean) disconnectmethod.invoke (Proxy,btdevice);
return Returnvalue.booleanvalue ();
}
public static void Setdiscoverabletimeout (int timeout) {
Bluetoothadapter Adapter=bluetoothadapter.getdefaultadapter ();
try {
Method setdiscoverabletimeout = BluetoothAdapter.class.getMethod ("Setdiscoverabletimeout", Int.class);
Setdiscoverabletimeout.setaccessible (TRUE);
Method setscanmode =bluetoothadapter.class.getmethod ("Setscanmode", Int.class,int.class);
Setscanmode.setaccessible (TRUE);
Setdiscoverabletimeout.invoke (adapter, timeout);
Setscanmode.invoke (adapter, bluetoothadapter.scan_mode_connectable_discoverable,timeout);
} catch (Exception e) {
E.printstacktrace ();
}
}
public static void Closediscoverabletimeout () {
Bluetoothadapter Adapter=bluetoothadapter.getdefaultadapter ();
try {
Method setdiscoverabletimeout = BluetoothAdapter.class.getMethod ("Setdiscoverabletimeout", Int.class);
Setdiscoverabletimeout.setaccessible (TRUE);
Method setscanmode =bluetoothadapter.class.getmethod ("Setscanmode", Int.class,int.class);
Setscanmode.setaccessible (TRUE);
Setdiscoverabletimeout.invoke (adapter, 1);
Setscanmode.invoke (adapter, bluetoothadapter.scan_mode_connectable,1);
} catch (Exception e) {
E.printstacktrace ();
}
}
public static void Getcurrentconnectingdevice (Context mccontext,bluetoothadapter bluetoothadapter,final Checkconnectinglistener listener) {
int A2DP = Bluetoothadapter.getprofileconnectionstate (BLUETOOTHPROFILE.A2DP);
int headset = bluetoothadapter.getprofileconnectionstate (Bluetoothprofile.headset);
int health = bluetoothadapter.getprofileconnectionstate (bluetoothprofile.health);
int flag =-1;
if (A2DP = = bluetoothprofile.state_connected) {
flag = A2DP;
}
else if (headset = = bluetoothprofile.state_connected) {
flag = headset;
}
else if (health = = bluetoothprofile.state_connected) {
Flag = health;
}
if (flag! =-1) {
Bluetoothadapter.getprofileproxy (Mccontext, New Servicelistener () {
@Override
public void onservicedisconnected (Int. profile) {
Listener.disconnected ();
LOG.I ("", "hh=======onservicedisconnected=>>");
}
@Override
public void onserviceconnected (Int. profile, Bluetoothprofile proxy) {
LOG.I ("", "hh=======onserviceconnected=>>" +profile);
list<bluetoothdevice> mdevices = Proxy.getconnecteddevices ();
Listener.getconnectingdvice (mdevices);
}
}, flag);
}else{
Listener.disconnected ();
}
}
}
Bluetooth connection pairing and Visual tool class