這裡重點說下藍芽連線:
在做android藍芽串列埠連線的時候一般會使用
bluetoothsocket tmp = null;
// get a bluetoothsocket for a connection with the
// given bluetoothdevice
try catch (ioexception e)
然後是tmp賦給bluetoothsocket,接著呼叫connect方法進行藍芽裝置的連線。
可是 bluetoothsocket 的connect方法本身就會報很多異常錯誤。
以下根據對藍芽開發的一點研究可通過以下方法解決:
方法1.先進行藍芽自動配對,配對成功,通過uuid獲得bluetoothsocket,然後執行connect()方法。
方法2.通過uuid獲得bluetoothsocket,然後先根據mdevice.getbondstate()進行判斷是否需要配對,最後執行connnect()方法。
/**接下來是呼叫的連線裝置方法connectdevice():* * 藍芽連線線程
* *
* @author
lsw *
*/private
class connectthread extends
thread
public
void
run()
mbluetoothdevice =mbluetoothadapter.getremotedevice(macaddress);
mbluetoothadapter.canceldiscovery();
//initsocket();
try
catch
(ioexception e)
while (!connected && connettime <= 10)
//重置connectthread
//synchronized (bluetoothservice.this)
}
public
void
cancel()
catch
(exception e)
finally}}
protected方法3.利用反射通過埠獲得bluetoothsocket,然後執行connect()方法。void
connectdevice()
else
} catch
(exception e)
mbluetoothadapter.canceldiscovery();
try
catch
(ioexception e)
catch
(ioexception e2)
} finally
}
/**接下來是初始化並得到bluetoothsocket的方法* * 藍芽連線線程
* *
* @author
lsw *
*/private
class connectthread extends
thread
public
void
run()
mbluetoothdevice =mbluetoothadapter.getremotedevice(macaddress);
mbluetoothadapter.canceldiscovery();
initsocket();
while (!connected && connettime <= 10)
catch
(ioexception e1)
catch
(ioexception e2)
} finally
//connectdevice();
}
//重置connectthread
//synchronized (bluetoothservice.this)
}
public
void
cancel()
catch
(exception e)
finally}}
/**要點:1.藍芽配對和連線是兩回事,不可混為一談。* 取得bluetoothsocket
*/private
void
initsocket() );
temp = (bluetoothsocket) m.invoke(mbluetoothdevice, 1);//
這裡埠為1
} catch
(securityexception e)
catch
(nosuchmethodexception e)
catch
(illegalargumentexception e)
catch
(illegalacces***ception e)
catch
(invocationtargetexception e)
socket =temp;
}
2.藍芽串列埠連線可通過埠 (1-30)和uuid兩種方法進行操作。
3.通過uuid進行藍芽連線最好先進行配對操作。
個人見解,僅供參考!
連線串列埠 Arduino 藍芽串列埠通訊
藍芽串列埠通訊 利用 藍芽 技術,能夠有效簡化移動通訊終端裝置之間的通訊,也能成功簡化裝置與internet之間的通訊,從而資料傳輸變得更迅速高效,為無線通訊拓寬道路。我們都用過手機藍芽,連線自己的耳機或者和別人的手機傳輸資料,使用起來非常方便簡單。在我們的arduino中,也可以使用藍芽來傳輸資料...
Android 藍芽連線
一 概述 藍芽是一種無線技術標準,可實現固定裝置 移動裝置和樓宇個人域網之間的短距離資料交換。最多可以同時和7個其它藍芽裝置建立連線,進行通訊。藍芽可分為兩大類 傳統藍芽 藍芽3.0規範之前 低功耗藍芽 藍芽4.0規範之後 android 從4.3版本 api level 18 開始支援低功耗藍芽b...
Android 藍芽連線
今天要做乙個藍芽4.0的通訊,先做個小test,之後再看看具體的api override public void onclick view v 3,掃瞄附近的裝置 list devicelist new arraylist if bluetoothadapter.isdiscovering else...