Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wireless Communication between Android Application and Sensors

Similar presentations


Presentation on theme: "Wireless Communication between Android Application and Sensors"— Presentation transcript:

1 Wireless Communication between Android Application and Sensors
CS2310 MSE Seminar Android Application Sensors Bluetooth(Bluetooth Low Energy)

2 Topics Wireless communication among devices in local environment
Wireless communication between sensors & Android application Development of Android application Wireless communication between sensors & Android application via Bluetooth

3 Wireless communication among devices in local environment
Devices on the same local network Network Service Discovery (NSD) Server Socket and Client Socket(not exactly local) Peer to peer Near Field Communication (NFC) WiFi-P2P Bluetooth-based communication between devices or sensors Classic Bluetooth Bluetooth Low Energy

4 Wireless communication between sensors & Android application
Bluetooth Low Energy: In contrast to Classic Bluetooth, Bluetooth Low Energy (BLE) is designed to provide significantly lower power consumption. This allows Android apps to communicate with BLE devices that have low power requirements, such as proximity sensors, heart rate monitors, fitness devices, and so on. Classic Bluetooth: A device to wirelessly exchange data with other Bluetooth devices. Classic Bluetooth is the right choice for more battery-intensive operations such as streaming and communicating between Android devices.

5 Wireless communication between sensors & Android application via Bluetooth
What do we need? Sensors Arduino & Arduino IDE(Integrated Development Environment) Alternative platforms: Galileo, Raspberry Pi, Single-chip microcomputers, etc. Arduino is an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board. Bluetooth module Android development environment Java environment & Android IDE

6 Step 1: Hardware setup Connect the Bluetooth module to Arduino board

7 Step 2: Communication serial port & pin configuration
Choose a serial port to be used as communication port Choose pins on the Arduino to be configured as inputs Choose pins on the Arduino to be configured as outputs

8 Step 3: Developement on Arduino side
void setup() { // initialize serial communications Serial.begin(9600); ... // set pin to input pinMode(inputPinNumber, INPUT); // set pin to output pinMode(outputPinNumber, OUTPUT); }

9 Step 3: Developement on Arduino side
void loop() { //write data to the serial port Serial.println(“”); //read data from the serial port if(Serial.available()) { data = Serial.read(); } //read data from an input pin inputValue = analogRead(inputPinNumber); //Write data to the output stream digitalWrite(outputPinNumber, OUTPUT_STATUS);

10 Step 4: Developement on Android side - Get prepared
Android IDE Android Studio(IntelliJ,Alternative: Eclipse)

11 Step 4: Developement on Android side - Get prepared
Android components - Activities An activity represents a single screen with a user interface. An activity is implemented as a subclass of Activity.

12 Step 4: Developement on Android side - Get prepared - BLE Types
Length limit of a characteristic: 20 bytes long. Think of a Bluetooth LE peripheral device(server) as a bulletin board and central devices(clients) as viewers of the board. Central devices view the services, get the data, then move on. Each transaction is quick (a few milliseconds), so multiple central devices can get data from one peripheral.

13 Step 4: Developement on Android side - Get prepared
Generic Attribute Profile (GATT): The GATT profile is a general specification for sending and receiving short pieces of data known as "attributes" over a BLE link. All current Low Energy application profiles are based on GATT. Attribute Protocol (ATT)—GATT is built on top of the Attribute Protocol (ATT). This is also referred to as GATT/ATT. ATT is optimized to run on BLE devices. To this end, it uses as few bytes as possible. Each attribute is uniquely identified by a Universally Unique Identifier (UUID), which is a standardized 128-bit format for a string ID used to uniquely identify information. The attributes transported by ATT are formatted as characteristics and services. GATT is the layer that defines services and characteristics and enables read/write/notify/indicate operations on them.

14 Step 4: Developement on Android side - Bluetooth APIs
BLE Permissions In order to use Bluetooth features in the application, we must declare the Bluetooth permission BLUETOOTH. we need this permission to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data. Declare the Bluetooth permission(s) in the application manifest file. For example: <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

15 Step 4: Developement on Android side - Bluetooth APIs
Setting up BLE Get the BluetoothAdapter // Initializes Bluetooth adapter. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); Enable Bluetooth // Ensures Bluetooth is available on the device and it is enabled. If not, // displays a dialog requesting user permission to enable Bluetooth. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }

16 Step 4: Developement on Android side - Bluetooth APIs
Finding BLE services // Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { //find a device } }); };

17 Step 4: Developement on Android side
- A simple demo

18 Step 4: Developement on Android side - Bluetooth APIs
Connecting to a GATT server The first step in interacting with a BLE device is connecting to it— more specifically, connecting to the GATT server on the device. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { } public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { } else { public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,int status) { ... };

19 Step 4: Developement on Android side
- A simple demo

20 Step 4: Developement on Android side - Bluetooth APIs
Reading BLE attributes Once the Android application has connected to a GATT server and discovered services, it can read and write attributes, where supported. Reading BLE services Once your Android app has connected to a GATT server and discovered services, it can read and write attributes, where supported. Closing the application Once the application has finished using a BLE device, it should call close() so the system can release resources appropriately.

21 Step 4: Developement on Android side
Uniform & Bluetooth-form independent solution Bluetooth signal can come from different forms: Classic Bluetooth: PC or mobile devices. Bluetooth Low Energy (BLE): Arduino, Single-chip microcomputers, etc. A general communication interface could be utilized: public class TDRCommunicationInterface { //get sensor name String getSensorName(); //read data from the serial port Boolean connectToSensor(); String collectData(); Boolean stopSensor(); } We can also utilize the Java Reflection Technology for dynamically loading Bluetooth drivers if needed by using the interface above. In that case, we won't have to update the application version whenever a new sensor comes in.

22 REFERENCES

23 Thank you!


Download ppt "Wireless Communication between Android Application and Sensors"

Similar presentations


Ads by Google