android蓝牙自动连接
① 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册
<receiverandroid:name=".">
<intent-filter>
<actionandroid:name="android.bluetooth.device.action.PAIRING_REQUEST"/>
</intent-filter>
</receiver>
自己在收到广播时处理并将预先输入的密码设置进去
java">
{
StringstrPsw="0";
@Override
publicvoidonReceive(Contextcontext,Intentintent)
{
//TODOAuto-generatedmethodstub
if(intent.getAction().equals(
"android.bluetooth.device.action.PAIRING_REQUEST"))
{
BluetoothDevicebtDevice=intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//byte[]pinBytes=BluetoothDevice.convertPinToBytes("1234");
//device.setPin(pinBytes);
Log.i("tag11111","ddd");
try
{
ClsUtils.setPin(btDevice.getClass(),btDevice,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(btDevice.getClass(),btDevice);
ClsUtils.cancelPairingUserInput(btDevice.getClass(),btDevice);
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
<b>/************************************蓝牙配对函数***************/
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importandroid.bluetooth.BluetoothDevice;
importandroid.util.Log;
publicclassClsUtils
{
/**
*与设备配对参考源码:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleancreateBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("createBond");
BooleanreturnValue=(Boolean)createBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}
/**
*与设备解除配对参考源码:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodremoveBondMethod=btClass.getMethod("removeBond");
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}
staticpublicbooleansetPin(ClassbtClass,BluetoothDevicebtDevice,
Stringstr)throwsException
{
try
{
MethodremoveBondMethod=btClass.getDeclaredMethod("setPin",
newClass[]
{byte[].class});
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice,
newObject[]
{str.getBytes()});
Log.e("returnValue",""+returnValue);
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returntrue;
}
//取消用户输入
(ClassbtClass,
BluetoothDevicedevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelPairingUserInput");
//cancelBondProcess()
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}
//取消配对
(ClassbtClass,
BluetoothDevicedevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelBondProcess");
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}
/**
*
*@paramclsShow
*/
(ClassclsShow)
{
try
{
//取得所有方法
Method[]hideMethod=clsShow.getMethods();
inti=0;
for(;i<hideMethod.length;i++)
{
Log.e("methodname",hideMethod[i].getName()+";andtheiis:"
+i);
}
//取得所有常量
Field[]allFields=clsShow.getFields();
for(i=0;i<allFields.length;i++)
{
Log.e("Fieldname",allFields[i].getName());
}
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}</b>
执行时直接使用:
<b>publicstaticbooleanpair(StringstrAddr,StringstrPsw)
{
booleanresult=false;
=BluetoothAdapter
.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
if(!bluetoothAdapter.isEnabled())
{
bluetoothAdapter.enable();
}
if(!BluetoothAdapter.checkBluetoothAddress(strAddr))
{//检查蓝牙地址是否有效
Log.d("mylog","devAdneffient!");
}
BluetoothDevicedevice=bluetoothAdapter.getRemoteDevice(strAddr);
if(device.getBondState()!=BluetoothDevice.BOND_BONDED)
{
try
{
Log.d("mylog","NOTBOND_BONDED");
ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//配对完毕就把这个设备对象传给全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}//
}
else
{
Log.d("mylog","HASBOND_BONDED");
try
{
ClsUtils.createBond(device.getClass(),device);
ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}
}
returnresult;
}</b>
② 如何实现android蓝牙自动配对连接
android蓝牙自动配对连接的具体代码如下:
1. 获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。
2. 判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。
3. 启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
4. 获取蓝牙适配器中已经配对的设备Set device=blueadapter.getBondedDevices();
当然,还需要在androidManifest.xml中声明蓝牙的权限5.自动配对设置Pin值
static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
throws Exception {
Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
Boolean result = (Boolean) autoBondMethod
.invoke(device, new Object[] { strPin.getBytes() });
return result;
}
6.开始配对请求
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
③ android 手机蓝牙怎样自动连接已配对的设备
蓝牙和手机的详细连接步骤如下:
1,手机打开蓝牙功能,进去搜索蓝牙耳机状态。
2,使蓝牙耳机在开机状态,需要长按蓝牙耳机的接听键。
3,直到指示灯:蓝灯红灯交替闪烁再松开。
4,这个时候蓝牙耳机和手机会再次进入匹对。
5,注意手机和蓝牙距离要在10米内。
等手机上面显示此蓝牙型号设备后,点击确定即可连接使用了。
④ 如何实现Android蓝牙开发 自动配对连接,并不弹出提示框
实现android蓝牙开发 自动配对连接,并不弹出提示框: 源码 BluetoothDevice 类中还有两个隐藏方法:cancelBondProcess()和cancelPairingUserInput(),这两个方法一个是取消配对进程一个是取消用户输入 下面是自动配对的代码 Mainfest,xml注册 <receiver android:name="." > <intent-filter> <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> </intent-filter> </receiver> 自己在收到广播时处理并将预先输入的密码设置进去 public class extends BroadcastReceiver { String strPsw = "0"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals( "android.bluetooth.device.action.PAIRING_REQUEST")) { BluetoothDevice btDevice = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234"); // device.setPin(pinBytes); Log.i("tag11111", "ddd"); try { ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对 ClsUtils.createBond(btDevice.getClass(), btDevice); ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } <b>/************************************ 蓝牙配对函数 * **************/ import java.lang.reflect.Field; import java.lang.reflect.Method; import android.bluetooth.BluetoothDevice; import android.util.Log; public class ClsUtils { /** * 与设备配对 参考源码:platform/packages/apps/Settings.git * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */ static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method createBondMethod = btClass.getMethod("createBond"); Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); return returnValue.booleanValue(); } /** * 与设备解除配对 参考源码:platform/packages/apps/Settings.git * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */ static public boolean removeBond(Class 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, String str) throws Exception { try { Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] {byte[].class}); Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] {str.getBytes()}); Log.e("returnValue", "" + returnValue); } 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(); } return true; } // 取消用户输入 static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); // cancelBondProcess() 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(); } } }</b> 执行时直接使用: <b>public static boolean pair(String strAddr, String strPsw) { boolean result = false; BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); bluetoothAdapter.cancelDiscovery(); if (!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) { // 检查蓝牙地址是否有效 Log.d("mylog", "devAdd un effient!"); } BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { try { Log.d("mylog", "NOT BOND_BONDED"); ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 ClsUtils.createBond(device.getClass(), device); remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice 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.createBond(device.getClass(), device); ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 ClsUtils.createBond(device.getClass(), device); remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice result = true; } catch (Exception e) { // TODO Auto-generated catch block Log.d("mylog", "setPiN failed!"); e.printStackTrace(); } } return result; }
⑤ android 怎么实现蓝牙的自动匹配和连接
eoe 看到过:
<receiver android:name=".broadcast.PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /$amp;>amp;$nbsp;
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /$amp;>amp;$nbsp;
</intent-filter>
</receiver>
public class PairingRequest extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
device.setPin(pinBytes);
}
}
}
其中的蓝牙BluetoothDevice这个类要用源码里的替换下(不然会缺少方法)。
⑥ android 蓝牙自动配对自动连接 可以实现吗
android蓝牙自动配对连接的具体代码如下:
1. 获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。
2. 判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。
3. 启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
4. 获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices();
当然,还需要在androidManifest.xml中声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5.自动配对设置Pin值
static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
throws Exception {
Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
Boolean result = (Boolean) autoBondMethod
.invoke(device, new Object[] { strPin.getBytes() });
return result;
}
6.开始配对请求
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
⑦ 通过android代码如何实现手机蓝牙自动连接最后一次已配对的设备
先明确场景。你是希望如下哪种自动连接?
case1: 手机开机自动连接蓝牙最后配对设备:Android已经在Framework层实现。不需要Apk再做。关键代码如下:
.java 里面
onReceive 收到事件BluetoothAdapter.STATE_ON后
else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
initiateConnection();给状态机发CONNECT
.sendMessage(.CONNECT);
case2:蓝牙关闭状态,打开蓝牙,自动连接最后使用的设备:Apk层注册监听蓝牙打开的广播,然后在事件响应中获取已配对列表,取第一个去进行连接就好。
注册监听蓝牙打开的广播事件:android.bluetooth.adapter.action.STATE_CHANGED
Bluetooth ACTION_STATE_CHANGED curState = 11,preState = 10 打开中
Bluetooth ACTION_STATE_CHANGED curState = 12,preState = 11 打开了。
获取已配对设备列表:BluetoothAdapter.getBondedDevices()
取出第一个设备(也就是最新的),连接:
framework/base/core/java/android/bluetooth/
BluetoothHeadsetClient: connect(device)
BluetoothA2dpSink: connect(device)
⑧ 如何实现android蓝牙自动配对连接
android蓝牙自动配对连接的具体代码如下:
1. 获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。
2. 判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。
3. 启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
4. 获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices();
当然,还需要在androidManifest.xml中声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5.自动配对设置Pin值
static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
throws Exception {
Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
Boolean result = (Boolean) autoBondMethod
.invoke(device, new Object[] { strPin.getBytes() });
return result;
}
6.开始配对请求
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}