Implement Dummy Marrow Charge Per Unit Of Measurement Criterion Profile Using Arduino Due + Hm-10, For Bluetoothlegatt Sample Code

Posted on

Previous post service “Connect HM-10 (BLE Module) to Android device, amongst BluetoothLeGatt sample project” exhibit step-by-step to modify Android BluetoothLeGatt sample program, by-pass Heart Rate Measurement profile treatment to exhibit component division of HM-10.

Here nosotros volition ship dummy information of Heart Rate Measurement profile past times  Arduino Due + HM-10.


First of all, edit BluetoothLeService.java to resume master treatment for Heart Rate Measurement profile:

/*  * Copyright (C) 2013 The Android Open Source Project  *  * Licensed nether the Apache License, Version 2.0 (the "License");  * yous may non purpose this file except inward compliance amongst the License.  * You may obtain a re-create of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required past times applicable constabulary or agreed to inward writing, software  * distributed nether the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either limited or implied.  * See the License for the specific linguistic communication governing permissions together with  * limitations nether the License.  */  packet com.example.android.bluetoothlegatt;  import android.app.Service; 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.bluetooth.BluetoothManager; import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log;  import java.util.List; import java.util.UUID;  /**  * Service for managing connexion together with information communication amongst a GATT server hosted on a  * given Bluetooth LE device.  */ world flat BluetoothLeService extends Service {     mortal in conclusion static String TAG = BluetoothLeService.class.getSimpleName();      mortal BluetoothManager mBluetoothManager;     mortal BluetoothAdapter mBluetoothAdapter;     mortal String mBluetoothDeviceAddress;     mortal BluetoothGatt mBluetoothGatt;     mortal int mConnectionState = STATE_DISCONNECTED;      mortal static in conclusion int STATE_DISCONNECTED = 0;     mortal static in conclusion int STATE_CONNECTING = 1;     mortal static in conclusion int STATE_CONNECTED = 2;      world in conclusion static String ACTION_GATT_CONNECTED =             "com.example.bluetooth.le.ACTION_GATT_CONNECTED";     world in conclusion static String ACTION_GATT_DISCONNECTED =             "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";     world in conclusion static String ACTION_GATT_SERVICES_DISCOVERED =             "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";     world in conclusion static String ACTION_DATA_AVAILABLE =             "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";     world in conclusion static String EXTRA_DATA =             "com.example.bluetooth.le.EXTRA_DATA";      world in conclusion static UUID UUID_HEART_RATE_MEASUREMENT =             UUID.fromString(SampleGattAttributes.HM_10);      // Implements callback methods for GATT events that the app cares about.  For example,     // connexion modify together with services discovered.     mortal in conclusion BluetoothGattCallback mGattCallback = novel BluetoothGattCallback() {         @Override         world void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {             String intentAction;             if (newState == BluetoothProfile.STATE_CONNECTED) {                 intentAction = ACTION_GATT_CONNECTED;                 mConnectionState = STATE_CONNECTED;                 broadcastUpdate(intentAction);                 Log.i(TAG, "Connected to GATT server.");                 // Attempts to honour services subsequently successful connection.                 Log.i(TAG, "Attempting to showtime service discovery:" +                         mBluetoothGatt.discoverServices());              } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {                 intentAction = ACTION_GATT_DISCONNECTED;                 mConnectionState = STATE_DISCONNECTED;                 Log.i(TAG, "Disconnected from GATT server.");                 broadcastUpdate(intentAction);             }         }          @Override         world void onServicesDiscovered(BluetoothGatt gatt, int status) {             if (status == BluetoothGatt.GATT_SUCCESS) {                 broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);             } else {                 Log.w(TAG, "onServicesDiscovered received: " + status);             }         }          @Override         world void onCharacteristicRead(BluetoothGatt gatt,                                          BluetoothGattCharacteristic characteristic,                                          int status) {             if (status == BluetoothGatt.GATT_SUCCESS) {                 broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);             }         }          @Override         world void onCharacteristicChanged(BluetoothGatt gatt,                                             BluetoothGattCharacteristic characteristic) {             broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);         }     };      mortal void broadcastUpdate(final String action) {         in conclusion Intent intent = novel Intent(action);         sendBroadcast(intent);     }      mortal void broadcastUpdate(final String action,                                  in conclusion BluetoothGattCharacteristic characteristic) {         in conclusion Intent intent = novel Intent(action);          // This is especial treatment for the Heart Rate Measurement profile.  Data parsing is         // carried out every bit per profile specifications:         // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml         if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {             int flag = characteristic.getProperties();             int format = -1;             if ((flag & 0x01) != 0) {                 format = BluetoothGattCharacteristic.FORMAT_UINT16;                 Log.d(TAG, "Heart charge per unit of measurement format UINT16.");             } else {                 format = BluetoothGattCharacteristic.FORMAT_UINT8;                 Log.d(TAG, "Heart charge per unit of measurement format UINT8.");             }             in conclusion int heartRate = characteristic.getIntValue(format, 1);             Log.d(TAG, String.format("Received pump rate: %d", heartRate));             intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));         } else {             // For all other profiles, writes the information formatted inward HEX.             in conclusion byte[] information = characteristic.getValue();             if (data != aught && data.length > 0) {                 in conclusion StringBuilder stringBuilder = novel StringBuilder(data.length);                 for(byte byteChar : data)                     stringBuilder.append(String.format("%02X ", byteChar));                 intent.putExtra(EXTRA_DATA, novel String(data) + "\n" + stringBuilder.toString());             }         }          /*         Log.v("AndroidLE", "broadcastUpdate()");          in conclusion byte[] information = characteristic.getValue();          Log.v("AndroidLE", "data.length: " + data.length);          if (data != aught && data.length > 0) {             in conclusion StringBuilder stringBuilder = novel StringBuilder(data.length);             for(byte byteChar : data) {                 stringBuilder.append(String.format("%02X ", byteChar));                  Log.v("AndroidLE", String.format("%02X ", byteChar));             }             intent.putExtra(EXTRA_DATA, novel String(data) + "\n" + stringBuilder.toString());         }         */          sendBroadcast(intent);     }      world flat LocalBinder extends Binder {         BluetoothLeService getService() {             supply BluetoothLeService.this;         }     }      @Override     world IBinder onBind(Intent intent) {         supply mBinder;     }      @Override     world boolean onUnbind(Intent intent) {         // After using a given device, yous should brand certain that BluetoothGatt.close() is called         // such that resources are cleaned upwardly properly.  In this item example, close() is         // invoked when the UI is disconnected from the Service.         close();         supply super.onUnbind(intent);     }      mortal in conclusion IBinder mBinder = novel LocalBinder();      /**      * Initializes a reference to the local Bluetooth adapter.      *      * @return Return truthful if the initialization is successful.      */     world boolean initialize() {         // For API flat eighteen together with above, teach a reference to BluetoothAdapter through         // BluetoothManager.         if (mBluetoothManager == null) {             mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);             if (mBluetoothManager == null) {                 Log.e(TAG, "Unable to initialize BluetoothManager.");                 supply false;             }         }          mBluetoothAdapter = mBluetoothManager.getAdapter();         if (mBluetoothAdapter == null) {             Log.e(TAG, "Unable to obtain a BluetoothAdapter.");             supply false;         }          supply true;     }      /**      * Connects to the GATT server hosted on the Bluetooth LE device.      *      * @param address The device address of the finish device.      *      * @return Return truthful if the connexion is initiated successfully. The connexion consequence      *         is reported asynchronously through the      *         {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}      *         callback.      */     world boolean connect(final String address) {         if (mBluetoothAdapter == aught || address == null) {             Log.w(TAG, "BluetoothAdapter non initialized or unspecified address.");             supply false;         }          // Previously connected device.  Try to reconnect.         if (mBluetoothDeviceAddress != aught && address.equals(mBluetoothDeviceAddress)                 && mBluetoothGatt != null) {             Log.d(TAG, "Trying to purpose an existing mBluetoothGatt for connection.");             if (mBluetoothGatt.connect()) {                 mConnectionState = STATE_CONNECTING;                 supply true;             } else {                 supply false;             }         }          in conclusion BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);         if (device == null) {             Log.w(TAG, "Device non found.  Unable to connect.");             supply false;         }         // We desire to straight connect to the device, together with hence nosotros are setting the autoConnect         // parameter to false.         mBluetoothGatt = device.connectGatt(this, false, mGattCallback);         Log.d(TAG, "Trying to practise a novel connection.");         mBluetoothDeviceAddress = address;         mConnectionState = STATE_CONNECTING;         supply true;     }      /**      * Disconnects an existing connexion or cancel a pending connection. The disconnection consequence      * is reported asynchronously through the      * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}      * callback.      */     world void disconnect() {         if (mBluetoothAdapter == aught || mBluetoothGatt == null) {             Log.w(TAG, "BluetoothAdapter non initialized");             return;         }         mBluetoothGatt.disconnect();     }      /**      * After using a given BLE device, the app must telephone phone this method to ensure resources are      * released properly.      */     world void close() {         if (mBluetoothGatt == null) {             return;         }         mBluetoothGatt.close();         mBluetoothGatt = null;     }      /**      * Request a read on a given {@code BluetoothGattCharacteristic}. The read consequence is reported      * asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}      * callback.      *      * @param feature The feature to read from.      */     world void readCharacteristic(BluetoothGattCharacteristic characteristic) {         if (mBluetoothAdapter == aught || mBluetoothGatt == null) {             Log.w(TAG, "BluetoothAdapter non initialized");             return;         }         mBluetoothGatt.readCharacteristic(characteristic);     }      /**      * Enables or disables notification on a give characteristic.      *      * @param feature Characteristic to deed on.      * @param enabled If true, enable notification.  False otherwise.      */     world void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,                                               boolean enabled) {         if (mBluetoothAdapter == aught || mBluetoothGatt == null) {             Log.w(TAG, "BluetoothAdapter non initialized");             return;         }         mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);          // This is specific to Heart Rate Measurement.         if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {             BluetoothGattDescriptor descriptor = characteristic.getDescriptor(                     UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));             descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);             mBluetoothGatt.writeDescriptor(descriptor);         }     }      /**      * Retrieves a listing of supported GATT services on the connected device. This should hold upwardly      * invoked alone subsequently {@code BluetoothGatt#discoverServices()} completes successfully.      *      * @return Influenza A virus subtype H5N1 {@code List} of supported services.      */     world List<BluetoothGattService> getSupportedGattServices() {         if (mBluetoothGatt == null) supply null;          supply mBluetoothGatt.getServices();     } }  

Connect Arduino Due together with HM-10:
HM-10 VCC – Arduino Due 3.3V
HM-10 GND – Arduino Due GND
HM-10 Tx – Arduino Due Rx
HM-10 Rx – Arduino Due Tx

Program on Arduino Due, DueSimHeartRate.ino. It alone ship dummy pump charge per unit of measurement information to series port.

int LED = 13; boolean LEDst = false;  byte c;  void setup() {   Serial3.begin(9600);   pinMode(LED, OUTPUT);   digitalWrite(LED, LEDst); }  void loop() {   delay(1000);   c = (byte)0x00;   Serial3.write(c);   c = (byte)80;   Serial3.write(c);    delay(1000);   c = (byte)0x00;   Serial3.write(c);   c = (byte)81;   Serial3.write(c);    delay(10);   c = (byte)0x00;   Serial3.write(c);   c = (byte)82;   Serial3.write(c);    ToggleLED(); }  void ToggleLED(){   digitalWrite(LED, LEDst = !LEDst); }