安卓工控觸摸一體機的藍牙全面開發(fā)教程
開發(fā)系統(tǒng):Android4.4.2 運行平臺:廣州微嵌安卓工業(yè)平板 安卓的藍牙打開包括獲取藍牙模塊、搜索藍牙設(shè)備、藍牙設(shè)備間的配對、連接跟通信等部分。 1、安卓中使用藍牙模塊需要藍牙的使用權(quán)限,需要在AndroidMainfest.xml中聲明:
允許程序連接到已配對的藍牙設(shè)備允許程序發(fā)現(xiàn)和配對藍牙設(shè)備1234
2、獲取藍牙適配器并打開藍牙
//獲取藍牙適配器mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if(mBluetoothAdapter ==null){ Toast.makeText(this, "不支持藍牙設(shè)備",Toast.LENGTH_LONG).show(); bluetoothSwitch.setEnabled(false); return; } 打開藍牙設(shè)備//判斷藍牙設(shè)備是否處于關(guān)閉狀態(tài),如果是則打開藍牙if(!mBluetoothAdapter.isEnabled()){ if(mBluetoothManager.enable()){//打開藍牙設(shè)備//開啟藍牙后,需設(shè)置藍牙為可發(fā)現(xiàn)狀態(tài),這樣其它的藍牙設(shè)備才能搜索到。Intent discoverableIntent = new Intent (BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); //最后的參數(shù)設(shè)置可見的時間,最長為300s,設(shè)為0表示一直可見discoverableIntent.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0); startActivity(discoverableIntent); //成功打開藍牙后開始搜索附近的藍牙設(shè)備 mBluetoothAdapter.startDiscovery(); //停止搜索:mBluetoothAdapter.cancelDiscovery();} }else{//關(guān)閉藍牙設(shè)備 mBluetoothManager.disable(); }//獲取已配對的藍牙設(shè)備Set Bondedlist =mBluetoothAdapter.getBondedDevices();123456789101112131415161718192021222324252627
3、定義廣播接收,在開始搜索附近的藍牙設(shè)備,系統(tǒng)回發(fā)出三個搜索狀態(tài)的廣播
BluetoothDevice.ACTION_FOUND //搜索到設(shè)備BluetoothAdapter.ACTION_DISCOVERY_STARTED //開始搜索BluetoothAdapter.ACTION_DISCOVERY_FINISHED //搜索結(jié)束 定義廣播接收相應(yīng)的廣播狀態(tài)private class BluetoothReceive extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO 自動生成的方法存根 String action = intent.getAction(); //判斷廣播內(nèi)容 //搜索到藍牙設(shè)備廣播 if(action.equals(BluetoothDevice.ACTION_FOUND)){ //獲取搜素到的藍牙設(shè)備 BluetoothDevice device =intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE); if(device.getName()==null){ return; } //獲取搜素到的藍牙設(shè)備是否已經(jīng)配對 if(device.getBondState() == BluetoothDevice.BOND_BONDED){ deviceSet.add(device); adapter.add(device.getName()+":可使用"); } else { deviceSet.add(device); adapter.add(device.getName()+":可配對"); } } //搜索結(jié)束的廣播 else if(action.equals (BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ Toast.makeText(MainActivity.this, "掃描結(jié)束",Toast.LENGTH_LONG).show(); } } }
4、藍牙設(shè)備的配對
//獲取搜索到的藍牙設(shè)備列表中的藍牙設(shè)備及其狀態(tài)BluetoothDevice device = deviceSet.get(position);int state = deviceSet.get(position).getBondState();//判斷藍牙設(shè)備狀態(tài)Switch(state){ //藍牙設(shè)備沒有配對 case BluetoothDevice.BOND_NONE: //配對方法一: Method createBondMethod= device.getClass().getMethod("createBond"); Boolean returnValue =(Boolean)createBondMethod.invoke(device); //配對方法二: device.createBond(); break; //藍牙設(shè)備已經(jīng)配對 case BluetoothDevice.BOND_BONDED: //可選操作:刪除配對信息、連接藍牙設(shè)備 //刪除配對信息: Method createBondMethod= device.getClass().getMethod("removeBond"); Boolean returnValue =(Boolean)createBondMethod.invoke(device); //連接藍牙設(shè)備: //建立藍牙客戶端并連接服務(wù)器 mBluetoothClient = new BluetoothClient(MainActivity.this,device,uuid); mBluetoothClient.connect(); break; }
上面的內(nèi)容主要是獲取藍牙模塊、打開藍牙、搜素附近藍牙設(shè)備跟進行配對。 下面的是藍牙設(shè)備間建立連接并進行通信。 藍牙設(shè)備的連接、通信跟網(wǎng)絡(luò)通信TCP的類似,分別有服務(wù)器、客戶端,先是新建一個服務(wù)器用于監(jiān)聽客戶端的連接請求,客戶端向服務(wù)器發(fā)送連接請求,連接成功后雙方都獲得BluetoothSocket的實例,雙方可以通過BluetoothSocket的實例進行通信。 5、服務(wù)器: 新建一個藍牙服務(wù)器并監(jiān)聽客戶端的連接請求 在listenUsingRfcommWithServiceRecord中有一個參數(shù)叫做UUID,UUID(Universally Unique Identifier)是一個128位的字符串ID,被用于唯一標識我們的藍牙服務(wù)。
String name = mBluetoothAdapter.getName();try {//創(chuàng)建一個BluetoothServerSocket藍牙服務(wù)器,并開啟接收線程等待客戶端的連接 mServerSocket = mBluetoothAdapter .listenUsingRfcommWithServiceRecord(name, uuid); new acceptThread().start(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); }; 等待客戶端連接:public class acceptThread extends Thread{ @Override public void run() { // TODO 自動生成的方法存根 try { //該方法是服務(wù)器阻塞等待客戶端的連接, //監(jiān)聽到有客戶端連接返回一個BluetoothSocket的實例 socket = mServerSocket.accept(); Log.d("Server", "以連接"); //開啟讀取線程讀取客戶端發(fā)來的數(shù)據(jù) read = new readThread(); read.start(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } super.run(); } } 讀取數(shù)據(jù):public class readThread extends Thread{ @Override public void run() { // TODO 自動生成的方法存根 if(socket.isConnected()){ try { //獲取socket的InputStream并不斷讀取數(shù)據(jù) InputStream in = socket.getInputStream(); byte[] buff = new byte[1024]; while(!isInterrupted()){ int size = in.read(buff);; if(size>0){ Log.d("RECVDATA", String.valueOf(buff)); } } in.close(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } super.run(); } } 發(fā)送數(shù)據(jù):public void write(String str){ if(socket.isConnected()){ try {//獲取socket的OutputStream并寫入數(shù)據(jù) OutputStream out = socket.getOutputStream(); out.write(str.getBytes()); out.close(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } }//關(guān)閉服務(wù)器:mServerSocket.close();//關(guān)閉BluetoothSocket:Socket.close();
6、客戶端:
獲取一個BluetoothSocket的實例并向服務(wù)器發(fā)送連接請求public class ConnectThread extends Thread{ @Override public void run() { // TODO 自動生成的方法存根 try { //獲取BluetoothSocket實例并連接服務(wù)器,該處的uuid需與服務(wù)器短 //的uuid一致才能連接成功,connect()是回阻塞的。 socket = mBluetoothDevice .createRfcommSocketToServiceRecord(uuid); socket.connect() Log.d("TAG", "連接成功"); read = new ReadThread(); read.start(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } super.run(); } } 客戶端的讀取跟發(fā)送與服務(wù)器的相同。
至此安卓系統(tǒng)的藍牙的基本功能就基本完成了,在兩塊都開啟了藍牙的安卓設(shè)備上分別建議服務(wù)器跟客戶端,并進行連接,連接成功后雙方就可以通過藍牙進行通信了。

提交
醫(yī)療垃圾回收系統(tǒng)工業(yè)平板電腦應(yīng)用解決方案
怎么樣使用QT開發(fā)安卓工業(yè)平板電腦程序
安卓系統(tǒng)的遠程數(shù)據(jù)庫MySql操作
安卓設(shè)備的網(wǎng)絡(luò)adb調(diào)試設(shè)置
組態(tài)王在WinCE工業(yè)平板電腦的安裝使用