API
BluetoothDevice
java.lang.Object
↳android.bluetooth.BluetoothDevice
代表 遠程藍牙設備
。通過BluetoothDevice,您可以與各自的設備建立連接或查詢有關該設備的信息,例如名稱,地址,類和綁定狀態。
//硬件地址形如: "00:11:22:AA:BB:CC"
String address = BluetoothDevice.address
//設備名稱(如果無則返回空)
String name = BluetoothDevice.name
//設備類型:
int type = BluetoothDevice.type
//未知類型
public static final int DEVICE_TYPE_UNKNOWN = 0;
//BR、EDR(經典)藍牙類型
public static final int DEVICE_TYPE_CLASSIC = 1;
//BLE(低功耗)藍牙類型
public static final int DEVICE_TYPE_LE = 2;
//雙模式類型
public static final int DEVICE_TYPE_DUAL = 3;
//設備支持的特徵通用唯一標識符(UUID)
int uuids = BluetoothDevice.uuids
//設備綁定狀態
int bondState = BluetoothDevice.bondState
//未與遠程藍牙設備綁定(配對),所以通信(如果允許)將是未經身份認證和加密的
public static final int BOND_NONE = 10;
//與遠程藍牙設備正在綁定(配對)
public static final int BOND_BONDING = 11;
//與遠程藍牙設備已經綁定(配對),通訊是經過身份驗證和加密的(設備綁定(配對)並不一定已連接。)
public static final int BOND_BONDED = 12;
BluetoothClass
java.lang.Object
↳android.bluetooth.BluetoothClass
表示 藍牙類別
,該類別描述設備的一般特徵和功能。例如,藍牙類將指定常規設備類型(例如電話,計算機或頭戴式耳機),以及是否支持諸如音頻或電話之類的服務。
-
getMajorDeviceClass
返回的是
主要設備類型
(比如是麥克風、電話、電腦、網絡、音視頻、外圍設備等等)。/** *返回的是主要設備,從此函數返回的值可以與BluetoothClass.Device.Major中的公共常量進行比較, *以確定當前設備主要類型。 */ int majorDeviceClass = BluetoothClass.getMajorDeviceClass; public static class Device { public static class Major { //麥克風 public static final int MISC = 0x0000; //電腦 public static final int COMPUTER = 0x0100; //電話 public static final int PHONE = 0x0200; //網絡 public static final int NETWORKING = 0x0300; //音視頻 public static final int AUDIO_VIDEO = 0x0400; //外圍 public static final int PERIPHERAL = 0x0500; //圖像 public static final int IMAGING = 0x0600; //可穿戴設備 public static final int WEARABLE = 0x0700; //玩具 public static final int TOY = 0x0800; //健康 public static final int HEALTH = 0x0900; //未分類 public static final int UNCATEGORIZED = 0x1F00; } }
-
getDeviceClass
返回此BluetoothClass的(主要和次要)設備類組件其中包含了
getMajorDeviceClass
類別。/** *此BluetoothClass的(主要和次要)設備類組件,從此函數返回的值可以與BluetoothClass.Device.Major *中的公共常量進行比較,以確定當前設備主要和次要類型。 */ int deviceClass = BluetoothClass.getDeviceClass public static class Device { //DeviceClass包含了上面的MajorDeviceClass,不再重複標示類型,參照上面getMajorDeviceClass public static class Major { public static final int MISC = 0x0000; public static final int COMPUTER = 0x0100; public static final int PHONE = 0x0200; public static final int NETWORKING = 0x0300; public static final int AUDIO_VIDEO = 0x0400; public static final int PERIPHERAL = 0x0500; public static final int IMAGING = 0x0600; public static final int WEARABLE = 0x0700; public static final int TOY = 0x0800; public static final int HEALTH = 0x0900; public static final int UNCATEGORIZED = 0x1F00; } //未分類的可穿戴設備 public static final int WEARABLE_UNCATEGORIZED = 0x0700; //可穿戴手錶 public static final int WEARABLE_WRIST_WATCH = 0x0704; //可穿戴尋呼機 public static final int WEARABLE_PAGER = 0x0708; //可穿戴夾克 public static final int WEARABLE_JACKET = 0x070C; //可穿戴頭盔 public static final int WEARABLE_HELMET = 0x0710; //可穿戴眼睛 public static final int WEARABLE_GLASSES = 0x0714; //其他取值如下: // Devices in the COMPUTER major class(major爲電腦的Devices有如下取值) public static final int COMPUTER_UNCATEGORIZED = 0x0100; public static final int COMPUTER_DESKTOP = 0x0104; //筆記本電腦 public static final int COMPUTER_SERVER = 0x0108; public static final int COMPUTER_LAPTOP = 0x010C; public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110; public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114; public static final int COMPUTER_WEARABLE = 0x0118; // Devices in the PHONE major class(major爲電話的Devices有如下取值) public static final int PHONE_UNCATEGORIZED = 0x0200; public static final int PHONE_CELLULAR = 0x0204; public static final int PHONE_CORDLESS = 0x0208; public static final int PHONE_SMART = 0x020C; public static final int PHONE_MODEM_OR_GATEWAY = 0x0210; public static final int PHONE_ISDN = 0x0214; // Minor classes for the AUDIO_VIDEO major class(major爲音視頻的Devices有如下取值) public static final int AUDIO_VIDEO_UNCATEGORIZED = 0x0400; public static final int AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404; public static final int AUDIO_VIDEO_HANDSFREE = 0x0408; //public static final int AUDIO_VIDEO_RESERVED = 0x040C; public static final int AUDIO_VIDEO_MICROPHONE = 0x0410; public static final int AUDIO_VIDEO_LOUDSPEAKER = 0x0414; public static final int AUDIO_VIDEO_HEADPHONES = 0x0418; public static final int AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C; public static final int AUDIO_VIDEO_CAR_AUDIO = 0x0420; public static final int AUDIO_VIDEO_SET_TOP_BOX = 0x0424; public static final int AUDIO_VIDEO_HIFI_AUDIO = 0x0428; public static final int AUDIO_VIDEO_VCR = 0x042C; public static final int AUDIO_VIDEO_VIDEO_CAMERA = 0x0430; public static final int AUDIO_VIDEO_CAMCORDER = 0x0434; public static final int AUDIO_VIDEO_VIDEO_MONITOR = 0x0438; public static final int AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C; public static final int AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440; //public static final int AUDIO_VIDEO_RESERVED = 0x0444; public static final int AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448; // Devices in the TOY major class(major爲玩具的Devices有如下取值) public static final int TOY_UNCATEGORIZED = 0x0800; public static final int TOY_ROBOT = 0x0804; public static final int TOY_VEHICLE = 0x0808; public static final int TOY_DOLL_ACTION_FIGURE = 0x080C; public static final int TOY_CONTROLLER = 0x0810; public static final int TOY_GAME = 0x0814; // Devices in the HEALTH major class(major爲健康的Devices有如下取值) public static final int HEALTH_UNCATEGORIZED = 0x0900; public static final int HEALTH_BLOOD_PRESSURE = 0x0904; public static final int HEALTH_THERMOMETER = 0x0908; public static final int HEALTH_WEIGHING = 0x090C; public static final int HEALTH_GLUCOSE = 0x0910; public static final int HEALTH_PULSE_OXIMETER = 0x0914; public static final int HEALTH_PULSE_RATE = 0x0918; public static final int HEALTH_DATA_DISPLAY = 0x091C; }
連接建立方式
安全連接
-
listenUsingRfcommWithServiceRecord & createRfcommSocketToServiceRecord
安全的連接、需要PIN配對、通信加密
listenUsingRfcommWithServiceRecord
&createRfcommSocketToServiceRecord
服務端與客戶端將使用UUID創建一個安全的RFCOMM藍牙Socket來監聽和連接 使用此種安全方式,連接到此Socket的遠程設備將通過身份驗證(即使用PIN碼配對 以防中間人攻擊),並且此Socket上的通信將被加密。
不安全連接
-
listenUsingInsecureRfcommWithServiceRecord & createInsecureRfcommSocketToServiceRecord
非安全連接、無需身份認證(PIN配對)、藍牙2.1設備已經強制加密
listenUsingInsecureRfcommWithServiceRecord
&createInsecureRfcommSocketToServiceRecord
:服務端與客戶端將使用UUID創建一個非安全的RFCOMM藍牙Socket來監聽和連接,通信通道將沒有經過身份驗證的鏈接密鑰(不會進行PIN配對),即它將受到中間人攻擊。(對於藍牙2.1設備,鏈接密鑰將被加密,因爲加密是強制性的。對於舊版設備(藍牙2.1之前的設備),鏈接密鑰不會被加密。如果需要加密並經過身份驗證的通信通道,請使用listenUsingRfcommWithServiceRecord
使用注意
-
listenUsingRfcommWithServiceRecord
僅在能夠進行認證的設備上使用,如果設備不具有輸入和輸出功能或僅具有顯示功能(無法進行PIN確認,如耳機等),這種情況下請使用:listenUsingInsecureRfcommWithServiceRecord
。 -
安全與非安全的創建方式服務端與客戶端應
對等使用
(即服務端使用安全的方式創建連接監聽,客戶端也應使用安全的方式來創建連接)。否則一方使用安全方式,另一方使用非安全方式,在配對後無法連接出現如下類似異常getBluetoothService() called with no BluetoothManagerCallback java.io.IOException: read failed, socket might closed or timeout, read ret: -1