Posts

Showing posts with the label Bluetooth

Bluetooth Data Transfer Example

Image
The following example uses the Android Bluetooth APIs to construct a simple peer-to-peer messaging system that works between two paired Bluetooth devices. Unfortunately the Android emulator can’t currently be used to test Bluetooth functionality. In order to test this application you will need to have two physical devices. 1. Start by creating a new BluetoothTexting project featuring a BluetoothTexting Activity. Modify the manifest to include BLUETOOTH and BLUETOOTH_ADMIN permissions. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paad.chapter13_bluetoothtexting" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".BluetoothTexting" android:label="@string/app_name"> <in...

Opening a Client Bluetooth Socket Connection

Image
To initiate a communications channel to a remote device, create a Bluetooth Socket from the BluetoothDevice object that represents it. To create a new connection call createRfcommSocket ToServiceRecord on the Bluetooth Device to connect to, passing in the UUID of the Bluetooth Server Socket accepting requests. If you attempt to connect to a Bluetooth Device that has not yet been paired (bonded) with the host device, you will be prompted to accept the pairing before the connect call     completes, as shown in Figure The user must accept the pairing request on both the host and remote devices for the connection to be established. NOTE:: connect is a blocking operation, so it’s best practice to initiate connection requests on a background thread rather than block the UI thread until a connection has been made. Connecting to a remote Bluetooth server   Try{       BluetoothDevice device = bluetooth.getRemoteDevice("00:23:76:35:2F:...

How to check Device Near By Your Phone having Bluetooth?

final BluetoothDevice device = bluetooth.getRemoteDevice("01:23:77:35:2F:AA"); final Set<BluetoothDevice> bondedDevices = bluetooth.getBondedDevices(); BroadcastReceiver discoveryResult = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if ((remoteDevice.equals(device) && (bondedDevices.contains(remoteDevice)) { // TODO Target device is paired and discoverable } }; registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND)); if (!bluetooth.isDiscovering()) bluetooth.startDiscovery();

Enabling Bluetooth and tracking the adapter state

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); BroadcastReceiver bluetoothState = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String prevStateExtra = BluetoothAdapter.EXTRA_PREVIOUS_STATE; String stateExtra = BluetoothAdapter.EXTRA_STATE; int state = intent.getIntExtra(stateExtra, −1); int previousState = intent.getIntExtra(prevStateExtra, −1); String tt = ""; switch (state) { case (BluetoothAdapter.STATE_TURNING_ON) : { tt = "Bluetooth turning on"; break; } case (BluetoothAdapter.STATE_ON) : { tt = "Bluetooth on"; unregisterReceiver(this); break; } case (BluetoothAdapter.STATE_TURNING_OFF) : { tt = "Bluetooth turning off"; break; } case (BluetoothAdapter.STATE_OFF) : { tt = "Bluetooth off"; break; } default: break; } Toast.makeText(this, tt, Toast.LENGTH_LONG).show(); } }; if (!bluetooth.isEnabled()) { String actionStateChanged = Bluetoo...

Managing Bluetooth Properties and State

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); String toastText; if (bluetooth.isEnabled()) { String address = bluetooth.getAddress(); String name = bluetooth.getName(); toastText = name + " : " + address; } else toastText = "Bluetooth is not enabled"; Toast.makeText(this, toastText, Toast.LENGTH_LONG).show(); If you also have the BLUETOOTH_ADMIN permission you can change the friendly name of the Bluetooth Adapter using the setName method: bluetooth.setName("Anonimous");

What is Bluetooth and how do we used this using android?

Bluetooth is a communications protocol designed for short-range, low-bandwidth peer-to-peer com- munications. As of Android 2.1, only encrypted communication is supported, meaning you can only form connections between paired devices. In Android, Bluetooth devices and connections are handled by the following classes: The Bluetooth Adapter represents the local Bluetooth device — that is, the Android device on which your application is running. ➤ BluetoothAdapter ➤ BluetoothDevice Each remote device with which you wish to communicate is represented    as a BluetoothDevice. ➤ BluetoothSocket ➤ BluetoothServerSocket By creating a Bluetooth Server Socket (using the    listenUsingRfcommWithServiceRecord method) on your local Bluetooth Adapter, you can Call createRfcommSocketToServiceRecord on a remote Bluetooth Device object to create a Bluetooth Socket that will let you make a connection request to the remote device, and then initiate communications. ...