当前位置:首页 » 安卓系统 » android蓝牙自动配对

android蓝牙自动配对

发布时间: 2022-09-09 02:39:56

A. 安卓系统 蓝牙 与车载蓝牙的连接

你可以试一下蓝牙连接方法大体详细步骤是:
1、首先手机、车载蓝牙设备要建立配对关系。分别开启手机、车载蓝牙设备的蓝牙功能,并将手机蓝牙设置中设为所有人可见。
2、然后在手机中搜索蓝牙设备,查找到之后选中进行配对连接,配对密码为:0000,完成配对后则连接成功。
3、车载蓝牙与手机蓝牙配对连接成功后,可以拨打和接听电话;在手机上播放音乐,可在车载蓝牙设备上欣赏音乐。
4、找到音频项找到蓝牙音频再按就是蓝牙音频装置列表,此时用手机一搜就见MB Bluetooth 了。
(1)android蓝牙自动配对扩展阅读:
车载蓝牙只是以无线蓝牙技术为基础而设计研发的车内无线免提系统,主要功能是为在正常行驶中用蓝牙技术与手机连接进行免提通话,以达到解放双手、降低交通肇事隐患的目的。

B. android蓝牙配对 如何自动配对设置PIN码

Android对于音频设备是自动输入0000的pin码的,参照$frameworks/base/core/java/android/server/BluetoothEventLoop.java 的onRequestPinCode()你若是在app里编写代码,可以在收到ACTION_PAIRING_REQUEST的时候,直接调用BluetoothDevice.setpin()reference $package/apps/Settings/src/android/settings/bluetooth/BluetoothPairingDialog.java 的onPair();

C. 通过android代码如何实现手机蓝牙自动连接最后一次已配对的设备

先明确场景。你是希望如下哪种自动连接?

case1: 手机开机自动连接蓝牙最后配对设备:Android已经在Framework层实现。不需要Apk再做。关键代码如下:

.java 里面

onReceive 收到事件BluetoothAdapter.STATE_ON后

else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {

initiateConnection();给状态机发CONNECT

.sendMessage(.CONNECT);


case2:蓝牙关闭状态,打开蓝牙,自动连接最后使用的设备:Apk层注册监听蓝牙打开的广播,然后在事件响应中获取已配对列表,取第一个去进行连接就好。

  1. 注册监听蓝牙打开的广播事件:android.bluetooth.adapter.action.STATE_CHANGED

    Bluetooth ACTION_STATE_CHANGED curState = 11,preState = 10 打开中

    Bluetooth ACTION_STATE_CHANGED curState = 12,preState = 11 打开了。

  2. 获取已配对设备列表:BluetoothAdapter.getBondedDevices()

  3. 取出第一个设备(也就是最新的),连接:

    framework/base/core/java/android/bluetooth/

    BluetoothHeadsetClient: connect(device)

    BluetoothA2dpSink: connect(device)

D. 如何实现android蓝牙开发 自动配对连接,并不弹出提示框

我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册

<receiverandroid:name=".">

<intent-filter>

<actionandroid:name="android.bluetooth.device.action.PAIRING_REQUEST"/>

</intent-filter>

</receiver>

自己在收到广播时处理并将预先输入的密码设置进去


{

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>

E. 如何实现android蓝牙自动配对连接

// 自动配对设置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;
}

// 开始配对
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
使用上面的代码可以实现配对,但是会出现输入提示框(点击取消后,查看配对是已配对)

F. 如何实现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 }

G. android 手机蓝牙怎样自动连接已配对的设备

蓝牙和手机的详细连接步骤如下:

1,手机打开蓝牙功能,进去搜索蓝牙耳机状态。

2,使蓝牙耳机在开机状态,需要长按蓝牙耳机的接听键。

3,直到指示灯:蓝灯红灯交替闪烁再松开。

4,这个时候蓝牙耳机和手机会再次进入匹对。

5,注意手机和蓝牙距离要在10米内。

等手机上面显示此蓝牙型号设备后,点击确定即可连接使用了。

H. 如何实现android蓝牙自动配对连接

之前做一个Android版的蓝牙 与血压计通讯的项目,遇到最大的难题就是自动配对.
上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了
我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册

1 <receiver android:name="." >
2 <intent-filter>
3 <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
4 </intent-filter>
5 </receiver>
自己在收到广播时处理并将预先输入的密码设置进去

