Android ble 藍牙4.0 總結一


本文介紹Android ble 藍牙4.0,也就是說API level >= 18,且支持藍牙4.0的手機才可以使用,如果手機系統版本API level < 18,也是用不了藍牙4.0的哦。

首先發一下官方的demo,有興趣的可以過去看看:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html。android系統4.3以上,手機支持藍牙4.0,具有搜索,配對,連接,發現服務及特征值,斷開連接等功能,下載地址:http://download.csdn.net/detail/lqw770737185/8116019

一、了解api及概念

1.1 BluetoothGatt

繼承BluetoothProfile,通過BluetoothGatt可以連接設備(connect),發現服務(discoverServices),並把相應地屬性返回到BluetoothGattCallback 

1.2 BluetoothGattCharacteristic

相當於一個數據類型,它包括一個value和0~n個value的描述(BluetoothGattDescriptor)

1.3 BluetoothGattDescriptor

描述符,對Characteristic的描述,包括范圍、計量單位等

1.4 BluetoothGattService

服務,Characteristic的集合。

1.5 BluetoothProfile

 一個通用的規范,按照這個規范來收發數據。

1.6 BluetoothManager

 通過BluetoothManager來獲取BluetoothAdapter

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

1.7 BluetoothAdapter

一個Android系統只有一個BluetoothAdapter ,通過BluetoothManager 獲取

BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

1.8 BluetoothGattCallback

已經連接上設備,對設備的某些操作后返回的結果。這里必須提醒下,已經連接上設備后的才可以返回,沒有返回的認真看看有沒有連接上設備。

		private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
			// 這里有9個要實現的方法,看情況要實現那些,用到那些就實現那些
			public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){};
			public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){};
		};
		BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
		BluetoothGatt gatt = device.connectGatt(this, false, mGattCallback);

1.8.1:notification對應onCharacteristicChanged

gatt.setCharacteristicNotification(characteristic, true);

1.8.2:readCharacteristic對應onCharacteristicRead;

gatt.readCharacteristic(characteristic);

1.8.3: writeCharacteristic對應onCharacteristicWrite;

gatt.wirteCharacteristic(mCurrentcharacteristic);

1.8.4:連接藍牙或者斷開藍牙 對應 onConnectionStateChange;

1.8.5: readDescriptor對應onDescriptorRead;

1.8.6:writeDescriptor對應onDescriptorWrite;

gatt.writeDescriptor(descriptor); 

1.8.7:readRemoteRssi對應onReadRemoteRssi;

gatt.readRemoteRssi()

1.8.8:executeReliableWrite對應onReliableWriteCompleted;

1.8.9:discoverServices對應onServicesDiscovered。

gatt.discoverServices()

1.9 BluetoothDevice

掃描后發現可連接的設備,獲取已經連接的設備

二、開啟藍牙權限

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

如果 android.hardware.bluetooth_le設置為false,可以安裝在不支持的設備上使用,判斷是否支持藍牙4.0用以下代碼就可以了

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, "設備不支持藍牙4.0", Toast.LENGTH_SHORT).show();
    finish();
}

三、對藍牙的啟動關閉操作

1、利用系統默認開啟藍牙對話框

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

2、后台打開藍牙,不做任何提示,這個也可以用來自定義打開藍牙對話框啦

mBluetoothAdapter.enable();

3、后台關閉藍牙

mBluetoothAdapter.disable();

四、掃描設備,連接設備,獲取設備信息 ,斷開連接設備,自行查看官方demo,還是看demo比較清晰啊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM