Bluetooth APIs were made available as part of the Android 2.0 SDK. Clearly, that means that not all Android devices have Bluetooth hardware. However, this is a popular consumer feature that Android developers can use to their advantage. When Bluetooth hardware is present, Android applications can
The Bluetooth APIs are part of the android.bluetooth package. As you might expect, the application must have permission to use the Bluetooth services. The android .permission. BLUETOOTH permission is required to connect to Bluetooth devices. Similarly, Android applications must have the android.permission.BLUETOOTH_ADMIN permission in order to administer Bluetooth hardware and related services, including tasks enabling or disabling the hardware and performing discovery scans.
The Bluetooth APIs are divided into several useful classes, including
Checking for the Existence of Bluetooth Hardware
The first thing to do when trying to enable Bluetooth functionality within your application is to establish whether or not the device has a Bluetooth radio. You can do this by calling and checking the return value of the BluetoothAdapter class’s static method called getDefault Adapter().BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
After you have determined that the device has a Bluetooth radio, you need to check to see if it is enabled using the BluetoothAdapter class method called isEnabled(). If the Bluetooth adapter is enabled, you can proceed. Otherwise, you need to request that it is turned on. This can be done in several ways:
Querying for Paired Devices
You can use the BluetoothAdapter to query for available Bluetooth devices to connect to. The getBondedDevices() method returns a set of BluetoothDevice objects that represent the devices paired to the Bluetooth adapter.Set<BluetoothDevice> pairedBtDevices = btAdapt.getBondedDevices();
New Bluetooth devices must be discovered and paired to the adapter before use. You can use the BluetoothAdapter to start and stop the discovery process for available Bluetooth devices to connect to. The startDiscovery() method starts the discovery process asynchronously. This method requires the android. permission. BLUETOOTH_ ADMIN permission.
After you have initiated the discovery process, your application needs to register to receive broadcasts for the following Intents:
The discovery process is resource and time-intensive. You can use the isDiscovering() method to test if the discovery process is currently underway. The cancel Discovery() method can be used to stop the discovery process. This method should also be used any time a connection is about to be established with a remote Bluetooth device.
Establishing Connections Between Devices
The general idea behind connecting two devices via Bluetooth is for one device to find the other device via whatever means necessary, depending upon whether it be a previously paired device or found through discovery. After it’s found, the device calls the connect() method. Both devices then have a valid BluetoothSocket object that can be used to retrieve the InputStream and OutputStream objects for initiating data communications between the two devices.
Now, that’s where the theory ends and reality sets in. If it’s the same application running on both devices, as it usually is, this means both devices should find a remote device and both should be discoverable so they can also be found, as well as open a listening socket via the BluetoothServerSocket object so they can receive incoming connection requests, and be able to connect to the other device. Add to that the fact that both the calls to the accept() method of the BluetoothServerSocket class and to the connect() method of the BluetoothSocket class are blocking synchronous calls, and you can quickly see you need to use some threads here. Discovery also uses a fair amount of the Bluetooth hardware resources, so you need to cancel and then later restart this process as appropriate. Performing discovery during a connection or even while attempting a connection likely leads to negative device performance.
Figure shows a reasonable layout for a Bluetooth implementation, as well as labeling the threads used within the SimpleBluetooth project.
Setting Up Your Android Development Environment
Writing Your First Android Application
Understanding The Anatomy Of An Android Application
Defining Your Application Using The Android Manifest File
Managing Application Resources
Exploring User Interface Screen Elements
Designing User Interfaces With Layouts
Drawing And Working With Animation
Using Android Data And Storage Apis
Sharing Data Between Applications With Content Providers
Using Android Networking Apis
Using Android Web Apis
Using Location-based Services (lbs) Apis
Using Android Multimedia Apis
Using Android Telephony Apis
Using Android 3d Graphics With Opengl Es
Using The Android Ndk
Using Android’s Optional Hardware Apis
Working With Notifications
Working With Services
Extending Android Application Reach
Managing User Accounts And Synchronizing User Data
Handling Advanced User Input
Targeting Different Device Configurations And Languages
The Mobile Software Development Process
Designing And Developing Bulletproof Android Applications
Testing Android Applications
Selling Your Android Application
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.