10分鍾完成一個最最簡單的BLE藍牙接收數據的DEMO


這兩天在研究藍牙,網上有關藍牙的內容非常有限,Github上的藍牙框架也很少很復雜,為此我特地寫了一個最最簡單的DEMO,實現BLE藍牙接收數據的問題,

不需要什么特定的UUID,

不需要什么斷開重連,

不需要什么多連接等等,

網上都把BLE藍牙寫的好復雜好復雜,那不是我想要的,我只想為新手提供一個最基本的例子

注意:

1.本DEMO運行前提是藍牙已經配對成功,如果想實現自動配對可以期待我的下一篇文章

2.修改代碼中的“你想要接收數據的已配對設備名稱”為你真實的藍牙設備

3.復制粘貼下面的代碼,日志TAG是“BLE”

代碼:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.Toast; import java.util.List; import java.util.Set; import java.util.UUID; public class MainActivity extends AppCompatActivity { private BluetoothAdapter adapter; private BluetoothGatt bluetoothGatt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openBlueToothLe(); } //打開藍牙
    private void openBlueToothLe() { adapter = BluetoothAdapter.getDefaultAdapter(); if (null == adapter) { Toast.makeText(this, "沒有藍牙功能", Toast.LENGTH_SHORT).show(); return; } if (!adapter.isEnabled()) { adapter.enable(); } startScan(); } //開始掃描
    private void startScan() { Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices(); for (BluetoothDevice bondedDevice : bondedDevices) { if ("你想要接收數據的已配對設備名稱".equals(bondedDevice.getName().trim())) { connectDevice(bondedDevice); } } } //連接設備
    private void connectDevice(BluetoothDevice bondedDevice) { bluetoothGatt = bondedDevice.connectGatt(this, false, new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (BluetoothGatt.STATE_CONNECTED == newState) { bluetoothGatt = gatt; gatt.discoverServices(); } else if (BluetoothGatt.STATE_DISCONNECTED == newState) { gatt.close(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); List<BluetoothGattService> services = gatt.getServices(); for (BluetoothGattService service : services) { List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); for (BluetoothGattCharacteristic character : characteristics) { enableNotification(gatt, service.getUuid(), character.getUuid()); } } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); byte[] value = characteristic.getValue(); Log.i("BLE", "receive value ----------------------------"); for (int i = 0; i < value.length; i++) { Log.i("BLE", "character_value = " + value[i]); } } }); } @Override protected void onDestroy() { super.onDestroy(); bluetoothGatt.disconnect(); } public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) { boolean success = false; BluetoothGattService service = gatt.getService(serviceUUID); if (service != null) { BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID); if (characteristic != null) { success = gatt.setCharacteristicNotification(characteristic, true); if (success) { // 來源:http://stackoverflow.com/questions/38045294/oncharacteristicchanged-not-called-with-ble
                    for (BluetoothGattDescriptor dp : characteristic.getDescriptors()) { if (dp != null) { if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); } gatt.writeDescriptor(dp); } } } } } return success; } private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) { BluetoothGattCharacteristic characteristic = null; List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); for (BluetoothGattCharacteristic c : characteristics) { if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
                    && characteristicUUID.equals(c.getUuid())) { characteristic = c; break; } } if (characteristic != null) return characteristic; for (BluetoothGattCharacteristic c : characteristics) { if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
                    && characteristicUUID.equals(c.getUuid())) { characteristic = c; break; } } return characteristic; } }

對,就是這么簡單,一個類足以,接下來就可以在Android studio的Logcat看到打印的返回值了

Github地址:https://github.com/king1039/BlueToothLe

歡迎關注我的微信公眾號:安卓圈


免責聲明!

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



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