首先,要操作藍牙,先要在A(yíng)ndroidManifest.xml里加入權限<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permissionandroid:name="android.permission.BLUETOOTH" />
然后,看下api,Android所有關(guān)于藍牙開(kāi)發(fā)的類(lèi)都在android.bluetooth包下,如下圖,只有8個(gè)類(lèi)
而我們需要用到了就只有幾個(gè)而已:
1.BluetoothAdapter 顧名思義,藍牙適配器,直到我們建立bluetoothSocket連接之前,都要不斷操作它
BluetoothAdapter里的方法很多,常用的有以下幾個(gè):
cancelDiscovery() 根據字面意思,是取消發(fā)現,也就是說(shuō)當我們正在搜索設備的時(shí)候調用這個(gè)方法將不再繼續搜索
disable()關(guān)閉藍牙
enable()打開(kāi)藍牙,這個(gè)方法打開(kāi)藍牙不會(huì )彈出提示,更多的時(shí)候我們需要問(wèn)下用戶(hù)是否打開(kāi),一下這兩行代碼同樣是打開(kāi)藍牙,不過(guò)會(huì )提示用戶(hù):
Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);
getAddress()獲取本地藍牙地址
getDefaultAdapter()獲取默認BluetoothAdapter,實(shí)際上,也只有這一種方法獲取BluetoothAdapter
getName()獲取本地藍牙名稱(chēng)
getRemoteDevice(String address)根據藍牙地址獲取遠程藍牙設備
getState()獲取本地藍牙適配器當前狀態(tài)(感覺(jué)可能調試的時(shí)候更需要)
isDiscovering()判斷當前是否正在查找設備,是返回true
isEnabled()判斷藍牙是否打開(kāi),已打開(kāi)返回true,否則,返回false
listenUsingRfcommWithServiceRecord(String name,UUID uuid)根據名稱(chēng),UUID創(chuàng )建并返回BluetoothServerSocket,這是創(chuàng )建BluetoothSocket服務(wù)器端的第一步
startDiscovery()開(kāi)始搜索,這是搜索的第一步
2.BluetoothDevice看名字就知道,這個(gè)類(lèi)描述了一個(gè)藍牙設備
createRfcommSocketToServiceRecord(UUIDuuid)根據UUID創(chuàng )建并返回一個(gè)BluetoothSocket
這個(gè)方法也是我們獲取BluetoothDevice的目的——創(chuàng )建BluetoothSocket
這個(gè)類(lèi)其他的方法,如getAddress(),getName(),同BluetoothAdapter
3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不過(guò)了,既然是Socket,方法就應該都差不多,
這個(gè)類(lèi)一種只有三個(gè)方法
兩個(gè)重載的accept(),accept(int timeout)兩者的區別在于后面的方法指定了過(guò)時(shí)時(shí)間,需要注意的是,執行這兩個(gè)方法的時(shí)候,直到接收到了客戶(hù)端的請求(或是過(guò)期之后),都會(huì )阻塞線(xiàn)程,應該放在新線(xiàn)程里運行!
還有一點(diǎn)需要注意的是,這兩個(gè)方法都返回一個(gè)BluetoothSocket,最后的連接也是服務(wù)器端與客戶(hù)端的兩個(gè)BluetoothSocket的連接
close()這個(gè)就不用說(shuō)了吧,翻譯一下——關(guān)閉!
4.BluetoothSocket,跟BluetoothServerSocket相對,是客戶(hù)端
一共5個(gè)方法,不出意外,都會(huì )用到
close(),關(guān)閉
connect()連接
getInptuStream()獲取輸入流
getOutputStream()獲取輸出流
getRemoteDevice()獲取遠程設備,這里指的是獲取bluetoothSocket指定連接的那個(gè)遠程藍牙設備。
1、獲取本地藍牙適配器
BluetoothAdapter
mAdapter= BluetoothAdapter.getDefaultAdapter();
2、打開(kāi)藍牙
if(!mAdapter.isEnabled()){
//彈出對話(huà)框提示用戶(hù)是后打開(kāi)
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, REQUEST_ENABLE);
//不做提示,強行打開(kāi)
// mAdapter.enable();
}
3、搜索設備
1)剛才說(shuō)過(guò)了mAdapter.startDiscovery()
是第一步,可以你會(huì )發(fā)現沒(méi)有返回的藍牙設備,怎么知道查找到了呢?向下看,不要急
2)定義BroadcastReceiver,關(guān)于BroadcastReceiver不多講了,不是今天的討論內容,代碼如下
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//找到設備
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
Log.v(TAG, "find device:" + device.getName()
+ device.getAddress());
}
}
//搜索完成
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
setTitle("搜索完成");
if (mNewDevicesAdapter.getCount() == 0) {
Log.v(TAG,"find over");
}
}
//執行更新列表的代碼
}
};
這樣,沒(méi)當查找到新設備或是搜索完成,相應的操作都在上段代碼的兩個(gè)if里執行了,不過(guò)前提是你要先注冊
BroadcastReceiver,具體代碼如下
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
(這段代碼,一般寫(xiě)在onCreate()里..)
3建立連接,首先Android sdk(2.0以上版本)支持的藍牙連接是通過(guò)BluetoothSocket建立連接(說(shuō)的不對請高人指正),服務(wù)器端(BluetoothServerSocket)和客戶(hù)端(BluetoothSocket)需指定同樣的UUID,才能建立連接,因為建立連接的方法會(huì )阻塞線(xiàn)程,所以服務(wù)器端和客戶(hù)端都應啟動(dòng)新線(xiàn)程連接
1)服務(wù)器端:
//UUID格式一般是"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"可到
//http://www.uuidgenerator.com 申請
BluetoothServerSocket serverSocket = mAdapter. listenUsingRfcommWithServiceRecord(serverSocketName,UUID);
serverSocket.accept();
2)客戶(hù)端:
//還記得我們剛才在BroadcastReceiver獲取了BLuetoothDevice么?
BluetoothSocket clienSocket=dcvice. createRfcommSocketToServiceRecord(UUID);
clienSocket.connect();
4、數據傳遞,通過(guò)以上操作,就已經(jīng)建立的BluetoothSocket連接了,數據傳遞無(wú)非是通過(guò)流的形式
1)獲取流
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
2)寫(xiě)出、讀入
略
android.bluetooth下有8個(gè)類(lèi),還有4個(gè)類(lèi)沒(méi)有用到,那4個(gè)類(lèi)里定義的都是常量
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。