Searching For Bluetooth Devices

Bluetooth is a wireless protocol for exchanging data over short distances from fixed and mobile devices, creating personal area networks(PANs).When any two devices need to talk to each other, they have to agree on a number of points before the conversation can begin.The Bluetooth protocol is used to communicate with mobile devices in wireless
media.It is a well known and popular technology that is being used by many mobile device manufactures.

Mainly the Bluetooth technology is used in mobile phones.Here is some code examples to search for Bluetooth devices near to your device.

Start Inquiring for Bluetooth Devices

LocalDevice localDeviceData = LocalDevice.getLocalDevice();
DiscoveryAgent bluetoothDiscoveryAgent = localDeviceData.getDiscoveryAgent();
bluetoothDiscoveryAgent.startInquiry(0x9e8b33, this);

After finishing the inquiry of Bluetooth devices,the deviceDiscovered(RemoteDevice rd, DeviceClass arg1) method will be invoked.

Vector remoteDevices;
List bluetoothDevicesList;
remoteDevices.addElement(rd);
bluetoothDevicesList.append(rd.getFriendlyName(false), null);


After finishing the device discovery then the application must search for services that the Bluetooth device provide.

RemoteDevice remoteDevice;
UUID blueAddresses[]= new UUID[1];
UUID bluetoothAddressUUID = new UUID("1106", true);
blueAddresses[0] = bluetoothAddressUUID;
remoteDevice = (RemoteDevice) remoteDevices.elementAt(iterator);
bluetoothDiscoveryAgent.searchServices(null, blueAddresses, remoteDevice, this);

When searching for services it will store the services at an array of ServiceRecord.
Then it will invoke servicesDiscovered(int a, ServiceRecord[] servRecord) method.

String serviceUrl;
for (int j = 0; j < servRecord.length; j++) {
serviceUrl = servRecord[j].getConnectionURL(0, false);

Finally we can connect to the Bluetooth device using the serviceUrl and start the communication between the devices.

}

Comments

Popular posts from this blog

SSL Pinning in Android

Java Unit testing using JUnit @RunWith(Suite.class)

Android Event Bus Implementations using Otto