CS5530 Mobile/Wireless Systems Android Bluetooth Interface Yanyan - - PowerPoint PPT Presentation

cs5530 mobile wireless systems android bluetooth interface
SMART_READER_LITE
LIVE PREVIEW

CS5530 Mobile/Wireless Systems Android Bluetooth Interface Yanyan - - PowerPoint PPT Presentation

CS5530 Mobile/Wireless Systems Android Bluetooth Interface Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang UC. Colorado Springs CS5530 Ref. CN5E, NT@UW, WUSTL Bluetooth Communication Bluetooth-enabled


slide-1
SLIDE 1
  • Ref. CN5E, NT@UW, WUSTL

CS5530

CS5530 Mobile/Wireless Systems Android Bluetooth Interface

Yanyan Zhuang

Department of Computer Science http://www.cs.uccs.edu/~yzhuang

  • UC. Colorado Springs
slide-2
SLIDE 2

Bluetooth Communication

  • Ref. CN5E, NT@UW, WUSTL

2 CS5530

  • Bluetooth-enabled devices must first complete

a pairing process

  • A discoverable device makes itself available for incoming connection
  • Another device finds the discoverable device using a discovery process
  • After the discoverable device accepts the pairing

request, the two devices complete a bonding process

  • After pairing and bonding are complete, two devices exchange info
  • Two devices remain bonded
  • So they can reconnect automatically during a future session as long as

they're in range of each other and neither device has removed the bond

slide-3
SLIDE 3

Android Bluetooth Interface

  • Ref. CN5E, NT@UW, WUSTL

3 CS5530

  • Using Bluetooth APIs, an Android app can

perform the following

  • Scan for other Bluetooth devices
  • Query local Bluetooth adapter for paired Bluetooth devices
  • Establish communication channels
  • Transfer data to and from other devices
  • Manage multiple connections
slide-4
SLIDE 4

Bluetooth Permissions

  • Ref. CN5E, NT@UW, WUSTL

4 CS5530

  • "android.permission.BLUETOOTH"
  • To use Bluetooth features, app must declare the

permission BLUETOOTH

  • For any Bluetooth communication, such as requesting a

connection, accepting a connection, and transferring data

  • "android.permission.BLUETOOTH_ADMIN"
  • If app wants to initiate device discovery or manipulate

Bluetooth settings, must declare BLUETOOTH_ADMIN in addition to the BLUETOOTH permission

slide-5
SLIDE 5

Types of Bluetooth Devices

  • Ref. CN5E, NT@UW, WUSTL

5 CS5530

  • A Bluetooth device can either be
  • Paired

} Paired devices are aware of each other’s existence and share a link

key, which can be used to authenticate

  • Connected

} Connected devices share a channel, allowing them to send and

receive data

  • Unknown

} Remote devices that visible but the current device isn't paired with

yet

slide-6
SLIDE 6

Setting up Bluetooth

  • Ref. CN5E, NT@UW, WUSTL

6 CS5530

  • Creating a BluetoothAdapter (similar to

WiFiManager)

  • BluetoothAdapter bluetoothAdapter =

BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // Device does not support Bluetooth }

slide-7
SLIDE 7

Querying Paired Devices

  • Ref. CN5E, NT@UW, WUSTL

7 CS5530

  • To see if the desired device is already known
  • Set<BluetoothDevice> pairedDevices =

bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { Log.d(TAG, "Device name: " + device.getName()); Log.d(TAG, "Device address: " + device.getAddress()); } } else { Log.d(TAG, "No paired device found :("); }

slide-8
SLIDE 8

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

8 CS5530

  • Asynchronous
  • The discovery process usually involves an inquiry scan of

about 12 seconds

  • App must register a BroadcastReceiver for the

ACTION_FOUND intent

} The system broadcasts this intent for each discovered device (WiFi

scan returns all)

} The intent contains the extra fields EXTRA_DEVICE and EXTRA_CLASS,

which in turn contain a BluetoothDevice and a BluetoothClass, respectively

slide-9
SLIDE 9

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

9 CS5530

  • Create an IntentFilter
  • bluetoothFilter = new

IntentFilter(BluetoothDevice.ACTION_FOUND);

  • Register the receiver
  • by calling registerReceiver(BroadcastReceiver, IntentFilter)

} E.g., Intent bluetoothIntent =

getApplicationContext().registerReceiver(bluetoothReceiver, bluetoothFilter);

slide-10
SLIDE 10

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

10 CS5530

  • Create an instance of BroadcastReceiver
  • public static BroadcastReceiver bluetoothReceiver = new

BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // define what to do when receive the broadcast } };

  • Stop receiving broadcasts
  • unregisterReceiver()
slide-11
SLIDE 11

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

11 CS5530

  • App request start/stop scan
  • bluetoothAdapter.startDiscovery(); // needs permission

// BLUETOOTH_ADMIN

  • bluetoothAdapter.cancelDiscovery();
slide-12
SLIDE 12

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

12 CS5530

  • What to do when receive the broadcast?
  • public void onReceive(Context context, Intent intent) {

String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG, "Device name: " + device.getName()); Log.d(TAG, "Device address: " + device.getAddress()); short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); Log.d(TAG, "RSSI: " + rssi); BluetoothClass deviceClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); Log.d(TAG, "Class: " + deviceClass.getMajorDeviceClass()); } }

slide-13
SLIDE 13

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

13 CS5530

  • What to do when receive the broadcast?
  • public void onReceive(Context context, Intent intent) {

String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // we found some device } }

slide-14
SLIDE 14

Discovering Devices (Scan)

  • Ref. CN5E, NT@UW, WUSTL

14 CS5530

  • Getting the device (name and MAC address)
  • BluetoothDevice device =

intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG, "Device name: " + device.getName()); Log.d(TAG, "Device address: " + device.getAddress());

  • Getting signal strength
  • short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,

Short.MIN_VALUE); Log.d(TAG, "RSSI: " + rssi);

  • Getting device class (profile)
  • BluetoothClass deviceClass =

intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); Log.d(TAG, "Class: " + deviceClass.getMajorDeviceClass());

slide-15
SLIDE 15

Bluetooth Permission (Android 6.0 or later)

  • Ref. CN5E, NT@UW, WUSTL

15 CS5530

  • AndroidManifest.xml permission and runtime permission

in code

  • AndroidManifest.xml:
  • <uses-permission

android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

  • r <uses-permission

android:name="android.permission.ACCESS_COARSE_LOCATION" />

slide-16
SLIDE 16

Bluetooth Permission (Android 6.0 or later)

  • Ref. CN5E, NT@UW, WUSTL

16 CS5530

  • Runtime check and request permission
  • String[] PERMS_INITIAL={

Manifest.permission.ACCESS_FINE_LOCATION, }; if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, PERMS_INITIAL, MY_PERMISSIONS_REQUEST); }

slide-17
SLIDE 17

Devices Class/Profile

  • Ref. CN5E, NT@UW, WUSTL

17 CS5530

  • AUDIO_VIDEO
  • Constant value: 1024 (0x00000400)
  • COMPUTER
  • Constant value: 256 (0x00000100)
  • HEALTH
  • Constant value: 2304 (0x00000900)
  • IMAGING
  • Constant value: 1536 (0x00000600)
  • UNCATEGORIZED
  • Constant value: 7936 (0x00001f00)
slide-18
SLIDE 18

My Scan Result

  • Ref. CN5E, NT@UW, WUSTL

18 CS5530

  • See text file!