cs5530 mobile wireless systems android bluetooth interface
play

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


  1. 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

  2. Bluetooth Communication • Bluetooth-enabled devices must first complete a pairing process o A discoverable device makes itself available for incoming connection o 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 o After pairing and bonding are complete, two devices exchange info • Two devices remain bonded o 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 CS5530 2 Ref. CN5E, NT@UW, WUSTL

  3. Android Bluetooth Interface • Using Bluetooth APIs, an Android app can perform the following o Scan for other Bluetooth devices o Query local Bluetooth adapter for paired Bluetooth devices o Establish communication channels o Transfer data to and from other devices o Manage multiple connections CS5530 3 Ref. CN5E, NT@UW, WUSTL

  4. Bluetooth Permissions • "android.permission.BLUETOOTH" o To use Bluetooth features, app must declare the permission BLUETOOTH o For any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data • "android.permission.BLUETOOTH_ADMIN" o If app wants to initiate device discovery or manipulate Bluetooth settings, must declare BLUETOOTH_ADMIN in addition to the BLUETOOTH permission CS5530 4 Ref. CN5E, NT@UW, WUSTL

  5. Types of Bluetooth Devices • A Bluetooth device can either be o Paired } Paired devices are aware of each other’s existence and share a link key, which can be used to authenticate o Connected } Connected devices share a channel, allowing them to send and receive data o Unknown } Remote devices that visible but the current device isn't paired with yet CS5530 5 Ref. CN5E, NT@UW, WUSTL

  6. Setting up Bluetooth • Creating a BluetoothAdapter (similar to WiFiManager) o BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // Device does not support Bluetooth } CS5530 6 Ref. CN5E, NT@UW, WUSTL

  7. Querying Paired Devices • To see if the desired device is already known o 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 :("); } CS5530 7 Ref. CN5E, NT@UW, WUSTL

  8. Discovering Devices (Scan) • Asynchronous o The discovery process usually involves an inquiry scan of about 12 seconds o 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 CS5530 8 Ref. CN5E, NT@UW, WUSTL

  9. Discovering Devices (Scan) • Create an IntentFilter o bluetoothFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); • Register the receiver o by calling registerReceiver(BroadcastReceiver, IntentFilter) } E.g., Intent bluetoothIntent = getApplicationContext().registerReceiver(bluetoothReceiver, bluetoothFilter); CS5530 9 Ref. CN5E, NT@UW, WUSTL

  10. Discovering Devices (Scan) • Create an instance of BroadcastReceiver o 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 o unregisterReceiver() CS5530 10 Ref. CN5E, NT@UW, WUSTL

  11. Discovering Devices (Scan) • App request start/stop scan o bluetoothAdapter.startDiscovery(); // needs permission // BLUETOOTH_ADMIN o bluetoothAdapter.cancelDiscovery(); CS5530 11 Ref. CN5E, NT@UW, WUSTL

  12. Discovering Devices (Scan) • What to do when receive the broadcast? public void onReceive(Context context, Intent intent) { o 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()); } } CS5530 12 Ref. CN5E, NT@UW, WUSTL

  13. Discovering Devices (Scan) • What to do when receive the broadcast? o public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // we found some device } } CS5530 13 Ref. CN5E, NT@UW, WUSTL

  14. Discovering Devices (Scan) • Getting the device (name and MAC address) o BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG, "Device name: " + device.getName()); Log.d(TAG, "Device address: " + device.getAddress()); • Getting signal strength o short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); Log.d(TAG, "RSSI: " + rssi); • Getting device class (profile) o BluetoothClass deviceClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); Log.d(TAG, "Class: " + deviceClass.getMajorDeviceClass()); CS5530 14 Ref. CN5E, NT@UW, WUSTL

  15. Bluetooth Permission (Android 6.0 or later) • AndroidManifest.xml permission and runtime permission in code • AndroidManifest.xml: o <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> or <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> CS5530 15 Ref. CN5E, NT@UW, WUSTL

  16. Bluetooth Permission (Android 6.0 or later) • Runtime check and request permission o 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); } CS5530 16 Ref. CN5E, NT@UW, WUSTL

  17. Devices Class/Profile • AUDIO_VIDEO o Constant value: 1024 (0x00000400) • COMPUTER o Constant value: 256 (0x00000100) • HEALTH o Constant value: 2304 (0x00000900) • IMAGING o Constant value: 1536 (0x00000600) • UNCATEGORIZED o Constant value: 7936 (0x00001f00) CS5530 17 Ref. CN5E, NT@UW, WUSTL

  18. My Scan Result • See text file! CS5530 18 Ref. CN5E, NT@UW, WUSTL

Recommend


More recommend