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

這兩天在研究藍牙,網上有關藍牙的內容很是有限,Github上的藍牙框架也不多很複雜,爲此我特意寫了一個最最簡單的DEMO,實現BLE藍牙接收數據的問題,java

不須要什麼特定的UUID,android

不須要什麼斷開重連,git

不須要什麼多鏈接等等,github

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

注意:app

1.本DEMO運行前提是藍牙已經配對成功,若是想實現自動配對能夠期待個人下一篇文章框架

2.修改代碼中的「你想要接收數據的已配對設備名稱」爲你真實的藍牙設備ide

3.複製粘貼下面的代碼,日誌TAG是「BLE」ui

代碼:this

<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

歡迎關注個人微信公衆號:安卓圈

相關文章
相關標籤/搜索