01 public class extends BroadcastReceiver
02 {
03
04 String strPsw = "0";
05
06 @Override
07 public void onReceive(Context context, Intent intent)
08 {
09 // TODO Auto-generated method stub
10 if (intent.getAction().equals(
11 "android.bluetooth.device.action.PAIRING_REQUEST"))
12 {
13 BluetoothDevice btDevice = intent
14 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
15
16 // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
17 // device.setPin(pinBytes);
18 Log.i("tag11111", "ddd");
19 try
20 {
21 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
22 ClsUtils.createBond(btDevice.getClass(), btDevice);
23 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
24 }
25 catch (Exception e)
26 {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 }
31
32
33 }
34 }

001 <b>/************************************ 蓝牙配对函数 * **************/
002 import java.lang.reflect.Field;
003 import java.lang.reflect.Method;
004
005 import android.bluetooth.BluetoothDevice;
006 import android.util.Log;
007 public class ClsUtils
008 {
009
010 /**
011 * 与设备配对 参考源码:platform/packages/apps/Settings.git
012 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
013 */
014 static public boolean createBond(Class btClass, BluetoothDevice btDevice)
015 throws Exception
016 {
017 Method createBondMethod = btClass.getMethod("createBond");
018 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
019 return returnValue.booleanValue();
020 }
021
022 /**
023 * 与设备解除配对 参考源码:platform/packages/apps/Settings.git
024 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
025 */
026 static public boolean removeBond(Class btClass, BluetoothDevice btDevice)
027 throws Exception
028 {
029 Method removeBondMethod = btClass.getMethod("removeBond");
030 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
031 return returnValue.booleanValue();
032 }
033
034 static public boolean setPin(Class btClass, BluetoothDevice btDevice,
035 String str) throws Exception
036 {
037 try
038 {
039 Method removeBondMethod = btClass.getDeclaredMethod("setPin",
040 new Class[]
041 {byte[].class});
042 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
043 new Object[]
044 {str.getBytes()});
045 Log.e("returnValue", "" + returnValue);
046 }
047 catch (SecurityException e)
048 {
049 // throw new RuntimeException(e.getMessage());
050 e.printStackTrace();
051 }
052 catch (IllegalArgumentException e)
053 {
054 // throw new RuntimeException(e.getMessage());
055 e.printStackTrace();
056 }
057 catch (Exception e)
058 {
059 // TODO Auto-generated catch block
060 e.printStackTrace();
061 }
062 return true;
063
064 }
065
066 // 取消用户输入
067 static public boolean cancelPairingUserInput(Class btClass,
068 BluetoothDevice device)
069
070 throws Exception
071 {
072 Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
073 // cancelBondProcess()
074 Boolean returnValue = (Boolean) createBondMethod.invoke(device);
075 return returnValue.booleanValue();
076 }
077
078 // 取消配对
079 static public boolean cancelBondProcess(Class btClass,
080 BluetoothDevice device)
081
082 throws Exception
083 {
084 Method createBondMethod = btClass.getMethod("cancelBondProcess");
085 Boolean returnValue = (Boolean) createBondMethod.invoke(device);
086 return returnValue.booleanValue();
087 }
088
089 /**
090 *
091 * @param clsShow
092 */
093 static public void printAllInform(Class clsShow)
094 {
095 try
096 {
097 // 取得所有方法
098 Method[] hideMethod = clsShow.getMethods();
099 int i = 0;
100 for (; i < hideMethod.length; i++)
101 {
102 Log.e("method name", hideMethod[i].getName() + ";and the i is:"
103 + i);
104 }
105 // 取得所有常量
106 Field[] allFields = clsShow.getFields();
107 for (i = 0; i < allFields.length; i++)
108 {
109 Log.e("Field name", allFields[i].getName());
110 }
111 }
112 catch (SecurityException e)
113 {
114 // throw new RuntimeException(e.getMessage());
115 e.printStackTrace();
116 }
117 catch (IllegalArgumentException e)
118 {
119 // throw new RuntimeException(e.getMessage());
120 e.printStackTrace();
121 }
122 catch (Exception e)
123 {
124 // TODO Auto-generated catch block
125 e.printStackTrace();
126 }
127 }
128 }</b>
执行时直接使用:

view sourceprint?
01 <b>public static boolean pair(String strAddr, String strPsw)
02 {
03 boolean result = false;
04 BluetoothAdapter bluetoothAdapter = BluetoothAdapter
05 .getDefaultAdapter();
06
07 bluetoothAdapter.cancelDiscovery();
08
09 if (!bluetoothAdapter.isEnabled())
10 {
11 bluetoothAdapter.enable();
12 }
13
14 if (!BluetoothAdapter.checkBluetoothAddress(strAddr))
15 { // 检查蓝牙地址是否有效
16
17 Log.d("mylog", "devAdd un effient!");
18 }
19
20 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);
21
22 if (device.getBondState() != BluetoothDevice.BOND_BONDED)
23 {
24 try
25 {
26 Log.d("mylog", "NOT BOND_BONDED");
27 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
28 ClsUtils.createBond(device.getClass(), device);
29 remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice
30 result = true;
31 }
32 catch (Exception e)
33 {
34 // TODO Auto-generated catch block
35
36 Log.d("mylog", "setPiN failed!");
37 e.printStackTrace();
38 } //
39
40 }
41 else
42 {
43 Log.d("mylog", "HAS BOND_BONDED");
44 try
45 {
46 ClsUtils.createBond(device.getClass(), device);
47 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
48 ClsUtils.createBond(device.getClass(), device);
49 remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
50 result = true;
51 }
52 catch (Exception e)
53 {
54 // TODO Auto-generated catch block
55 Log.d("mylog", "setPiN failed!");
56 e.printStackTrace();
57 }
58 }
59 return result;
60 }</b>

热点内容
安卓怎么关闭美易订阅 发布:2024-05-18 19:29:16 浏览:642
苹果手机配置代理服务器怎么开 发布:2024-05-18 19:29:07 浏览:229
服务器屏蔽了别人的ip 发布:2024-05-18 19:10:09 浏览:619
怎么获取ins服务器地址 发布:2024-05-18 19:10:03 浏览:30
仙方一般是什么配置 发布:2024-05-18 18:53:43 浏览:159
黑莓安卓手机主题下载到哪里 发布:2024-05-18 18:47:18 浏览:57
汤靖轩编程 发布:2024-05-18 18:46:04 浏览:533
脚本故事梗 发布:2024-05-18 18:29:02 浏览:823
安卓和csharp哪个发展好 发布:2024-05-18 18:09:30 浏览:527
换编程题库 发布:2024-05-18 18:00:58 浏览:562