生活随笔 
收集整理的這篇文章主要介紹了
                                
android开发之蓝牙配对连接的方法 
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
 
                                
                             
                             
                            最近在做藍(lán)牙開鎖的小項(xiàng)目,手機(jī)去連接單片機(jī)總是出現(xiàn)問題,和手機(jī)的連接也不穩(wěn)定,看了不少藍(lán)牙方面的文檔,做了個(gè)關(guān)于藍(lán)牙連接的小結(jié)。
在做android藍(lán)牙串口連接的時(shí)候一般會(huì)使用
?
1 
2 
3 
4 
5 
6 
7 
8
 
BluetoothSocket tmp = null; 
 
 
try { 
tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
} catch (IOException e) { 
Log.e(TAG, "create() failed", e); 
}
 
 
 
然后是tmp賦給BluetoothSocket,接著調(diào)用connect方法進(jìn)行藍(lán)牙設(shè)備的連接。
可是 BluetoothSocket 的connect方法本身就會(huì)報(bào)很多異常錯(cuò)誤。
以下根據(jù)對(duì)藍(lán)牙開發(fā)的一點(diǎn)研究可通過以下方法解決:
方法1.先進(jìn)行藍(lán)牙自動(dòng)配對(duì),配對(duì)成功,通過UUID獲得BluetoothSocket,然后執(zhí)行connect()方法。
方法2.通過UUID獲得BluetoothSocket,然后先根據(jù)mDevice.getBondState()進(jìn)行判斷是否需要配對(duì),最后執(zhí)行connnect()方法。
?
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44
 
private class ConnectThread extends Thread { 
String macAddress = ""; 
 
public ConnectThread(String mac) { 
macAddress = mac; 
} 
 
public void run() { 
connecting = true; 
connected = false; 
if(mBluetoothAdapter == null){ 
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
} 
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress); 
mBluetoothAdapter.cancelDiscovery(); 
try { 
socket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid); 
 
} catch (IOException e) { 
 
 
Log.e(TAG, "Socket", e); 
}  
 
while (!connected && connetTime <= 10) {  
connectDevice(); 
} 
 
 
 
 
} 
 
public void cancel() { 
try { 
socket.close(); 
socket = null; 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
connecting = false; 
} 
} 
}
 
 
 
接下來是調(diào)用的連接設(shè)備方法connectDevice():
?
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37
 
protected void connectDevice() {  
try {  
 
if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {  
Method creMethod = BluetoothDevice.class 
.getMethod("createBond");  
Log.e("TAG", "開始配對(duì)");  
creMethod.invoke(mBluetoothDevice);  
} else {  
}  
} catch (Exception e) {  
 
 
e.printStackTrace();  
}  
mBluetoothAdapter.cancelDiscovery();  
try {  
socket.connect();  
 
 
connected = true; 
} catch (IOException e) {  
 
 
connetTime++; 
connected = false; 
try {  
socket.close(); 
socket = null; 
} catch (IOException e2) {  
 
Log.e(TAG, "Cannot close connection when connection failed");  
}  
} finally { 
connecting = false; 
}  
}
 
 
 
方法3.利用反射通過端口獲得BluetoothSocket,然后執(zhí)行connect()方法。
?
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54
 
private class ConnectThread extends Thread { 
String macAddress = ""; 
 
public ConnectThread(String mac) { 
macAddress = mac; 
} 
 
public void run() { 
connecting = true; 
connected = false; 
if(mBluetoothAdapter == null){ 
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
} 
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress); 
mBluetoothAdapter.cancelDiscovery(); 
initSocket();  
 
while (!connected && connetTime <= 10) { 
try { 
socket.connect(); 
connected = true; 
} catch (IOException e1) { 
connetTime++; 
connected = false; 
 
try { 
socket.close(); 
socket = null; 
} catch (IOException e2) { 
 
Log.e(TAG, "Socket", e2); 
} 
} finally { 
connecting = false; 
} 
 
} 
 
 
 
 
} 
 
public void cancel() { 
try { 
socket.close(); 
socket = null; 
} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
connecting = false; 
} 
} 
}
 
 
 
接下來是初始化并得到BluetoothSocket的方法
?
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22
 
/** 
* 取得BluetoothSocket 
*/ 
private void initSocket() { 
BluetoothSocket temp = null; 
try {  
Method m = mBluetoothDevice.getClass().getMethod( 
"createRfcommSocket", new Class[] { int.class }); 
temp = (BluetoothSocket) m.invoke(mBluetoothDevice, 1); 
} catch (SecurityException e) { 
e.printStackTrace(); 
} catch (NoSuchMethodException e) { 
e.printStackTrace(); 
} catch (IllegalArgumentException e) { 
e.printStackTrace(); 
} catch (IllegalAccessException e) { 
e.printStackTrace(); 
} catch (InvocationTargetException e) { 
e.printStackTrace(); 
} 
socket = temp; 
}
 
 
 
要點(diǎn):1.藍(lán)牙配對(duì)和連接是兩回事,不可混為一談。
   2.藍(lán)牙串口連接可通過端口 (1-30)和UUID兩種方法進(jìn)行操作。
   3.通過UUID進(jìn)行藍(lán)牙連接最好先進(jìn)行配對(duì)操作。
                            總結(jié) 
                            
                                以上是生活随笔 為你收集整理的android开发之蓝牙配对连接的方法 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。