Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package moppydesk.outputs;
- import javax.bluetooth.*;
- import java.io.IOException;
- public class BluetoothScan {
- private boolean scanFinished = false;
- private RemoteDevice hc05device;
- private String hc05Url;
- public String scan() throws BluetoothStateException {
- LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener() {
- @Override
- public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
- try {
- String name = btDevice.getFriendlyName(false);
- System.out.format("%s (%s)\n", name, btDevice.getBluetoothAddress());
- if (name.contains("HT")) {//!!!!!!!Replace this with a part of your Bluetooth name e.g. HT-05
- hc05device = btDevice;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void inquiryCompleted(int discType) {
- scanFinished = true;
- }
- @Override
- public void serviceSearchCompleted(int transID, int respCode) {
- }
- @Override
- public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
- }
- });
- while (!scanFinished) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- //search for services:
- UUID uuid = new UUID(0x1101); //scan for btspp://... services (as HC-05 offers it)
- UUID[] searchUuidSet = new UUID[]{uuid};
- int[] attrIDs = new int[]{
- 0x0100 // service name
- };
- scanFinished = false;
- LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet,
- hc05device, new DiscoveryListener() {
- @Override
- public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
- }
- @Override
- public void inquiryCompleted(int discType) {
- }
- @Override
- public void serviceSearchCompleted(int transID, int respCode) {
- scanFinished = true;
- }
- @Override
- public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
- for (int i = 0; i < servRecord.length; i++) {
- hc05Url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
- if (hc05Url != null) {
- break; //take the first one
- }
- }
- }
- });
- while (!scanFinished) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(hc05Url);
- return hc05Url;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement