Android ble 藍芽4.0 總結一,androidble
本文介紹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比較清晰啊
samsung ble 藍芽40與moto ble 藍芽40開發是不一樣的??
兩家的API不同,開發自然也不一樣。
藍芽40 BLE支援
嗨!
哈哈哈有什麼rom能把我的2s刷成4100萬像素??
米2的藍芽就是4.0的
小米企業平台[官方認證]