Android ble 藍芽4.0 總結一

來源:互聯網
上載者:User

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比較清晰啊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.