先來了解下Android傳統藍牙連接的大致簡單的流程:
其中涉及到幾個類依次來介紹,廢話不多說,下面是從Android4.4開發指南藍牙所用到的類的截圖:
第一個類BluetoothAdapter:
注意兩點:
1這是一個繼承子Object的final類,不能進行繼承。
2在系統為4.2及以下可以調用靜態方法getDefaultAdapter()獲取設備本地適配器;在系統為4.3及以上版本調用BluetoothManager的
getAdapter()
類中其他重要方法:獲取已經配對的設備--BluetoothDevices的set集合,開始發現設備--bool,創建偵聽的RFCOMM安全/非安全通道
遠程藍牙設備,可以通過UUID創建出BluetoothSocket(藍牙套接字接口)對象,可以進行連接操作。
詳細說明下createRfcommSocketToServiceRecord方法:
創建一個RFCOMM藍牙套接字准備開始一個安全的傳出連接到遠程設備。返回的是BluetoothSocket對象
注意點:如果連接藍牙串行板,嘗試使用著名的UUID-00001101-0000-1000-8000-00805F9B34FB(一般固定的)然而若是一個Android對等體請使用自己生成的UUID
第三,四個類BluetoothSocket與BluetoothServerSocket
首先看下BluetoothSocket介紹:
藍牙套接字接口類似tcp套接字(Socket與ServerSocket);
在服務端方面,使用一個BluetoothServerSocket來創建一個偵聽的服務端套接字。當一個連接被BluetoothServerSocket接受,它將返回一個新的BluetoothSocket來管理連接;在客戶端,使用單個BluetoothSocket來啟動傳出連接和管理連接。
最常見的藍牙套接字類型是RFCOMM,這是Android API支持的類型。 RFCOMM是面向連接的,通過藍牙的流傳輸。 它也稱為串行端口配置文件(SPP)。
使用 BluetoothDevice.createRfcommSocketToServiceRecord()去創建一個BluetoothSocket連接一個已知的設備,然后通過他回調connect()與遠程設備建立一個連接。
一旦套接字已連接,無論是連接為客戶端還是連接為服務端,通過調用getInputStream()與getOutputStream()來分別檢索InputStream對象,這些對象分別自動連接到套接字。
BluetoothSocket是線程安全的,另外,close() 方法將立即終止正在進行的操作和關閉套接字。
再來看下BluetoothServerSocket類:
其中有兩個重載方法,一個可設置超時連接,方法阻塞,直到建立連接
返回值為BluetoothSocket對象可以管理連接,數據共享交互
第五個類為BluetoothClass:描述藍牙設備的一般特征和功能,暫時用不到。
下面來介紹下具體連接藍牙功能的代碼實現:
思路:如果將藍牙連接與數據通信部分放在Activity中,那么假如退出Activity,套接字也會隨着activity關閉,而且每打開一次Activity又 要原樣來一遍,又不穩定。有沒有一中可以一直在后台運行的東西,可控制性的去管理它呢?
通過服務與廣播機制來實現Activity與Service通信,Service啟動方式有兩種,一種是通過bindService(),另一種是通過startService(),
這兩種啟動方式的區別大家去清楚,bindService方式的服務隨着調用者消亡而死亡;startService方式創建一次就會存在,除非 stopself()方法或者進程死亡。兩種方式都可以實現與Activity交互,顯然采用第二種方式更穩定,Activity與Service的通信采用廣播機制 清晰簡單。
原理介紹下:
下面貼下Demo代碼:
1需要聲明權限,android:exported指的是能否與其他程序交互,不能被訪問則為false.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.likego.gobackbluetooth"> <!--聲明藍牙權限!--> <uses-permission android:name="android.permission.BLUETOOTH" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".BlueToothService" android:exported="false"></service> </application> </manifest>
2.界面代碼
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查找發現設備" android:id="@+id/lookBtn" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發送數據" android:id="@+id/sendBtn" android:layout_below="@+id/lookBtn" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空數據" android:id="@+id/clearBtn" android:layout_below="@+id/sendBtn" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:id="@+id/editText" android:text="返回結果:" android:singleLine="false" android:scrollbars="vertical" android:editable="false" android:focusable="false" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_below="@+id/clearBtn" android:layout_alignParentBottom="true" android:gravity="top" /> </RelativeLayout>
3.Activity代碼-發現與查找設備,連接設備,發送數據等。
4.SerVice代碼