當前位置:首頁 » 安卓系統 » 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-04 21:36:31 瀏覽:551
給linux虛擬機傳文件 發布:2024-05-04 21:36:28 瀏覽:312
加密手機在哪裡 發布:2024-05-04 21:18:59 瀏覽:190
掃雷電腦配置低怎麼辦 發布:2024-05-04 21:07:37 瀏覽:821
微雲緩存文件在哪裡 發布:2024-05-04 20:53:07 瀏覽:509
咸陽市移動dns伺服器地址 發布:2024-05-04 20:39:19 瀏覽:442
小車哪個配置好 發布:2024-05-04 20:38:38 瀏覽:797
周長的演算法 發布:2024-05-04 20:37:46 瀏覽:371
保衛蘿卜2安卓版什麼時候更新 發布:2024-05-04 20:08:48 瀏覽:203
建工黨建宣傳片腳本 發布:2024-05-04 20:07:31 瀏覽:219